View Full Version : Repeated differential equations
Andreas_D
Sep23-09, 03:35 AM
Hi,
I'm trying to make Mathematica calculate a system of differential equations
(initial value problem) in a FOR loop. This looks quite difficult, it seems
impossible to evaluate and plot in a "FOR" loop.
What I try to do is
- give initial parameters
- FOR (...number of repetitions)
- ndsolve differential equation
- return parameters -> new initial parameters
- continue with "FOR" loop
Thank you for any ideas!
Andreas
lurflurf
Sep23-09, 06:08 AM
That is confusing, could you be more specific. In mathematica outpit is supressed inside interation structures like for,while,and do. You can force output using
Print[expr]
Andreas_D
Sep23-09, 07:58 AM
Maybe let's forget about the differential equations for a moment.
It would be helpful to find out how to do a repeated calculation - meaning an iterative one...
I'm used to other languages and I tried something like that - but there is no way in Mathematica:
a_out = 0; # initial value of some parameter
i = 0;
f[x] = x^2 + a_out # some function
For [i<10; i++] # this is how i would write it in C
{
Plot [f[x],{x,0,1}
a_out = f[1] # return calculated value to replace initial parameter
}
so there should be 9 plots of the function f with different parameters a_out. but nothing happens :-(
DaleSpam
Sep23-09, 01:50 PM
Of course there is a way to do that in Mathematica. You just need to learn the syntax correctly.
First, the underscore character is a reserved character used for patterns, so a_out is not a symbol named "a_out", it is a pattern named "a" representing any single object of type "out". Try "aout" instead.
Second, you want to define a function correctly.
f[x] = x^2 + aout
Would immediately evaluate x^2 + aout to get x^2 and would set the symbol f[x] equal to that. This means that changes to aout would have no impact and that f[x] would evaluate to x^2 but f[2] would evaluate to f[2] instead of 4. The correct way to define a function is
f[x_]:= x^2 + aout
or even
f = Function[x, x^2 + aout]
Third, you have the syntax of For completely wrong for Mathematica. Just look up the help entry for For.
Fourth, remember that For evaluates to Null, so you will need to Print[Plot[...]] to get the output you want.
Andreas_D
Sep24-09, 05:19 AM
Hi,
I was quite aware that I'm mixing the languages - in order to find out the mistake in my thinking.
The crucial point was the Print[Plot...] order. Now it works out properly to plot a series of functions with this code:
aout = 0;
For [i = 0, i < 4, i++,
{
f[x_] = x^2 + aout;
Print[Plot[f[x], {x, 0, 1} ]];
aout = f[1]
}
]
Thanks a lot, now I can think about the differential equations :biggrin:
I'd do something like said above, with parameters X1,X2,X3
SOLN[X1_, X2_, X3_ ] := NDSolve[{system[X1,X2,X3]}]
X1=0;
X2=0;
X3=0;
For [i = 0, i < 4, i++,
{
SOLNF=SOLN[X1,X2,X3];
X1=New something [SOLNF]
X2=New something [SOLNF]
X3=New something [SOLNF]
}
]
that way, the function isn't redefined every loop, but it is reevaluated everytime you call it, with whatever parameters X1,X2,X3 happen to be at the time of the call.
Andreas_D
Oct5-09, 09:56 AM
Thanks, I did that and I think my calculation is faster now... and, in any case, it looks better!
vBulletin® v3.7.6, Copyright ©2000-2009, Jelsoft Enterprises Ltd.