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
Click For Summary

Discussion Overview

The discussion revolves around using NDSolve in Mathematica to solve ordinary differential equations (ODEs) and subsequently evaluate the function values at specific points. Participants explore various methods for extracting function values and discuss the implications of different approaches within the context of Mathematica's syntax and functionality.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant inquires about how to extract the value of a function y at specific x values after solving an ODE using NDSolve.
  • Another participant suggests modifying the NDSolve command by replacing y[x] with y to facilitate function evaluation.
  • There is a discussion about the use of Flatten in the context of the output from NDSolve, with explanations provided about its necessity for accessing the function correctly.
  • A participant asks how to find the x value for which y equals a certain value, leading to a suggestion to use FindRoot for numerical solutions.
  • Another participant presents a new problem involving coupled differential equations and seeks help in evaluating the real part of the sum of two functions at a specific x value.
  • A later reply provides a solution to the new problem, demonstrating how to apply a second substitution to extract the desired value from the NDSolve output.

Areas of Agreement / Disagreement

Participants generally agree on the methods to extract function values from NDSolve outputs, but there are variations in approaches and some uncertainty about the implications of different syntactical choices. The discussion remains open regarding the best practices for finding specific function values and handling multiple functions.

Contextual Notes

Some participants express limitations in their understanding of certain Mathematica functions, such as Flatten, and how they apply in specific contexts. There is also mention of potential pitfalls when using numerical methods like FindRoot, indicating a need for careful consideration of initial guesses and function behavior.

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
3K
  • · Replies 24 ·
Replies
24
Views
4K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
1
Views
2K