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

I am trying to plot the following numerical solutions:Input:

  1. Aug 23, 2010 #1
    I am trying to plot the following numerical solutions:

    Input: NDSolve[{y''[x] + (2/x) y'[x] + 1 == 0, y[10^(-10)] == 0.9999999,
    y'[10^(-10)] == 0}, y, {x, 0, 30}]

    Output: {{y->InterpolatingFunction[{{3.34649*10^-105, 30.}},<>]}}

    But when I plot it, I just get a completely blank graph. Why is this?

    Does it have something to do with my step size?
     
  2. jcsd
  3. Aug 24, 2010 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    Re: Mathematica

    You should be careful with such results, in this case it gives a list with a list with a function. So the correct plot command would be something like

    Code (Text):

    NDSolve[{y''[x] + (2/x) y'[x] + 1 == 0, y[10^(-10)] == 0.9999999,
      y'[10^(-10)] == 0}, y, {x, 0, 30}]
    Plot[(y /. Flatten[%])[x], {x, 0, 30}]
     
    Also, I get a warning from NDSolve:
    Maximum number of 10000 steps reached at the point x == 3.3464887739011725`*^-105. >>
    which means that you probably can't really trust your solution after the point x = 0 :)

    [edit] You can check this, if you plot the result of the differential equation,
    Code (Text):
    Plot[D[(y /. Flatten[%%])[x], {x, 2}] + 2/x D[(y /. Flatten[%%])[x], x] + 1 /. x -> y, {y, 0, 10}]
    If you add PlotRange->All as an option, you see that around 0 you get deviations as large as 0.6[/edit]
     
  4. Aug 24, 2010 #3
    Re: Mathematica

    For starters, you can't plot starting from zero if you run the numerical integrator starting at the point 10^{-10}. Also, the plot function is limited to machine precision which I think is about 16 digits. How about I just do 5?

    mysol = NDSolve[{y''[x] + (2/x) y'[x] + 1 == 0,
    y[0.00001] == 0.9999999, y'[0.00001] == 0}, y, {x, 0.00001, 30}]
    Plot[y[x] /. mysol, {x, 0.00001, 30}]
     
  5. Aug 24, 2010 #4
    Re: Mathematica

    Thanks guys, I appreciate it.
     
  6. Aug 25, 2010 #5
    Re: Mathematica

    What if I have different numerical solutions and I wanted to plot them all on the same graph?

    Like y''(x) + (2/x)y'(x) + 1 = 0, y''(x) + (2/x)y'(x) + y(x) = 0, etc.?

    Thanks.
     
  7. Aug 26, 2010 #6
    Re: Mathematica

    Many functions in Mathematica are "listable" meaning you can supply lists as arguments enclosed in curly brackets:

    mysol = NDSolve[{y''[x] + (2/x) y'[x] + 1 == 0,
    u''[x] + 2/x u'[x] + u[x] == 0, y[0.00001] == 0.9999999,
    y'[0.00001] == 0, u[0.00001] == 2, u'[0.00001] == 1}, {y, u}, {x,
    0.00001, 30}]
    Plot[{y[x], u[x]} /. mysol, {x, 0.00001, 30},
    PlotRange -> {{0, 30}, {-20, 20}}]
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: I am trying to plot the following numerical solutions:Input:
  1. Numerical solution ODE (Replies: 10)

Loading...