1
$\begingroup$

I want to plot the solution of a differential equation which i solve it numerically with NDSolve. Here's the code:

A := 2.23818*^8;
Zh := 1*^11;
a := 1.496*^11;
M := 1.33*^20;
NDSolve[{(1 + (4 A t^2)/(r[t]^2 (r[t]^2 - Zh^2)) + 
   Zh^2/(r[t]^2 - Zh^2) + (4 A Zh^4 t^2)/(
   r[t]^2 (r[t]^2 - Zh^2)^3) + (8 A Zh^2 t^2)/(
   r[t]^2 (r[t]^2 - Zh^2)^2))*
 r'[t]^2 + ((-4 A t)/(r[t] (r[t]^2 - Zh^2)) - (4 A Zh^2 t)/(
   r[t] (r[t]^2 - Zh^2)^2)) r'[t] + A/(r[t]^2 - Zh^2) - (2 M)/
r[t] + M/a == 0, r[0] == 1.496*^11}, r, {t, 0, 3.15*^5}]

And mathematica answers:

{{r->InterpolatingFunction[{{0.,`, 315000.`}},<>]},{r->InterpolatingFunction[{{0.,`, 315000.`}},<>]}

I want to plot the answer as a function of "t". what is the code?

$\endgroup$

closed as off-topic by Michael E2, MarcoB, user9660, WReach, m_goldberg May 22 '16 at 0:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Michael E2, Community, WReach
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Plot[r[t] /. %, {t, 0, 3*10^5}] $\endgroup$ – user36273 May 21 '16 at 8:18
  • $\begingroup$ See the first two codes under "Basic Examples" of the docs for NDSolve. $\endgroup$ – Michael E2 May 21 '16 at 10:25
  • $\begingroup$ Possible duplicates: (63731), (66252). Also related: (108236) $\endgroup$ – Michael E2 May 21 '16 at 10:38
  • 1
    $\begingroup$ Possible duplicate of Plot multiple solutions $\endgroup$ – MarcoB May 21 '16 at 15:12
  • $\begingroup$ I don't think it is quite a duplicate. I think it has value to show how to take the output of NDsolve and make a function (or functions) that can be used in downstream processing. I would vote to retain this question and I find m_goldberg's answer particularly helpful. I like alternatives to replacing using the solution. $\endgroup$ – Jack LaVigne May 22 '16 at 14:23
2
$\begingroup$

Here is one way to do it.

With[{A = 2.23818*^8, Zh = 1*^11, a = 1.496*^11, M = 1.33*^20},
  {rLo, rHi} = 
    NDSolve[
      {(1 + 
         (4 A t^2)/(r[t]^2 (r[t]^2 - Zh^2)) + Zh^2/(r[t]^2 - Zh^2) + 
         (4 A Zh^4 t^2)/(r[t]^2 (r[t]^2 - Zh^2)^3) + 
         (8 A Zh^2 t^2)/(r[t]^2 (r[t]^2 - Zh^2)^2)) r'[t]^2 + 
        ((-4 A t)/(r[t] (r[t]^2 - Zh^2)) - (4 A Zh^2 t) /
          (r[t] (r[t]^2 - Zh^2)^2)) r'[t] + 
        A/(r[t]^2 - Zh^2) - (2 M)/r[t] + M/a == 0, 
       r[0] == 1.496*^11},
      r, {t, 0, 3.15*^5}][[All, 1, 2]]];

Plot[{rHi[t], rLo[t]}, {t, 0, 3.15*^5}]

plot

$\endgroup$
2
$\begingroup$

Update: If you use r[t] instead of r as the second argument of NDSolve you get the desired functions directly:

sol2 = NDSolve[{(1 + (4 A t^2)/(r[t]^2 (r[t]^2 - Zh^2)) +  Zh^2/(r[t]^2 - Zh^2) + 
       (4 A Zh^4 t^2)/(r[t]^2 (r[t]^2 - Zh^2)^3) + 
       (8 A Zh^2 t^2)/(r[t]^2 (r[t]^2 - Zh^2)^2))* r'[t]^2 + 
       ((-4 A t)/(r[t] (r[t]^2 - Zh^2)) - 
       (4 A Zh^2 t)/(r[t] (r[t]^2 - Zh^2)^2)) r'[t] + 
       A/(r[t]^2 - Zh^2) - (2 M)/r[t] + M/a == 0, r[0] == 1.496*^11}, 
    r[t], {t, 0, 3.15*^5}];

funcs2 = r[t] /. sol2

{InterpolatingFunction[{{0., 315000.}}, <>][t], InterpolatingFunction[{{0., 315000.}}, <>][t]}

So you can use funcs2 directly when plotting:

Plot[funcs2, {t, 0, 3.15*^5}]

Mathematica graphics

Original post:

ClearAll[r, t, A, Zh, a, M]
A = 2.23818*^8;
Zh = 1*^11;
a = 1.496*^11;
M = 1.33*^20;
sol = NDSolve[{(1 + (4 A t^2)/(r[t]^2 (r[t]^2 - Zh^2)) +  Zh^2/(r[t]^2 - Zh^2) + 
       (4 A Zh^4 t^2)/(r[t]^2 (r[t]^2 - Zh^2)^3) + 
       (8 A Zh^2 t^2)/(r[t]^2 (r[t]^2 - Zh^2)^2))* r'[t]^2 + 
       ((-4 A t)/(r[t] (r[t]^2 - Zh^2)) - 
       (4 A Zh^2 t)/(r[t] (r[t]^2 - Zh^2)^2)) r'[t] + 
       A/(r[t]^2 - Zh^2) - (2 M)/r[t] + M/a == 0, r[0] == 1.496*^11}, 
    r, {t, 0, 3.15*^5}];

funcs = r /. sol;

Plot[Evaluate[Through@funcs@t], {t, 0, 3.15*^5}]

Mathematica graphics

How it works:

funcs is a list of two pure functions:

funcs

{InterpolatingFunction[{{0., 315000.}}, <>], InterpolatingFunction[{{0., 315000.}},<>]}

funcs[t]

{InterpolatingFunction[{{0., 315000.}}, <>], InterpolatingFunction[{{0., 315000.}},<>]}[t]

We need to push the argument t inside the {..} to to get the functions we need to plot. This can be done in a number of ways:

r[t] /. sol

or

{funcs[[1]][t], funcs[[2]][t]}

or

#[t] & /@ funcs

or

Through[funcs[t]]

all give

{InterpolatingFunction[{{0., 315000.}}, <>][t], InterpolatingFunction[{{0., 315000.}}, <>][t]}

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.