# Simplifying code

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];


.

• Please insert the actual code in your question, not an image of it. Doing so will increase the chance of receiving good responses. – bbgodfrey Mar 20 '15 at 16:04
• 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 – Sjoerd C. de Vries Mar 20 '15 at 16:09
• Thanks guys. I got something working now. I appreciate you taking the time to answer my question. – Magnar Mar 20 '15 at 16:24

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]

• 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 ";"). – Sjoerd C. de Vries Mar 20 '15 at 20:48
• You are correct. I made some changes that appears to work. – Magnar Mar 21 '15 at 10:57
• 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. – Sjoerd C. de Vries Mar 21 '15 at 15:48
• 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! – Magnar Mar 21 '15 at 16:14
• 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. – Sjoerd C. de Vries Mar 21 '15 at 16:50