在Matlab中,我尝试使用lsqcurvefit将达芬方程作为微分方程系统拟合到一些生成的数据。但是,当我运行下面的代码时,出现错误:
Error using lsqcurvefit (line 262)
Function value and YDATA sizes are not equal.
Error in de_test (line 10)
[theta,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]= lsqcurvefit(@solve_duffing, theta0,t, data, zdata);
有人知道如何解决此问题吗?
t = [0 1000];
z0 = [100; 1];
theta0=[1;1;1;1;1;1];
xdata = [3.6 7.7 9.3 4.1 8.6 2.8 1.3 7.9 10.0 5.4];
ydata = [16.5 150.6 263.1 24.7 208.5 9.9 2.7 163.9 325.0 54.3];
zdata = [95.09 23.11 60.63 48.59 89.12 76.97 45.68 1.84 82.17 44.47];
data = [xdata,ydata];
[theta,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]= lsqcurvefit(@solve_duffing, theta0,t, data, zdata);
function [zs, ts] = solve_duffing(theta, t)
% Solve the system using the generic ode45 solver.
% x is the output grid of output time steps
% y is the value of z and z' at each point on the output grid.
% (These labels are assigned by Matlab)
z0 = [100; 1];
[x, y] = ode45(@duffing, t, z0);
ts = x;
% Derive z'' from the results
z = y(:,1);
zt = y(:,2);
ztt = -(theta(1)*zt + theta(2)*(zt.^2) + theta(3)*(zt.^3) ...
+ theta(4) *z + theta(5)*(z.^2) + theta(6)*(z.^3));
zs = [z zt ztt];
% Definition of the Duffing Equation as an system of implicit first
% order linear D.Es.
%
% Inputs
% t - The current time step. As the D.E. is implicit this is ignored.
% However it is required for the numeric solver.
% z - A vector of the values (z, z') for the current time step
% Outputs
% dz - A vector of the derivatives of z (ie. z', z'') for the current
% time step
function dz = duffing(~,z)
dz1 = z(2);
dz2 = -(theta(1)*z(2) + theta(2)*z(2)^2 + theta(3)*z(2)^3 ...
+ theta(4)*z(1) + theta(5)*z(1)^2 + theta(6)*z(1)^3);
dz = [dz1; dz2];
end
end