Mathematica plotting with a slightly complicated relationship between variables

In summary, the conversation discusses the use of Mathematica to solve a set of differential equations with a constant value. The speaker was able to get a plot of the solution for a specific value of the constant using NDSolve, but encountered a problem when trying to plot the solution for an arbitrary value of the constant. This is because Mathematica needs to evaluate the function multiple times, which takes a long time. The speaker considers using diagnostic code and only evaluating a fixed list of values of the constant to speed up the process.
  • #1
McLaren Rulez
292
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

[tex]a'(t)=P*b(t)[/tex]
[tex]b'(t)=P^{2}*a(t)[/tex]

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
  • #2
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.
 

1. How can I plot a function with multiple variables in Mathematica?

To plot a function with multiple variables in Mathematica, you can use the Plot3D function. This function takes in the function expression, the range of values for each variable, and any other desired options such as labels or color schemes. You can also use the ContourPlot function to visualize the relationship between two variables while holding the others constant.

2. Can I use conditional statements in my plot in Mathematica?

Yes, Mathematica allows you to use conditional statements in your plot by using the ConditionalExpression function. This allows you to specify different functions or expressions to be plotted depending on certain conditions. You can also use Piecewise functions to define different functions for different ranges of variables.

3. How do I plot a 3D surface with non-numeric values in Mathematica?

To plot a 3D surface with non-numeric values, you can use the RegionPlot3D function. This function takes in a boolean expression and plots the region where the expression is true. You can also use RegionFunction to further specify the region to be plotted.

4. How can I add labels and legends to my plot in Mathematica?

Adding labels and legends to your plot in Mathematica can be done using the PlotLabels and PlotLegends options in the plot function. These options allow you to customize the labels and legends for your plot, including adding descriptive text, symbols, and colors.

5. Is it possible to animate my plot in Mathematica?

Yes, you can animate your plot in Mathematica using the Animate function. This function takes in a plot expression and allows you to specify the range of values for a variable that you want to animate. You can also use Manipulate to create interactive plots that can be manipulated in real-time by adjusting the values of variables.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
257
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
217
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
130
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
841
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top