[Wolfram Mathematica] - how scripts work in Mathematica

In summary, the conversation was about the use of scripts in Mathematica, specifically for creating a script to solve the Lorenz system using Euler's method. The conversation included a discussion on the code syntax and parameters, as well as a solution for creating a three dimensional plot using the points generated by the script.
  • #1
Hernaner28
263
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
  • #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.
 
  • #3
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:

1. How do I write a script in Mathematica?

In Mathematica, scripts are written using the Wolfram Language. You can either use the built-in interface or a text editor to write your script. The script must start with a Shebang line (#!) that specifies the location of the Mathematica kernel on your computer. After that, you can write your code following the syntax of the Wolfram Language.

2. How do I run a script in Mathematica?

To run a script in Mathematica, you can either use the built-in interface or the command line. In the interface, you can go to File > Open and select your script. Then, click on the "Run" button to execute the script. In the command line, you can use the command "math -script scriptname.m" to run your script.

3. Can I pass arguments to my script in Mathematica?

Yes, you can pass arguments to your script in Mathematica. To do so, you can use the command line option "-run" followed by the arguments you want to pass. In your script, you can use the function $CommandLine to access the arguments. For example, if you pass the arguments "3" and "5" to your script, you can access them as $CommandLine[[3]] and $CommandLine[[4]].

4. How do I save the output of my script in Mathematica?

In Mathematica, you can save the output of your script by using the function Export. This function allows you to specify the format in which you want to save the output (e.g. PDF, PNG, etc.) and the location where you want to save it. You can also use the command line option "-o" to specify the output file and format when running your script.

5. Can I use external packages in my script in Mathematica?

Yes, you can use external packages in your script in Mathematica. To do so, you can use the command "Needs" to load the package at the beginning of your script. You can also specify the location of the package using the option "AddToPath". Alternatively, you can use the command line option "-packages" to specify the packages you want to load when running your script.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
881
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • STEM Educators and Teaching
2
Replies
38
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
850
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
7K
Back
Top