Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7539 > unrolled thread

Rule replacement doesn't work after NDSolve?

Started bygac <g.crlsn@gmail.com>
First post2012-01-02 07:53 +0000
Last post2012-01-03 10:32 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Rule replacement doesn't work after NDSolve? gac <g.crlsn@gmail.com> - 2012-01-02 07:53 +0000
    Re: Rule replacement doesn't work after NDSolve? "Nasser M. Abbasi" <nma@12000.org> - 2012-01-03 10:29 +0000
    Re: Rule replacement doesn't work after NDSolve? A Retey <awnl@gmx-topmail.de> - 2012-01-03 10:32 +0000

#7539 — Rule replacement doesn't work after NDSolve?

Fromgac <g.crlsn@gmail.com>
Date2012-01-02 07:53 +0000
SubjectRule replacement doesn't work after NDSolve?
Message-ID<jdrnpb$93i$1@smc.vnet.net>
Can anyone explain why the three rule replacement steps after NDSolve don't work, but the three Plot3D commands do?  Thanks.

n = 2;
tf = 20;
varsx = Table[Subscript[x, i][z, t], {i, n}];
varsy = Table[y[z, t], {i, 1, 1}];

eqnsx = Table[{\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(
\(\*SubscriptBox[\(x\), \(i\)]\)[z, t]\)\) ==  y[z, t] \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(j = 1\), \(n\)]\(\((i - j)\)\ 
\(\*SubscriptBox[\(x\), \(j\)]\)[z, t]\)\), 
    Subscript[x, i][z, 0] == Sin[2 i Pi z]}, {i, n}];

eqnsy = Table[{\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(y[z, t]\)\) == - y[z, t] + 
      Exp[-(t - tf/4)^2], y[z, 0] == 0}, {j, 1, 1}];

vars = Join[varsx, varsy];
eqns = Join[eqnsx, eqnsy];

sol = NDSolve[eqns, vars, {t, 0, tf}, {z, 0, 10}, DependentVariables -> vars, MaxStepSize -> 0.01, MaxSteps -> 10^5, Method -> "ExplicitRungeKutta"][[1]]

Subscript[x, 1][0.1, 3] /. sol
Subscript[x, 2][0.1, 1] /. sol
y[0.5, 1] /. sol

Grid[{{Plot3D[y[z, t] /. sol, {t, 0, tf}, {z, 0, 1}, PlotLabel -> "y[z,t]", AxesLabel -> {"t", "z"}, PlotRange -> All, ImageSize -> 400],
Plot3D[Subscript[x, 1][z, t] /. sol, {t, 0, tf}, {z, 0, 1}, PlotLabel -> "\!\(\*SubscriptBox[\(x\), \(1\)]\)[z,t]]", AxesLabel -> {"t", "z"}, PlotRange -> All, ImageSize -> 400]}, 
{Plot3D[Subscript[x, 2][z, t] /. sol, {t, 0, tf}, {z, 0, 1}, PlotLabel -> "\!\(\*SubscriptBox[\(x\), \(2\)]\)[z,t]", AxesLabel -> {"t", "z"}, PlotRange -> All, ImageSize -> 400],}}]

[toc] | [next] | [standalone]


#7543

From"Nasser M. Abbasi" <nma@12000.org>
Date2012-01-03 10:29 +0000
Message-ID<jdul9m$jp9$1@smc.vnet.net>
In reply to#7539
On 1/2/2012 1:53 AM, gac wrote:
> Can anyone explain why the three rule replacement steps after NDSolve don't work,
>but the three Plot3D commands do?  Thanks.

Change

Subscript[x, 2][0.1, 1] /. sol

with

Subscript[x, 1] [z, t] /. sol

The plot commands work becuase you used the correct names there, i.e
Subscript[x, 1] [z, t] etc..

--Nasser


[toc] | [prev] | [next] | [standalone]


#7553

FromA Retey <awnl@gmx-topmail.de>
Date2012-01-03 10:32 +0000
Message-ID<jdulfe$jqi$1@smc.vnet.net>
In reply to#7539
Hi,

> Can anyone explain why the three rule replacement steps after NDSolve don't work, but the three Plot3D commands do?  Thanks.
>
> n = 2;
> tf = 20;
> varsx = Table[Subscript[x, i][z, t], {i, n}];
> varsy = Table[y[z, t], {i, 1, 1}];
>
> eqnsx = Table[{\!\(
> \*SubscriptBox[\(\[PartialD]\), \(t\)]\(
> \(\*SubscriptBox[\(x\), \(i\)]\)[z, t]\)\) ==  y[z, t] \!\(
> \*UnderoverscriptBox[\(\[Sum]\), \(j = 1\), \(n\)]\(\((i - j)\)\
> \(\*SubscriptBox[\(x\), \(j\)]\)[z, t]\)\),
>      Subscript[x, i][z, 0] == Sin[2 i Pi z]}, {i, n}];
>
> eqnsy = Table[{\!\(
> \*SubscriptBox[\(\[PartialD]\), \(t\)]\(y[z, t]\)\) == - y[z, t] +
>        Exp[-(t - tf/4)^2], y[z, 0] == 0}, {j, 1, 1}];
>
> vars = Join[varsx, varsy];
> eqns = Join[eqnsx, eqnsy];
>
> sol = NDSolve[eqns, vars, {t, 0, tf}, {z, 0, 10}, DependentVariables ->  vars, MaxStepSize ->  0.01, MaxSteps ->  10^5, Method ->  "ExplicitRungeKutta"][[1]]
>
> Subscript[x, 1][0.1, 3] /. sol
> Subscript[x, 2][0.1, 1] /. sol
> y[0.5, 1] /. sol

That's the order of evaluation, the LHS of the rules in sol just don't 
match the expressions you try to replace. You need e.g.:

y[z, t] /. sol /. {z -> 0.5, t -> 1}

> Grid[{{Plot3D[y[z, t] /. sol, {t, 0, tf}, {z, 0, 1}, PlotLabel ->  "y[z,t]", AxesLabel ->  {"t", "z"}, PlotRange ->  All, ImageSize ->  400],
> Plot3D[Subscript[x, 1][z, t] /. sol, {t, 0, tf}, {z, 0, 1}, PlotLabel ->  "\!\(\*SubscriptBox[\(x\), \(1\)]\)[z,t]]", AxesLabel ->  {"t", "z"}, PlotRange ->  All, ImageSize ->  400]},
> {Plot3D[Subscript[x, 2][z, t] /. sol, {t, 0, tf}, {z, 0, 1}, PlotLabel ->  "\!\(\*SubscriptBox[\(x\), \(2\)]\)[z,t]", AxesLabel ->  {"t", "z"}, PlotRange ->  All, ImageSize ->  400],}}]

to avoid such problems, I usually find it much better to define the 
dependent variables in NDSolve without arguments, like here:

sol = NDSolve[eqns, Head /@ vars, {t, 0, tf}, {z, 0, 10},
    MaxStepSize -> 0.01, MaxSteps -> 10^5,
    Method -> "ExplicitRungeKutta"][[1]]

then the rest of your original code will work just fine. Is there a 
special reason why you use DependentVariables here? It seems to make no 
sense when you do return all of them anyway...

hth,

albert

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web