Solve ode45 system with input calculated inside ode function
Mostra commenti meno recenti
Dear MathWorks community,
I have a problem with my ode solving method. I'm trying do solve state-space model in ode45 with input based on actual state calculated inside ode. This input is a bit of random (so probably this is the problem I think) but let me explain.
What I'm trying to do is something like this:
[t, x] = ode45(@(t, x) sys(t, x, plant, MV_bounds, OV_bounds,u_initial), tspan, iniCon);
function dx = sys(t, x, ss_model, MV_bounds, OV_bounds, u_initial)
persistent u % persisten variable needed to save last input u
persistent kk % persisten variable needed to save sampling instant
if(isempty(kk)||t==0)
kk=0;
end
if(isempty(u)||t==0)
u=u_initial;
end
Ts = 0.1;
d_ss_model = c2d(ss_model,Ts);
A = ss_model.A;
B = ss_model.B;
Am = d_ss_model.A;
Bm = d_ss_model.B;
%% some parameters for PSO
....
% optimization
if(floor(t/Ts)>=kk)
% if time reaches optimization sampling time, optimize.
% I added such a condition in case the time step of ODE
% miss exact time of k*Ts (which im sure it will).
% I've tried also set ode option of maxstep to 0.1.
%%
%% PSO optimization that calculates suboptimal input u
% based on testing discrete state-space model of the system.
%%
u=gbest_p'; % resulted input
kk= kk+1;
end
% if time is between optimization sampling,
% hold previous input up to next optimization time
dx = A*x + B*u;
end
When I run this, ode acts weridly. When I add a breakpoint (for checking timesteps) it seems like ode makes some small steps and then takes a step back (sometimes a big step back) and it takes forever to calculate 10s of timespan (but sometimes it finishes, depends on my patience at the moment). I guess this is a poor design, but I'm really new in constructing such systems and I don't really know about restrictions and conditions I must follow in order to make good system/control design.
What I can add, that PSO calculates input vector pretty well. As I set reference state at 0 (with quadcopter state space model linearized at hover conditions), resulting inputs are very small as suspected.
How can I solve such a problem? Do I need to calculate input outside ODE? How can I handle sampling time for optimization in some better, more reliable way? If the idea of such design is poor and just stupid please be honest, I woud appreciate any constructive critics. Sooooomeday I will be PRO in systems control :)
6 Commenti
As far as I know, control input calculation and integration are decoupled.
Usually, you estimate a control vector u = (u0,u1,...,u_(n-1)) for time intervals [t0:t1],...,[t_(n-1:t_n] solve the state-space model with this vector (usually by integrating from t0 to t1 with input u0, calling the solver anew at t1 with control input u1 and integrating up to t2 and so on). With the response vector x = (x0,...,x_n), you estimate a new control vector u_new =(u0_new,u1_new,...,u_n_new), repeat integration and so on.
Piotr Pawlowski
il 7 Ago 2019
Modificato: Piotr Pawlowski
il 7 Ago 2019
Torsten
il 7 Ago 2019
And why do you then calculate (or set) u inside the ODE function and not in an external loop in which the ode integrator is called for the next time step ?
Piotr Pawlowski
il 7 Ago 2019
Torsten
il 7 Ago 2019
That's what I meant.
Although I don't know whether it's necessary to do optimization of single u in the loop. I only know of cases where the complete control vector (for each output time 0:Ts:10) is computed after the loop. Then the loop is executed again with this full new control vector.
Piotr Pawlowski
il 7 Ago 2019
Risposte (0)
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!