Repeated differential equations

AI Thread Summary
The discussion revolves around using Mathematica to solve a system of differential equations within a FOR loop, addressing the challenges of output suppression in iterative structures. The initial poster, Andreas, seeks to perform repeated calculations and plot results, drawing comparisons to syntax from other programming languages like C. Key points include the importance of correct syntax in Mathematica, such as using the underscore character appropriately and defining functions properly. A significant breakthrough occurs when Andreas learns to use Print[Plot[...]] to display outputs within the loop. After resolving initial issues, he successfully implements a loop to plot functions with varying parameters. He then transitions to applying similar logic to solve differential equations, emphasizing that the function should be reevaluated with updated parameters rather than redefined in each iteration. The conversation highlights the learning curve associated with Mathematica's syntax and functionality for iterative computations.
Andreas_D
Messages
4
Reaction score
0
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
 
Physics news on Phys.org
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]
 
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 :-(
 
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.
 
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.
 
Thanks, I did that and I think my calculation is faster now... and, in any case, it looks better!
 

Similar threads

Replies
1
Views
2K
Replies
4
Views
3K
Replies
3
Views
2K
Replies
4
Views
3K
Replies
5
Views
14K
Back
Top