Re: NDSolve with arrays
- To: mathgroup at smc.vnet.net
- Subject: [mg89232] Re: NDSolve with arrays
- From: dh <dh at metrohm.ch>
- Date: Sun, 1 Jun 2008 03:33:47 -0400 (EDT)
- References: <g1m8ev$ia$1@smc.vnet.net>
Hi Svend,
Name the 10 functions you are looking for: f1..f10. Note that all 10
functions have the same derivatives. Further, from t=m to t=m+1, the
derivative is fm[x]. We therefore solve the equations from t=1 to t=2
using f1 for the derivatives. Then from t=2 to t=3 using f2 etc.
Therefore we have an outermost Do loop that iterates over starting times.
To solve, we first setup a list with function names:funs =
Table[Symbol["f" <> ToString[j]], {j, 1, n}]
Then we define the equations, remembering that in step m the derivatives
are given by fm:
eq1[j_]:={D[funs[[j]][x],x]==funs[[x0]][x],funs[[j]][x0]==(funs[[j]][x0]/.tes[[x0]])};
eqs=Flatten[Table[eq1[j],{j,1,n}]];
finally we solve ad append the soulution to the solution list: tes:
AppendTo[tes, NDSolve[eqs, funs, {x, x0, x0 + 1}][[1]]];
Finally we need to assemple the different pieces of the 10 functions
into Piecewise functions:
Table[Piecewise@Table[{(funs[[i]]/.Transpose[Drop[tes,1]][[i,j]])[x],x<j+1},{j,1,n-1}]
,{i,1,10}];
Here is the whole code:
n=10;
funs=Table[Symbol["f"<>ToString[j]],{j,1,n}];
tes={Table[funs[[j]][1]->j^2,{j,1,n}]};
Do[
eq1[j_]:={D[funs[[j]][x],x]==funs[[x0]][x],funs[[j]][x0]==(funs[[j]][x0]/.tes[[x0]])};
eqs=Flatten[Table[eq1[j],{j,1,n}]];
AppendTo[tes,NDSolve[eqs,funs,{x,x0,x0+1}][[1]]];
,{x0,1,10}];
res=Table[Piecewise@Table[{(funs[[i]]/.Transpose[Drop[tes,1]][[i,j]])[x],x<j+1},{j,1,n-1}]
,{i,1,10}];
Plot[res,{x,1,4}]
hope this helps, Daniel
svend wrote:
> hi all,
> i want to solve a PDE by discretizing in one variable in an array and solve. the equations are of the following type:
> n = 10;
> x0 = 1;
> x1 = 10;
>
> play[x_] := IntegerPart[x]
>
> eq1 := {D[f[j][x], x] == f[play[x]][x], f[j][x0] == j^2}
>
> eqs = Flatten[Table[eq1, {j, 1, n}]]
> fun = Flatten[Table[f[j], {j, 1, n}]];
> tes = NDSolve[eqs, fun, {x, x0, x1}][[1]];
>
> my problem is that the variable x, in which the equation is differential, also appears on the rhs in the "index" of my array. mathematica does not evaluate "play[x]" during NDSolve and just tells me that the rhs is not numerical. I tried "_NumericQ" and similar things but nothing seems. I would be very happy if someone could tell me how to resolve this.
>
> thanks
> svend
>
--
Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:dh at metrohm.com>
Internet:<http://www.metrohm.com>