2
$\begingroup$

I would like to use NDSolve to evaluate a PDE involving a vector valued function. For instance, I'm looking for something like a heat equation with vector-valued boundary conditions:

NDSolve[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == {1,0}, 
    u[t, 0] == {Cos[t],Sin[t]}, u[t, 5] == {1,0}}, u, {t, 0, 10}, {x, 0, 5}]

When I try this I get several error messages:

Thread::tdlen: Objects of unequal length in {0,0} {1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.} cannot be combined.

NDSolve::vlen: "The vector {0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.} and the weight vector {0.,0.} are of unequal lengths. "

NDSolve::ndnum: Encountered non-numerical value for a derivative at t == 0.`

How do I get around this? I'd like to avoid using different equations for each component function of $u$.

Apologies if I'm missing something simple—I'm still a beginner and I've searched for a solution for some time without success.

$\endgroup$
3
$\begingroup$

Just add this one line

u[t_, x_] := {u1[t, x], u2[t, x]};

This tells M all u's in the code are vectors:

eq = {D[u[t, x], t] == D[u[t, x], {x, 2}], u[0, x] == {1, 0}, 
    u[t, 0] == {Cos[t], Sin[t]}, u[t, 5] == {1, 0}};
NDSolve[eq, u[t, x], {t, 0, 10}, {x, 0, 5}]

Mathematica graphics

Update: For plotting, might be easier to use {u1,u2} instead of {u1[t,x],u2[t,x]}. Hence the above becomes

u[t_, x_] := {u1[t, x], u2[t, x]};
eq = {D[u[t, x], t] == D[u[t, x], {x, 2}], u[0, x] == {1, 0}, 
    u[t, 0] == {Cos[t], Sin[t]}, u[t, 5] == {1, 0}};
sol = First@NDSolve[eq, {u1, u2}, {t, 0, 10}, {x, 0, 5}]

Not can do things like

Plot[{(u1 /. sol)[t, 0], (u2 /. sol)[t, 0]}, {t, 0, 2 Pi}, PlotTheme -> "Detailed"]

Mathematica graphics

 ParametricPlot[{(u1 /. sol)[t, 0], (u2 /. sol)[t, 0]}, {t, 0, 2 Pi}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ That works perfectly! Thanks a ton, Nasser. $\endgroup$ – Nick Strehlke Nov 30 '14 at 7:32
  • $\begingroup$ I didn't include this in the question, but if there's a simple answer, would you mind letting me know how to plot the solution parametrically in $t$ for fixed $x$? The naive ParametricPlot[u[t,0] /. heat, {t, 0, 2 Pi}] doesn't seem to do it. I should have put this in the question as it's my ultimate objective (and I can open another question), but I thought it would follow easily. $\endgroup$ – Nick Strehlke Nov 30 '14 at 8:47
  • $\begingroup$ @NickStrehlke added plot. Not sure if this is what you are looking for. $\endgroup$ – Nasser Nov 30 '14 at 9:10
  • $\begingroup$ That's right along the lines of what I was looking for. Thanks again for your help! $\endgroup$ – Nick Strehlke Nov 30 '14 at 18:21

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.