Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NDSolve to solve a set of "loop" equations

13 views
Skip to first unread message

Haibo Min

unread,
Jul 2, 2009, 7:14:53 AM7/2/09
to
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

unread,
Jul 3, 2009, 5:35:57 AM7/3/09
to
On Jul 2, 7:14 am, Haibo Min <yshdf...@gmail.com> wrote:
> 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

Haibo Min

unread,
Jul 4, 2009, 6:37:26 AM7/4/09
to
Thanks Pillsy, a really elegant express!

Best regards,
haibo

On Fri, Jul 3, 2009 at 5:39 PM, Pillsy <pill...@gmail.com> wrote:

> On Jul 2, 7:14 am, Haibo Min <yshdf...@gmail.com> wrote:

> > 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
>
>

0 new messages