Using NDSolve in Mathematica to Solve ODEs and Find Function Values

  • Thread starter zeebo17
  • Start date
  • Tags
    Mathematica
In summary, to evaluate the value of Re[y[x]+z[x]] at a certain value of x, you can use the following steps after using NDSolve to solve for y[x] and z[x]:1. Flatten the solution obtained from NDSolve.2. Substitute the desired value of x into the expression Re[y[x]+z[x]].
  • #1
zeebo17
41
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
  • #2
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]
 
  • #3
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?
 
  • #4
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.
 
  • #5
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
 
  • #6
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.
 
  • #7
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
 

What is NDSolve in Mathematica?

NDSolve is a function in the Mathematica software that is used to numerically solve differential equations. It is commonly used in scientific and mathematical research to model and simulate systems that involve differential equations.

How do I use NDSolve in Mathematica?

To use NDSolve in Mathematica, you need to first define the differential equation you want to solve, along with any initial or boundary conditions. Then, you can use the NDSolve function with the appropriate syntax to solve the equation and obtain a numerical solution.

What types of differential equations can NDSolve handle?

NDSolve can handle a wide range of differential equations, including ordinary differential equations (ODEs), partial differential equations (PDEs), and delay differential equations (DDEs). It can also handle systems of equations and equations with discontinuities.

Can I customize the NDSolve function in Mathematica?

Yes, you can customize the NDSolve function in Mathematica by specifying options such as the method used for solving the equation, the precision of the solution, and the maximum number of steps. You can also specify events that trigger during the solving process.

What are the advantages of using NDSolve in Mathematica?

NDSolve in Mathematica offers several advantages, including the ability to handle a wide range of differential equations, customizable options for solving equations, and the ability to visualize and analyze the solutions. It also has a user-friendly interface and is widely used and supported by the scientific and mathematical community.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
117
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
1
Views
1K
Replies
7
Views
2K
Replies
6
Views
122
  • Differential Equations
Replies
4
Views
2K
  • Differential Equations
Replies
1
Views
656
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
246
  • Differential Equations
Replies
3
Views
1K
Back
Top