Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica plotting with a slightly complicated relationship between variables

  1. Nov 18, 2012 #1
    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.
     
  2. jcsd
  3. Nov 18, 2012 #2
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook