Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

NDSolve in mathematica

  1. Jun 10, 2009 #1
    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}]
     
  2. jcsd
  3. Jun 11, 2009 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    I suggest replacing y[x] in the second argument of NDSolve by y. Then y /. Solution is really a function, e.g.
    Code (Text):

    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 (Text):

    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]
     
     
  4. Jun 11, 2009 #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?
     
  5. Jun 11, 2009 #4

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    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 (Text):
    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 (Text):
    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.
     
  6. Jun 30, 2010 #5
    Hi

    can any one 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
     
  7. Jun 30, 2010 #6

    phyzguy

    User Avatar
    Science Advisor

    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.
     
  8. Jun 30, 2010 #7
    thanks a lot dude...its working fine
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook