Solve ode45 system with input calculated inside ode function
2 views (last 30 days)
Show older comments
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 Comments
Torsten
on 7 Aug 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.
Answers (0)
See Also
Categories
Find more on Systems of Nonlinear Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!