Hi, there.
I am new to mathematica and recently I am intrigued by the convenience of
NDSolve function, since there is no need to write the Runge-kutta like
functions myself to solve ODEs. However, if what I want to solve is a list
of equations expressed in a loop way, I have no idea how to express it.
For example, a set of simple equations are:
Subscript[y, i]'[x] = Subscript[y, i][x] Cos[x + Subscript[y, i][x]] + i,
where i=1,2,...,N;
if N is small, of course I can write the equations separately, i.e.
Subscript[y,1]'[x] = Subscript[y,1][x] Cos[x + Subscript[y,1][x]] + 1
Subscript[y,2]'[x] = Subscript[y,2][x] Cos[x + Subscript[y,2][x]] + 2
...
Subscript[y,N]'[x] = Subscript[y,N][x] Cos[x + Subscript[y,N][x]] + N
But if N is large, this method seems clumsy, so I would like to express this
kind of "loop" equations in a neat way.
I have tried to nest "do" or "for" to control this loop, but the problem is
that they can not be incorporated into the NDSolve function. Does anyone
have a better solution?
Best regards and thanks in advance.
Haibo
Pillsy - 03 Jul 2009 10:35 GMT
> Hi, there.
> I am new to mathematica and recently I am intrigued by the convenience of
> NDSolve function, since there is no need to write the Runge-kutta like
> functions myself to solve ODEs. However, if what I want to solve is a lis=
t
> of equations expressed in a loop way, I have no idea how to express it.
> For example, a set of simple equations are:
> Subscript[y, i]'[x] = Subscript[y, i][x] Cos[x + Subscript[y, i][x]]=
+ i,
> where i=1,2,...,N;
This is almost certainly not what you want, because, like in a few
other languages, "=" is an assignment operatore, whereas "==" is the
test for equality. So instead you'd want
Subscript[y, i]'[x] == Subscript[y, i][x] Cos[x + Subscript[y, i][x]]
+ i
In order to create a list of these, for i rangeing from 1...n, you
want to use Mathematica's Table construct. This function will generate
a list of n equations, for a positive integer n:
eqns[n_Integer] /; Positive[n] :=
Table[Subscript[y, i]'[x] == Subscript[y, i][x] Cos[x + Subscript[y,
i][x]] + i, {i, n}];
HTH,
Pillsy