[Mathematica] NIntegrate Piecewise function with DE solution

Click For Summary
SUMMARY

This discussion addresses the issue of using the output from Mathematica's NDSolve function within a Piecewise function for numerical integration with NIntegrate. The user, Jack, initially encounters an error due to not extracting the first element from the list returned by NDSolve. The solution involves modifying the function definition for myy to include [[1]], allowing NIntegrate to successfully compute the integral of the Piecewise function. The final output of the integration is confirmed to be 3.5.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of differential equations and their numerical solutions
  • Knowledge of Piecewise functions in Mathematica
  • Experience with numerical integration using NIntegrate
NEXT STEPS
  • Explore the use of NDSolve for more complex differential equations in Mathematica
  • Learn about advanced features of Piecewise functions in Mathematica
  • Investigate error handling in NIntegrate for better debugging
  • Study the implications of list outputs in Mathematica function definitions
USEFUL FOR

Mathematica users, mathematicians, and engineers working with differential equations and numerical integration techniques.

jackmell
Messages
1,806
Reaction score
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
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
 
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. :)
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K