2
$\begingroup$

How can I vary an initial condition in the numerical solution of a system of ODEs and then make a 3D plot of the solution space with that condition as one of the variables.

sol = NDSolve[
  {y''[t] + y'[t] + 4*y[t] + x[t] - x'[t] == 0, 
   x'[t] + 3*c*y[t] == 0,
   y[1] == 1, y'[1] == 1, 
   x[0] == 1}, {x, y}, {t, 0, 10}]

I want to vary one of the condition variables, say y[1] over u = Range[-10, 10, step]. Then I want to make a 3D plot of the solution space (x[t[, y[t], y[1][u]).

Can anyone please guide me in solving this query.

$\endgroup$
8
  • $\begingroup$ Your equation can be solved analytically, so you can just exchange y[1]==1 with y[1]==z and DSolve the equation and Plot3D[x[t] /. sol, {t, -1, 1}, {z, -1, 1}]. $\endgroup$
    – xzczd
    Mar 7, 2013 at 8:09
  • $\begingroup$ @xzczd In fact, the equation in my problem involves multipliers in terms of time, I have put the simplest form. I need a way to solve numerically. $\endgroup$ Mar 7, 2013 at 8:44
  • 3
    $\begingroup$ @MuhammadZubair, have a look at ParametricNDSolve and see if that helps. $\endgroup$
    – user21
    Mar 7, 2013 at 9:00
  • $\begingroup$ @ruebenko Wow, I think I'd better upgrade to v9 quickly. $\endgroup$
    – xzczd
    Mar 7, 2013 at 9:06
  • $\begingroup$ @xzczd, if you need a parametric version of NDSolve this is it; automatic sensitivity computation. It's pretty cool, I think. Also a much better Event language and better DAE solving capabilities, are just a few highlights of V9 NDSolve. Hope that's enough of a teaser... $\endgroup$
    – user21
    Mar 7, 2013 at 10:20

2 Answers 2

1
$\begingroup$

Playing with @ruebenko's suggestion,

c = 1
pf = ParametricNDSolveValue[
  {y''[t] + y'[t] + 4*y[t] + x[t] - x'[t] == 0,
   x'[t] + 3*c*y[t] == 0, y[1] == 1,
   y'[1] == u,
   x[0] == 1}, y, {t, 0, 10}, {u \[Element] Reals}]

Plot3D[pf[u][t], {u, -10, 10}, {t, 0, 10}, PlotRange -> All]

Mathematica graphics

$\endgroup$
3
$\begingroup$

ruebenko has pointed out the best way to solve this in the comment above: use ParametricNDSolve in version 9. Then, I'd like to post my clumsy solution with version 8 since I've already finished it…:

c = 1;
zmin = -1; zmax = 1; n = 25;
tmin = -1; tmax = 1;
ex = 
 Table[Join[{z}, #] & /@ 
       Transpose[{First[(x /. First@#)["Coordinates"]], (x /. First@#)["ValuesOnGrid"]}] &@
       NDSolve[{y''[t] + y'[t] + 4 y[t] + x[t] - x'[t] == 0, 
                x'[t] + 3 c y[t] == 0, y[1] == z, y'[1] == 1, x[0] == 1}, 
               {x, y}, {t, tmin, tmax}], 
       {z, zmin, zmax, (zmax - zmin)/n}];

ListPlot3D[Flatten[ex, 1]]

Mathematica graphics

Just for your sample, it can be solved analytically, too:

c = 1;
sol = DSolve[{y''[t] + y'[t] + 4 y[t] + x[t] - x'[t] == 0, 
              x'[t] + 3 c y[t] == 0, y[1] == z, y'[1] == 1, x[0] == 1}, 
             {x, y}, {t}];
Plot3D[x[t] /. sol, {z, -1, 1}, {t, -1, 1}]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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