Message Boards Message Boards

[?] Plot solutions obtained with NDSolve?

Posted 7 years ago

Hello, I am trying to plot the functions generated by the NDSolve command. I am using the code:

sol = NDSolve[{Derivative[1][an][t] ==  an[t]*Re[lambda[x, t]] +  0.5*Re[H[x, x/2, t]]*(an2[t]^2 - bn2[t]^2) + 
0.5*Im[H[x, x/2, t]]*(an2[t]*bn2[t] + an2[t]*bn2[t]),  Derivative[1][an2][ t] == (Re[lambda[x/2, t]] + Cc[x, t]*an[t])*
an2[t] +(Im[lambda[x/2, t]] + Dd[x, t]*an[t])*bn2[t], Derivative[1][bn2][ t] == (Re[lambda[x/2, t]] - Cc[x, t]*an[t])*
bn2[t] +(-Im[lambda[x/2, t]] + Dd[x, t]*an[t])*an2[t], an[.0] == 1/4000, an2[.0] == 1/5000, bn2[.0] == 1/5000},
{an[t], an2[t], bn2[t]}, {t, .0, 2*tzero}, AccuracyGoal -> 1000]
The command itself works just fine. It returns 3 functions for an[t], an2[t] and bn2[t]. Then I tryed to use the command:

Plot[Evaluate[an[t] /. sol], {t, 0, tzero}, PlotRange -> All]
Plot[Evaluate[an'[t] /. sol], {t, 0, tzero}, PlotRange -> All]

 The first one works, I got the plot of an[t] versus t. But the second one does not work. I got a blank plot. Can't figure out why it is not working. Can someone help me?

Thank you.
POSTED BY: Pedro Henrique
2 Replies
Posted 7 years ago
  Thank you Gianluca, now I understood the problem :).
POSTED BY: Pedro Henrique

It is a syntax pitfall. Try with

Plot[Evaluate[D[an[t] /. sol, t]], {t, 0, tzero}, PlotRange -> All]

Since you chose to solve for an[t], the replacement an'[t] /. sol will not work, because FullForm[an'[t]] is Derivative[1][an][t], which does not contain an[t]. It would work if you had solved for an, without explicit t. Compare the syntax:

NDSolve[{an'[t] == an[t], an[0] == 1}, an[t], {t, 0, 1}]
NDSolve[{an'[t] == an[t], an[0] == 1}, an, {t, 0, 1}]

You may consider using NDSolveValue instead of NDSolve.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract