Using NDSolve in Mathematica to Solve ODEs and Find Function Values

  • Thread starter Thread starter zeebo17
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
NDSolve in Mathematica can be used to solve ordinary differential equations (ODEs) and evaluate function values at specific points. To find the value of a function, such as y[5], after solving an ODE, one can use the command y /. Solution, where Solution is the output from NDSolve. Changing y[x] to just y allows for easier evaluation of the function at specific values, as it treats y as a function rather than an expression dependent on x. Flatten is used to simplify the output format from NDSolve, making it easier to extract the function. For coupled differential equations, a similar approach can be applied to evaluate expressions like Re[y[x] + z[x]] at specific x values.
zeebo17
Messages
40
Reaction score
0
I have used NDSolve in mathematica to find a solution to an ODE. How would I then find what is the value for the function y at certain values of x? Basically I just want to ask it what y[5] equals and have it print it to the screen. What is the command to do this?

Thank you!

Solution = NDSolve[{y'[x] == 3 y[x] (1 - y[x]), y[0] == .7}, y[x], {x, -10, 10}]
Plot[Evaluate[y[x] /. Solution], {x, -10, 10}, PlotRange -> {-1.5, 1.5}]
 
Physics news on Phys.org
I suggest replacing y[x] in the second argument of NDSolve by y. Then y /. Solution is really a function, e.g.
Code:
Solution = NDSolve[{y'[x] == 3 y[x] (1 - y[x]), y[0] == .7}, y, {x, -10, 10}];
Plot[y[x] /. Solution, {x, -10, 10}, PlotRange -> {-1.5, 1.5}]
y[5] /. Solution

or perhaps even more conveniently,

Code:
solution = y /. Flatten[NDSolve[{y'[x] == 3 y[x] (1 - y[x]), y[0] == .7}, y, {x, -10, 10}]];
Plot[solution[x], {x, -10, 10}, PlotRange -> {-1.5, 1.5}]
solution[4]
 
Ok great! That worked!

But what does the changing the y[x] to just y do? And I looked up the documentation on Flatten, but I'm not sure what that is doing in this case.

Also, would you happen to know how would I then find at what value of x will y equal a certain value?
 
IIRC, if you use y[x] then it gives the expression for y with x as the unknown. It is like writing in Mathematica
Code:
f[x] = 2 Sin[x] - x
When you just use y, then it gives a function, which you can apply to a value (like 4 or x). It is like writing
Code:
f[x_] := 2 Sin[x] - x
Notice the difference, in the first case, you would need to do something like "x = 4; f[x]" or "f[x] /. x -> 4" to evaluate f at 4, in the second case you can just do f[4] (even if x already has a value).

Flatten removes all nesting in a list. In this case, I had to use it because NDSolve gives back something like
Solution = {{ y -> stuff }}
and you need something of the form
y /. {y -> stuff}
So you can do either
y /. Flatten[Solution]
or (in this case equivalently)
y /. Solution[[1]]

(N)Solve doesn't really seem to work in this case, but you could try
FindRoot[solution[x] == 0.5, {x, 0}]
where 0.5 is the intersection point and 0 is your guess (the more accurate your guess, the more accurate the result, in general). Note that there are not many checks built in here, for example,
FindRoot[solution[x] == 3, {x, 0}]
will - despite some warnings - still give you a value of x. However, if you look at the plot, you will see that there is no solution. The reason is of course that the result it was considering when it decided to bail out of the numerical procedure is actually returned; the lesson is you should be extra careful.
 
Hi

can anyone have an idea how to evaluate the value of

Re[y[x]+z[x]] for x=5 say when

y[x] and z[x] satisfies a coupled differential equation which i solved using NDSolve as follows:

s=NDSolve[{y'[x]-z[x]-(0.5)y[x] y[x]==0,z'[x]-y[x]-(0.5)z[x] z[x]==0,y[0]==5 i,z[0]==5 i},
{y[x],z[x]},{x,0,10}]

NDSolve works, and then i can also plot Re[y[x]+z[x]] using

Plot[Evaluate[Re[y[x]+z[x]]]/.s]

But what I need is only the value of Re[y[x]+z[x]] at say x=5.

the method described in previous post doesn't work:now i have two functions y,z!
i have mathematica 6 only to work with.

Please help.

Bests
Santanu
 
You just need to do a second substitution, as follows:

s = Flatten[
NDSolve[{y'[x] - z[x] - (0.5) y[x] y[x] == 0,
z'[x] - y[x] - (0.5) z[x] z[x] == 0, y[0] == 5 I,
z[0] == 5 I}, {y[x], z[x]}, {x, 0, 10}]]

Re[y[x] + z[x]] /. s /. x -> 5

The answer came out -4.0271.
 
phyzguy said:
You just need to do a second substitution, as follows:

s = Flatten[
NDSolve[{y'[x] - z[x] - (0.5) y[x] y[x] == 0,
z'[x] - y[x] - (0.5) z[x] z[x] == 0, y[0] == 5 I,
z[0] == 5 I}, {y[x], z[x]}, {x, 0, 10}]]

Re[y[x] + z[x]] /. s /. x -> 5

The answer came out -4.0271.

thanks a lot dude...its working fine
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 0 ·
Replies
0
Views
833
  • · Replies 4 ·
Replies
4
Views
4K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
2K
Replies
2
Views
5K