How do I solve a differential equation that has an input derivative using ode45?

12 views (last 30 days)
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.

Accepted Answer

Jan
Jan on 17 Jun 2013
See http://www.mathworks.com/matlabcentral/answers/59582#answer_72047: A linear interpolation is not smooth, such that the step size control of the integrator must fail. Here u has a very hard jump in the 201.th frame. So the only feasible solution is to integrate until the jump, stop the integration and use the final values as new initial values for the next integration.
  1 Comment
Hannes
Hannes on 18 Jun 2013
Edited: Hannes on 18 Jun 2013
I see. Since that's sort of annoying to implement in a modular way (for varying inputs), I wound up using Simulink instead (effectively switching to a discrete solver), which worked for me. But thanks for your response.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!