0
$\begingroup$

This question is a continuation of this.

As answered by J.M., I am able to iterate a solution. However, I am unable to plot it. I am sure that this has to do with my inability to understand how the plot routine works and I can't seem to figure it out:

state = First[
  NDSolve`ProcessEquations[{D[u[t, x], t] == D[u[t, x], x, x], 
    u[0, x] == 0, u[t, 0] == Sin[t], u[t, 5] == 0}, u, t, {x, 0, 5}]]

NDSolve`Iterate[state, 5]
NDSolve`ProcessSolutions[state, "Forward"]

Plot3D[
 Evaluate[u[t, x] /. NDSolve`ProcessSolutions[state, "Forward"]],
 {t,0,2},
 {x, 0, 5}
 ]

gives me a blank plot with no curves. What am I doing wrong? I can't omit {t, 0, 2} as that would give me an error.

$\endgroup$
  • $\begingroup$ have a look at this: reference.wolfram.com/mathematica/tutorial/… $\endgroup$ – acl May 22 '12 at 15:23
  • $\begingroup$ @acl I did have a look at that. When I replaced the u[t,x,y] equation with u[t,x], all I got was blank plots. $\endgroup$ – dearN May 22 '12 at 15:24
2
$\begingroup$

If you look at the interpolation object that's returned, it's not a function u(t,x) only u(x) with fixed t. In fact, it returns both u[x] and u'[x] for fixed t. So try

state = First[
NDSolve`ProcessEquations[{D[u[t, x], t] == D[u[t, x], x, x], 
u[0, x] == 0, u[t, 0] == Sin[t], u[t, 5] == 0}, u, t, {x, 0, 5}]]

Table[NDSolve`Iterate[state, t];
Plot[NDSolve`ProcessSolutions[state, "Forward"][[All, 2]], {x, 0, 5},
PlotRange -> 1], {t, 0, 5}]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks for making the distinction between u[t,x] and u[x]. It wasn't obvious to me! :) $\endgroup$ – dearN May 23 '12 at 14:35
1
$\begingroup$

Something like this?

L = -10;
state = First[
   NDSolve`ProcessEquations[{D[u[t, x], 
       t] == + D[u[t, x], x, x] - Sin[u[t, x]],
     u[0, x] == Exp[-(x^2)],
     u[t, -L] == u[t, L]}, u, t, {x, -L, L}, 
    Method -> {"MethodOfLines", 
      "SpatialDiscretization" -> {"TensorProductGrid", 
        "DifferenceOrder" -> "Pseudospectral"}}]];
GraphicsGrid[Partition[Table[
   NDSolve`Iterate[state, tau ];

   Plot[Evaluate[
     u[tau, x] /. 
      NDSolve`ProcessSolutions[state, "Forward"]], {x, -L, L}, 
    PlotRange -> {-1/4, 1/4}],
   {tau, 0., 3., .5}], 2]]

Mathematica graphics

$\endgroup$
  • $\begingroup$ You know, I'll have to check my [ and see if all is well with my code. $\endgroup$ – dearN May 22 '12 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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