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

  • Context: Graduate 
  • Thread starter Thread starter Void123
  • Start date Start date
  • Tags Tags
    Numerical Plot
Click For Summary

Discussion Overview

The discussion revolves around plotting numerical solutions to differential equations using NDSolve in Mathematica. Participants explore issues related to plotting results, step sizes, and how to handle multiple equations in a single graph.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant reports getting a blank graph when attempting to plot the output from NDSolve and questions if the issue relates to step size.
  • Another participant suggests that the output from NDSolve is a list containing a function, and provides an alternative plot command to correctly visualize the results.
  • A warning from NDSolve about reaching the maximum number of steps is mentioned, indicating potential unreliability of the solution near x = 0.
  • A different approach is proposed by another participant, suggesting starting the numerical integration from a small positive value instead of zero to avoid plotting issues.
  • One participant expresses interest in plotting multiple numerical solutions on the same graph, raising a question about how to do so effectively.
  • Another participant responds by explaining that many functions in Mathematica can accept lists as arguments, providing an example of how to plot multiple solutions simultaneously.

Areas of Agreement / Disagreement

Participants generally agree on the challenges of plotting numerical solutions starting from zero and the need for careful handling of the output from NDSolve. However, there are multiple approaches suggested for resolving the plotting issues, indicating that no single solution is universally accepted.

Contextual Notes

Limitations include potential inaccuracies in solutions obtained near singular points and the dependence on numerical precision settings in Mathematica.

Void123
Messages
138
Reaction score
0
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?
 
Physics news on Phys.org


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:
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:
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]
 


Void123 said:
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}]
 


Thanks guys, I appreciate it.
 


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.
 


Void123 said:
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}}]
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
964
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 24 ·
Replies
24
Views
5K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K