[Wolfram Mathematica] - how scripts work in Mathematica

Click For Summary
SUMMARY

This discussion focuses on defining functions and executing scripts in Wolfram Mathematica, specifically for implementing Euler's method to solve the Lorenz system. The user initially struggles with syntax errors in their function definition but receives guidance on correcting it. The final solution involves using a Module to encapsulate the logic and properly indexing the array. Additionally, a method for visualizing the results using ParametricPlot3D is provided, enabling the user to plot the Lorenz attractor effectively.

PREREQUISITES
  • Familiarity with Wolfram Mathematica syntax and functions
  • Understanding of Euler's method for numerical solutions
  • Basic knowledge of the Lorenz system and its equations
  • Experience with 3D plotting in Mathematica
NEXT STEPS
  • Explore advanced function definitions in Wolfram Mathematica
  • Learn about ListInterpolation in Mathematica for data visualization
  • Investigate other numerical methods for solving differential equations
  • Study the properties and applications of the Lorenz attractor in chaos theory
USEFUL FOR

Mathematicians, data scientists, and educators interested in numerical methods and visualizing complex systems using Wolfram Mathematica.

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 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 13 ·
Replies
13
Views
3K