Mathematica Mathematica plotting with a slightly complicated relationship between variables

AI Thread Summary
The discussion revolves around the challenges of solving a set of differential equations in Mathematica and the performance issues encountered when plotting results for varying parameters. The user successfully solved the equations for a specific constant value of P and created plots, but when attempting to generalize the solution to an arbitrary P, the computation time increased significantly, taking two to three minutes for each value of P. This slowdown is attributed to Mathematica needing to re-evaluate the entire NDSolve for each input, leading to inefficiencies. Suggestions include limiting the number of evaluations by predefining a fixed list of P values and using ListPlot to connect points, which could reduce computation time. For creating a 3D plot, a similar approach of using a manageable number of NDSolve evaluations is recommended to make the task feasible.
McLaren Rulez
Messages
289
Reaction score
3
Hi,

So here's my latest problem as I get to grips with Mathematica. I will give a simpler version of the one I am actually doing.

I started with a closed set of differential equations. Consider the following equations

a'(t)=P*b(t)
b'(t)=P^{2}*a(t)

with known initial conditions and P is a constant. I solved it for a specific value of P using s=NDSolve[...] with the equations and initial conditions inside the NDSolve argument. I can get a plot of a(t) against t using Plot[Evaluate[a(t)/.s], {t, 0, Infinity}]. So far, so good. Now, I want to get a feel for how the system behaves for an arbitrary P. I tried to upgrade s to a function that takes in argument p. That is, s[P_]=NDSolve[...] and this works as well. So now, I can input s[5] and get the solution of my DEs for P=5, say. Note that now, I have to use Plot[Evaluate[a(t)/.s[5], {t, 0, Infinity}] to get my plot of a(t) against t.

Similarly, I can get a plot of a[t] against P for a given t. That is, Plot[Evaluate[a(1)/.s[P], {P, 0, Infinity}] works too. But for some reason, this takes a very long time. It takes a good two or three minutes for each value of t. Why is it so slow?

I eventually want to get a 3D plot of a(t) against t and P. I'm not sure how to go about it and even if I did, if it takes two or three minutes for one value of t, it will never be able to do a 3D plot of all t and all P, will it?

Thank you for your help.
 
Physics news on Phys.org
McLaren Rulez said:
Why is it so slow?

You have asked Mathematica to give you a detailed accurate plot where p ranges to infinity. To do that it needs to evaluate the function lots of places. Every evaluation requires doing a complete NDSolve again. You can put in a bit of diagnostic code that will tell you where or how many times NDSolve is being evaluated to verify this.

Mathematica has no idea that your function might be relatively smooth and only doing a handful of NDSolve might be enough. Perhaps you know enough that you could tell Mathematica to only do NDSolve for a fixed list of values of p and then do a ListPlot and connect those points together. Perhaps that would let you accomplish this with a dozen or two NDSolve.

Then when you want to do 3D perhaps you could accomplish that with a hundred or two NDSolve.
 

Similar threads

Replies
1
Views
537
Replies
1
Views
2K
Replies
2
Views
2K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Back
Top