[Wolfram Mathematica] - how scripts work in Mathematica

AI Thread Summary
The discussion focuses on how to define and execute a function in Wolfram Mathematica for simulating the Lorenz system using Euler's method. The user initially struggles with syntax errors and incorrect parameter passing in their script. After several iterations, they refine their function to correctly compute the values and generate a matrix of results. They also seek assistance in creating a three-dimensional plot of the Lorenz attractor using the computed points. Ultimately, they find a solution for visualizing the attractor with a code snippet that successfully plots the results.
Hernaner28
Messages
261
Reaction score
0
[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:
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:
Physics news on Phys.org
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.
 
Thanks mate! I think I could fix it with this:

Code:
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:
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:

Similar threads

Replies
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
3K
Replies
1
Views
2K
Replies
13
Views
2K
Replies
4
Views
2K
Back
Top