[Mathematica] NIntegrate Piecewise function with DE solution

In summary, the issue with the NIntegrate function not working was due to the output of NDSolve being a list. The solution was to take the first element of the list in the myy function definition. After making this adjustment, the NIntegrate function was able to successfully integrate the Piecewise function.
  • #1
jackmell
1,807
54
Hi,

I'd like to solve a DE, create a function with the solution, then use that solution in a Piecewise function, and then NIntegrate the Piecewise function but I can't get NIntegate to work. Here's what I'm trying to do:

Code:
mysol = NDSolve[{y'[x] == x, y[0] == 1}, y, {x, 0, 1}]
myy[x_] := Evaluate[y[x] /. mysol];

myf[x_] := Piecewise[{{myy[x], 0 < x <= 1}, {x^2, x > 1}}];

NIntegrate[myf[x], {x, 0, 2}]

NIntegrate then tells me it's not numeric in the interval.

Can someone explain to me what I'm doing wrong?

Thanks,
Jack
 
Physics news on Phys.org
  • #2
The output of NDSolve (mysol) is a list. You need to take the first element of it in your myy function definition, as follows:

Code:
In[25]:= mysol = NDSolve[{y'[x] == x, y[0] == 1}, y, {x, 0, 1}]

In[26]:= myy[x_] := Evaluate[y[x] /. mysol][[1]];

In[27]:= myf[x_] := Piecewise[{{myy[x], 0 < x <= 1}, {x^2, x > 1}}];

In[28]:= NIntegrate[myf[x], {x, 0, 2}]

Out[28]= 3.5
 
  • #3
phyzguy said:
The output of NDSolve (mysol) is a list. You need to take the first element of it in your myy function definition, as follows:

Code:
In[25]:= mysol = NDSolve[{y'[x] == x, y[0] == 1}, y, {x, 0, 1}]

In[26]:= myy[x_] := Evaluate[y[x] /. mysol][[1]];

In[27]:= myf[x_] := Piecewise[{{myy[x], 0 < x <= 1}, {x^2, x > 1}}];

In[28]:= NIntegrate[myf[x], {x, 0, 2}]

Out[28]= 3.5

Ok. Thanks a bunch. It's working now. :)
 

1. How do I use the NIntegrate function in Mathematica to integrate a piecewise function?

To integrate a piecewise function in Mathematica using the NIntegrate function, you can specify the function as an argument within the NIntegrate function and define the limits of integration. For example, NIntegrate[Piecewise[{{x^2, x < 1}, {x^3, x >= 1}}], {x, 0, 2}] would integrate the piecewise function x^2 from 0 to 1 and x^3 from 1 to 2.

2. Can I use NIntegrate to integrate a piecewise function with a differential equation solution?

Yes, you can use NIntegrate to integrate a piecewise function that includes a differential equation solution. You will need to define the piecewise function and differential equation separately, and then use them as arguments within the NIntegrate function.

3. How do I specify different integration methods for different intervals in a piecewise function?

To specify different integration methods for different intervals in a piecewise function, you can use the Method option within the NIntegrate function. For example, NIntegrate[Piecewise[{{x^2, x < 1}, {x^3, x >= 1}}], {x, 0, 2}, Method -> {"GlobalAdaptive", "SymbolicProcessing" -> False}] would use the "GlobalAdaptive" method for the first interval and turn off symbolic processing for the second interval.

4. Is it possible to use NIntegrate to integrate a piecewise function with multiple variables?

Yes, you can use NIntegrate to integrate a piecewise function with multiple variables. You will need to define the function and its variables separately, and then use them as arguments within the NIntegrate function.

5. How can I improve the accuracy of my NIntegrate calculation for a piecewise function?

To improve the accuracy of your NIntegrate calculation for a piecewise function, you can try increasing the number of integration points or using a more advanced integration method such as "DoubleExponential". You can also try adjusting the precision and accuracy settings within the NIntegrate function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
157
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
963
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
202
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top