Repeated differential equations

Click For Summary

Discussion Overview

The discussion revolves around implementing a system of differential equations in Mathematica, specifically focusing on using iterative loops to solve initial value problems. Participants explore syntax issues, function definitions, and plotting results within loops.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses difficulty in using a FOR loop to calculate and plot a system of differential equations in Mathematica, seeking guidance on the correct approach.
  • Another participant points out that output in Mathematica is suppressed within iteration structures and suggests using Print to force output.
  • A participant proposes shifting the focus from differential equations to iterative calculations, sharing a code snippet that illustrates their confusion with Mathematica's syntax compared to other programming languages.
  • One participant provides corrections regarding the use of reserved characters and function definitions in Mathematica, emphasizing the need for proper syntax in defining functions and loops.
  • A participant acknowledges their initial misunderstanding and shares that they successfully implemented a working code for plotting functions iteratively after correcting the Print order.
  • Another participant suggests a method for solving differential equations iteratively, emphasizing the importance of reevaluating parameters without redefining the function in each loop iteration.
  • A participant confirms improved performance and aesthetics in their calculations after applying the suggested changes.

Areas of Agreement / Disagreement

Participants generally agree on the importance of correct syntax in Mathematica and share solutions to similar problems. However, there are varying approaches to implementing the iterative calculations, and no consensus is reached on a single method for handling differential equations.

Contextual Notes

Some limitations include potential misunderstandings of Mathematica's syntax, the need for clarity in function definitions, and the handling of output within loops. Specific mathematical steps and assumptions in the differential equations remain unresolved.

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 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 27 ·
Replies
27
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
14K