I want to solve following DDE using a for loop in MATLAB:
xdot(t) = Ax(t) + BKx(t-h)
in which:
A = [0 1 ; -1 0.1];
B = [0 ; 1];
h = 0.2;
K = [-0.0469 -1.7663];
t = [0 5]
Solving this with conventional procedure is simple and the results are acceptable.
sol = dde23(ddefun,lags,history,tspan,options,varargin)
However, when I try to solve it using for loop, the results are wrong. Here is a simple code for my for loop.
time = 0:0.001:5;
for i = 2:5001
x(:,1) = [1 -1];
history(:,1) = [1 -1];
[t h] = ode23(@(t,h)histExam1(t,h,A,B,K),[time(i-1) time(i)],history(:,i-1));
history(:,i)= h(end,:);
sol = dde23(((@(t,y,z)ddefun(t,y,z,A,B,K))),0.2,history(:,i),[time(i-1) time(i)]);
x(:,i)=sol.y(:,end);
end
I think, the only problem in this code is my time steps and delay input. I use same dde function for both codes so It cannot be a problem.The reason I want to solve DDE in a for loop is "BK" value which is state dependent (not in this simple example) and in each time step I need to update "BK".
The correct answer plotted above with conventional method. And the wrong answer I get using "for loop" is plotted above. It is interesting to mention that correct answer is sensitive to delay. But delay doesn’t affect the answer from 2nd method.
Ok. After weeks of thinking, finally found a solution. Simply:
sol = dde23(((@(t,y,z)ddefun(t,y,z,A,B,K))),0.2,[1;-1],[0 time(i)]);
and let the magics happen. This code helps you with updating states at each time step. I hope it will help you in the future.
All the Best,
Sina