已经晚了 - 我找不到我的错误在我的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
我用来执行代码的主文件:
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))
这是错误:
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> lverr at 8
value = (y(:,1)-H').^2+(y(:,2)-L').^2;