# [Mathematica] NIntegrate Piecewise function with DE solution

• Mathematica

## Main Question or Discussion Point

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 == 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

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
phyzguy
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:= mysol = NDSolve[{y'[x] == x, y == 1}, y, {x, 0, 1}]

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

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

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

Out= 3.5

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:= mysol = NDSolve[{y'[x] == x, y == 1}, y, {x, 0, 1}]

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

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

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

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