0
$\begingroup$

I have a program where I solve the Lorentz-63 system (system of ODEs). I wish to generalize my program to make my code cleaner.

Is there any way to create a function that works in a fashion such as this

plot = myLorentz['system','initial values',"various parameters"].

Then to get a plot returned![enter image description here][1]

my current code:

s = 10; b = 8/3; r = 20;

system = {x'[t] == s (y[t] - x[t]), 
       y'[t] == -x[t] z[t] + r x[t] - y[t], z'[t] == x[t] y[t] - b z[t]};

initVals = {x[0] == 5, y[0] == 5, z[0] == 5};

lorenz = NDSolve[{system, initVals}, {x, y, z}, {t, 0, 500}, 
        MaxSteps -> Infinity];

a = ParametricPlot3D[
    Evaluate[{x[t], y[t], z[t]} /. lorenz], {t, 0, 500}, 
    PlotPoints -> 10000, PlotStyle -> Thin]; 

.

$\endgroup$
  • 1
    $\begingroup$ Please insert the actual code in your question, not an image of it. Doing so will increase the chance of receiving good responses. $\endgroup$ – bbgodfrey Mar 20 '15 at 16:04
  • $\begingroup$ You can combine most of your code in one Module and assign that to a function name with some parameters. Have a look at DefiningFunctions $\endgroup$ – Sjoerd C. de Vries Mar 20 '15 at 16:09
  • $\begingroup$ Thanks guys. I got something working now. I appreciate you taking the time to answer my question. $\endgroup$ – Magnar Mar 20 '15 at 16:24
1
$\begingroup$

Think this answers my question

plotofSystem[system_, init_, runtime_] := 
 Module[{sys = system, in = init, tmax = runtime},
  par = {sys, in};
  lorenz = NDSolve[par, {x, y, z}, {t, 0, tmax}, MaxSteps -> Infinity];
  ParametricPlot3D[Evaluate[{x[t], y[t], z[t]} /. lorenz], {t, 0,tmax}, PlotPoints -> 10000, PlotStyle -> Thin]
   ]

and I define system and initial values in the following way

system[s_, b_, r_] = {x'[t] == s (y[t] - x[t]),y'[t] == -x[t] z[t] + r x[t] - y[t], z'[t] == x[t] y[t] - b z[t]};
initvalues = {x[0] == xstart, y[0] == ystart, z[0] == zstart};

and run the program by

plotofSystem[system[s, b, r], initvalues, runtime]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Didn't you intend to let system be a parameter of your function? It's now a global variable, just like lorentz and plotofSolution. On the other hand, the local variables that you define don't seem to be necessary as they don't get reassigned. You could have used the parameters instead. By the way: Return is not really necessary. Module returns the last value ( if it is not suppressed by ";"). $\endgroup$ – Sjoerd C. de Vries Mar 20 '15 at 20:48
  • $\begingroup$ You are correct. I made some changes that appears to work. $\endgroup$ – Magnar Mar 21 '15 at 10:57
  • $\begingroup$ Nice, but it can still be improved. None of your local variables are necessary. You could have used the function parameters directly. If you feel you need par, you should make it local. $\endgroup$ – Sjoerd C. de Vries Mar 21 '15 at 15:48
  • $\begingroup$ Do you mean I can insert the NSolve function into ParametricPlot3D. And in that way avoid the variable lorentz? If not I am not sure what you mean. Thank you for the feedback! $\endgroup$ – Magnar Mar 21 '15 at 16:14
  • $\begingroup$ I didn't actually mention Lorenz, but yes, you could do that. Or you can use NDSolveValue which doesn't need replacements. I was actually talking about sys, in and tmax. Defining these is not necessary, because you don't assign them to new values after their initialization with the function parameters. Therefore, you can simply use the function parameters everywhere you used these local variables. Note that you haven't localized par and lorenz. $\endgroup$ – Sjoerd C. de Vries Mar 21 '15 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.