Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Repeated differential equations

  1. Sep 23, 2009 #1
    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
     
  2. jcsd
  3. Sep 23, 2009 #2

    lurflurf

    User Avatar
    Homework Helper

    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]
     
  4. Sep 23, 2009 #3
    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 :-(
     
  5. Sep 23, 2009 #4

    Dale

    Staff: Mentor

    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.
     
  6. Sep 24, 2009 #5
    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:
     
  7. Sep 29, 2009 #6

    Hepth

    User Avatar
    Gold Member

    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.
     
  8. Oct 5, 2009 #7
    Thanks, I did that and I think my calculation is faster now... and, in any case, it looks better!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Repeated differential equations
Loading...