Using NDSolve in Mathematica to Solve ODEs and Find Function Values

  • Context: Undergrad 
  • Thread starter Thread starter zeebo17
  • Start date Start date
  • Tags Tags
    Mathematica
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 12K views
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