View Full Version : Mathematica
Void123
Aug23-10, 11:34 PM
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?
CompuChip
Aug24-10, 04:20 AM
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
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 :)
You can check this, if you plot the result of the differential equation,
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
jackmell
Aug24-10, 06:57 AM
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?
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}]
Void123
Aug24-10, 10:58 AM
Thanks guys, I appreciate it.
Void123
Aug25-10, 06:28 PM
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.
jackmell
Aug26-10, 06:49 AM
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.
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}}]
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.