How do I solve a differential equation that has an input derivative using ode45?
显示 更早的评论
I have a simple equation I'd like to solve: Fdot = udot - b0*(F - u), where u is a time varying input, F is the variable I'm solving for, and b0 is a scalar constant. However, because there is a derivative of the input involved, I'm not sure how to handle it in ode45. Here's the code I'm using now:
%%Initialize Variables
tpoints = [0 1];
dt = 0.001;
tvec = tpoints(1):dt:tpoints(2);
dtvec = circshift(tvec,[0 -1]);
dtvec = dtvec(1:(size(tvec,2)-1));
initF = 0;
beta0 = 100;
u = ones(1,size(tvec,2));
u(101:200) = 2;
u(201:300) = 1;
%%Run continuous model
[T2,f] = ode45(@(t,y) contForce2(t,y,beta0,u,tvec,dtvec), tpoints, initF);
%%Plot Results
figure;
plot(T2,f,'r');
where the function in question is defined by
function dy = contForce2(t,y,beta0,u_in,tvec,dtvec)
dy = 0;
u = interp1(tvec,u_in,t);
dudt_vec = diff(u_in)./diff(tvec);
if t < dtvec(1)
dudt = dudt_vec(1);
% disp(t)
else
dudt = interp1(dtvec,dudt_vec,t);
end
dy = dudt - beta0*(y - u);
end
In the code above, I handled the input derivative by approximating it at each time step. However, this results in huge jumps in the solution corresponding to jump discontinuities in the input function. I know that these can't actually exist since the system has a smooth exponential solution for any setpoint (this is easily seen from the error dynamics, which can be rewritten as xdot = -b0*x, where error x = F - u, and have a solution of x = exp(-b0*t)).
I would really appreciate any help in dealing with these input derivatives to get plausible solutions for the differential equation.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!