1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Wolfram Mathematica] - how scripts work in Mathematica

  1. Nov 6, 2013 #1
    [Wolfram Mathematica] -- how scripts work in Mathematica

    Hi. This is not a homework. I just did a script in Octave and I've just finished writing it on Wolfram Mathematica. The problem is that I don't know how to define the function, I don't know how scripts work in Mathematica.

    My script should be like this:

    Code (Text):
    Euler[c_,final_,step_,o_,r_,b_]:=
    x=ConstantArray[0,{final/step,3}];
    For[i=2,i<= final/step,i++,
    ReplacePart[x, {i,1}->o*(x[[2,i-1]]-x[[1,i-1]])*step+x[[1,i-1]]]
    ReplacePart[x, {i,2}-> step*(r*x[[1,i-1]]-x[[2,i-1]]-x[[1,i-1]]*x[[3,i-1]])+x[[2,i-1]]]
    ReplacePart[x, {i,3}-> step*(x[[1,i-1]]*x[[2,i-1]]-b*x[[3,i-1]])+x[[3,i-1]]]
    ]
    But that has a wrong syntax. I just want to type Euler and some parameters and receive that large matrix. How is that done in Mathematica?

    Thanks!

    EDIT: I forgot to write the title! I just typed the Tag, I cannot edit it now, sorry
     
    Last edited: Nov 6, 2013
  2. jcsd
  3. Nov 6, 2013 #2
    Perhaps this

    Euler[c_, final_, step_, o_, r_, b_] := Module[{x},
    x = ConstantArray[0, {final/step, 3}];
    For[i = 2, i <= final/step, i++,
    x = ReplacePart[x, {i, 1} -> o*(x[[2, i - 1]] - x[[1, i - 1]])*step + x[[1, i - 1]]] ;
    x = ReplacePart[x, {i, 2} -> step*(r*x[[1, i - 1]] - x[[2, i - 1]] - x[[1, i - 1]]*x[[3, i - 1]]) + x[[2, i - 1]]] ;
    x = ReplacePart[x, {i, 3} -> step*(x[[1, i - 1]]*x[[2, i - 1]] - b*x[[3, i - 1]]) + x[[3, i - 1]]]
    ];
    x];
    Euler[3, 8, 2, 1, 2, 3]

    but I don't know what parameters you want to pass in and all the ones I try end up giving back zeros.

    Try it, see if you can figure out what needs to change and leave a reply if you need more.
     
  4. Nov 6, 2013 #3
    Thanks mate! I think I could fix it with this:

    Code (Text):
    Euler[c_, final_, step_, o_, r_, b_] := Module[{x},
      x = ConstantArray[0, {Quotient[final, step], 3}];
      x[[1]] = c;
      For[i = 2, i <= Quotient[final, step], i++,
       x[[i, 1]] = o*(x[[i - 1, 2]] - x[[i - 1, 1]])*step + x[[i - 1, 1]] ;
       x[[i, 2]] =
        step*(r*x[[i - 1, 1]] - x[[i - 1, 2]] -
            x[[i - 1, 1]]*x[[i - 1, 3]]) + x[[i - 1, 2]] ;
       x[[i, 3]] =
        step*(x[[i - 1, 1]]*x[[i - 1, 2]] - b*x[[i - 1, 3]]) +
         x[[i - 1, 3]] ;
       ];
      x
      ]
    What this does is to give the points to the solution of the Lorenz system using Euler's method. Try Euler[{0, 2, 0}, 10, 0.5, 10, 28, 8/3] .


    Now the question is, how can I get a three dimensional plot using those points I get as a result? I should see the Lorenz attractor

    Thanks!!

    Edit:

    Just found this code on the Internet:

    Code (Text):
    With[{ip = ListInterpolation[#, {{0, 1}}] & /@ Transpose[x]},
      func[t_] := Through[ip[t]]];

    ParametricPlot3D[func[t], {t, 0, 1}, BoxRatios -> 1]
    and it's working like a charm!! The code seems to be working! Try this:

    x = Euler[{0, 2.01, 0}, 10, 0.01, 10, 28, 8/3]

    and then put that code to see the Lorenz attractor
     
    Last edited: Nov 6, 2013
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: [Wolfram Mathematica] - how scripts work in Mathematica
  1. Mathematica question (Replies: 2)

Loading...