Mathematica plotting with a slightly complicated relationship between variables

Click For Summary
SUMMARY

This discussion focuses on optimizing the plotting of solutions to a set of differential equations in Mathematica, specifically using the NDSolve function. The user successfully implemented a function s[P_] to solve the equations for varying values of P but encountered significant slowdowns when plotting a(t) against P for multiple t values. The community suggests that the slow performance is due to Mathematica's repeated evaluations of NDSolve for each point, and recommends using a fixed list of P values to reduce computation time, potentially employing ListPlot for efficiency. For 3D plotting, a limited number of NDSolve evaluations is advised to manage performance.

PREREQUISITES
  • Understanding of differential equations and their solutions
  • Familiarity with Mathematica's NDSolve function
  • Knowledge of plotting functions in Mathematica, such as Plot and ListPlot
  • Basic concepts of performance optimization in computational tasks
NEXT STEPS
  • Explore Mathematica's ListPlot for efficient data visualization
  • Learn about performance optimization techniques in Mathematica
  • Investigate the use of fixed lists for parameter evaluation in NDSolve
  • Study methods for creating 3D plots in Mathematica, focusing on performance
USEFUL FOR

Mathematica users, mathematicians, and researchers working with differential equations who seek to optimize plotting performance and understand the implications of parameter evaluations on computation time.

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 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K