Re: Rule replacement doesn't work after NDSolve?
- To: mathgroup at smc.vnet.net
- Subject: [mg123978] Re: Rule replacement doesn't work after NDSolve?
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Tue, 3 Jan 2012 05:29:26 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201201020748.CAA09253@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
sol involves replacing z and t:
sol
{Subscript[x,
1][z,t]->InterpolatingFunction[{{0.,20.},{0.,10.}},<>][t,z],Subscript[x,
2][z,t]->InterpolatingFunction[{{0.,20.},{0.,10.}},<>][t,z],y[z,t]->InterpolatingFunction[{{0.,20.},{0.,10.}},<>][t,z]}
The Plot arguments are Subscript[x, 1][z, t] /. sol and Subscript[x, 2][z,
t] /. sol, as they should be, with z and t available for replacement.
But your "three rule replacement steps after NDSolve" don't have z or t:
Subscript[x, 1][0.1, 3] /. sol
Subscript[x, 2][0.1, 1] /. sol
y[0.5, 1] /. sol
so they don't do what you intend.
You could do this instead:
Subscript[x, 1][z, t] /. sol /. {z -> 0.1, t -> 3}
Subscript[x, 2][z, t] /. sol /. {z -> 0.1, t -> 3}
y[z, t] /. sol /. {z -> 0.5, t -> 1}
0.587097
0.951482
1.22233*10^-8
All becomes far simpler if you define solution functions rather than using
replacement every time.
I eliminate Subscript, fancy PartialD, and fancy Sum as well (they're a
needless bother), but use them if you think you MUST.
Clear[x, y, z, t]
n = 2;
tf = 20;
functions = Flatten@{Array[x, {2}], y}
eqnsx = Table[{D[x[i][z, t], t] ==
y[z, t] Sum[(i - j) x[i][z, t], {j, n}],
x[i][z, 0] == Sin[2 i Pi z]}, {i, n}];
eqnsy = Table[{D[y[z, t], t] == - y[z, t] +
Exp[-(t - tf/4)^2], y[z, 0] == 0}, {j, 1, 1}];
eqns = Flatten@{eqnsx, eqnsy};
{x[1], x[2], y} =
functions /.
NDSolve[eqns, vars, {t, 0, tf}, {z, 0, 10},
DependentVariables -> vars, MaxStepSize -> 0.01,
MaxSteps -> 10^5, Method -> "ExplicitRungeKutta"][[1]];
x[1][0.1, 3]
x[2][0.1, 1]
y[0.5, 1]
{x[1], x[2], y}
0.58736
0.951057
1.22233*10^-8
Grid[{{Plot3D[y[z, t] /. sol, {t, 0, tf}, {z, 0, 1},
PlotLabel -> HoldForm[y[z, t]], AxesLabel -> {t, z},
PlotRange -> All, ImageSize -> 400],
Plot3D[x[1][z, t] /. sol, {t, 0, tf}, {z, 0, 1},
PlotLabel -> Subscript[x, 1][z, t], AxesLabel -> {t, z},
PlotRange -> All, ImageSize -> 400]},
{Plot3D[x[2][z, t] /. sol, {t, 0, tf}, {z, 0, 1},
PlotLabel -> Subscript[x, 2][z, t], AxesLabel -> {t, z},
PlotRange -> All, ImageSize -> 400], Null}}]
I left subscripts in the Plot, but in the real world I probably wouldn't.
Bobby
On Mon, 02 Jan 2012 01:48:20 -0600, gac <g.crlsn at gmail.com> wrote:
> 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],}}]
>
--
DrMajorBob at yahoo.com
- References:
- Rule replacement doesn't work after NDSolve?
- From: gac <g.crlsn@gmail.com>
- Rule replacement doesn't work after NDSolve?