I use the Runge Kutta method in Mathematica to solve a matrix differential equation, that depends on a number of variables. At the end of it, I extract some information that depends on my choice of variables at the start.
I want to be able to numerically optimise this such that the information I extract at the end is done with the best possible choice of variables. A fairly basic version of what I want to accomplish is written below
opt[var_] := (
cc1 =({{var/2, 1},{0, 1/(2 var)}});
dd1 = ( {{0, t/var},{-var t, t}});
rk={};
it = 0;
steps = 10^4;
dt = 10/steps;
sf =cc1;
lya[mat_, time_] := (dd1 /. t -> time).mat + mat.(dd1 /. t -> time)\[Transpose];
Do[si = sf // N;
k1 = lya[si, it] // N;
k2 = lya[si + dt/2 k1, it + dt/2] // N;
k3 = lya[si + dt/2 k2, it + dt/2] // N;
k4 = lya[si + dt k3, it] // N;
sf = si + \dt/6 (k1 + 2 k2 + 2 k3 + k4) // N;
AppendTo[rk, {it, sf}];, {iterations, steps}];
ee = var/2 sf[[1, 1]] + sf[[2, 2]]/(2 var)
)
Let's say ee is what I want to optimise, and var is my variable. I assumed I could just call
NMinimize[{opt[var],0<var<=5},var]
but the RungeKutta won't solve. I've looked around the Mathematica documentation for something around this, and came up with naught.
Any help is appreciated. More information available if required.
For context the actual code I'll be running has 6x6 matrices, with 10 variables to optimise over! these may be able to be reduced.
Thanks
ParametricNDSolveValueto solve the equation? – xzczd Feb 18 '17 at 02:33\dt/6, which is a syntax error Also,itis initialized as0but never changes, even though it is used throughout the remainder of the code and saved inrk. This, too, probably is an error. Please correct. – bbgodfrey Feb 18 '17 at 05:09