通过for循环

时间:2017-07-17 10:04:24

标签: matlab delay ode dde

我想在MATLAB中使用for循环解决以下DDE:

xdot(t) = Ax(t) + BKx(t-h)

其中:

A = [0 1 ; -1 0.1];
B = [0 ; 1];
h = 0.2;
K = [-0.0469 -1.7663];
t = [0 5]

用传统方法解决这个问题很简单,结果是可以接受的。

sol = dde23(ddefun,lags,history,tspan,options,varargin)

但是,当我尝试使用for循环解决它时,结果是错误的。这是我的for循环的简单代码。

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

我认为,此代码中唯一的问题是我的时间步和延迟输入。我对两个代码使用相同的dde函数,所以它不是问题。我想在for循环中解决DDE的原因是" BK"依赖于状态的值(不是在这个简单的例子中),并且在每个时间步骤中我需要更新" BK"。
enter image description here

上面用传统方法绘制的正确答案。 enter image description here 我得到的错误答案" for loop"如上图所示。 有趣的是,正确答案对延迟很敏感。但延迟不影响第二种方法的答案。

1 个答案:

答案 0 :(得分:0)

确定。经过数周的思考,终于找到了解决方案。 简单地:

sol = dde23(((@(t,y,z)ddefun(t,y,z,A,B,K))),0.2,[1;-1],[0 time(i)]);

让魔法发生。 此代码可帮助您在每个时间步更新状态。 我希望将来能帮到你。

All the Best,

新浪

相关问题