已经很晚了 - 我无法找到我的错误在我的error功能中.我正在尝试参数估计.
function value = lv(t,y,p)
%Lotka-Volterra Model
%p(1)=a, p(2) = b, p(3) = c, p(4) = r.
value = [-p(1)*y(1)-p(2)*y(1)*y(2);-p(4)*y(2)+p(3)*y(1)*y(2)];
end
function error = lverr(p)
%LVERR: Function defining error function for
%example with Lotka-Volterra equations.
H = [30.0 47.2 70.2 77.4 36.3 20.6 18.1 21.4 22 25.4 27.1];
L = [4 6.1 9.8 35.2 59.4 41.7 19 13 8.3 9.1 7.4];
[t,y] = ode45(@lv,[0 10],[H(1);L(1)],[],p);
value = (y(:,1)-H').^2+(y(:,2)-L').^2;
%Primes transpose data vectors H and L
error = sum(value);
end
Run Code Online (Sandbox Code Playgroud)
以及我用来执行代码的主要文件:
clc;
clear all;
format long;
H = [30.0 47.2 70.2 77.4 36.3 20.6 18.1 21.4 22 25.4 27.1];
L = [4 6.1 9.8 35.2 59.4 41.7 19 13 8.3 9.1 7.4];
guess = [0.47;0.024;0.023;0.76];
[p,error] = fminsearch(@lverr,guess);
[t,y]=ode45(@lv,[0 10],[30.0; 4.0],[],p);
subplot(2,1,1)
plot(t,y(:,1))
subplot(2,1,2)
plot(t,y(:,2))
Run Code Online (Sandbox Code Playgroud)
这是错误:
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> lverr at 8
value = (y(:,1)-H').^2+(y(:,2)-L').^2;
Run Code Online (Sandbox Code Playgroud)
该ode45积分器是一个自适应步长,可变阶Runge-Kutta方法.这意味着每个步骤使用的步长取决于此时解决方案的行为.这意味着输出中元素的数量[t,y]会因运行而异,并且永远不应该依赖.
在lverr(p),您希望区分这种积分的结果和一些固定长度的矢量.实际上,这将失败.您可以
ode45仅在指定时间生成输出(调整您的tspan元素数量H)H(和L).暂时不要 - 请不要使用error变量名称; 它也是一个内置函数,可能会导致您和Matlab解释器混淆.
| 归档时间: |
|
| 查看次数: |
767 次 |
| 最近记录: |