以下代码运行良好并给出了准确的结果。
c1...c5 是已知常数,x(1)...x(5)(匿名向量)是未知变量,需要fminsearch
使用最小二乘法进行拟合。(t,y) 是拟合方程的给定数据(曲线),myfunc
是ode45
保存在单独的 .m 文件中的函数。
% run the solver
options = optimset('MaxFunEvals',10000,'MaxIter',10000,'Display','iter');
[x,fval,exitflag,output] = fminsearch(@(x) Obj(x,initial_p,c1,c2,c3,c4,c5,c6,t,y),x0,options);
function F = Obj(x,initial_p,c1,c2,c3,c4,c5,c6,t,y)
p = myfunc(x(2:end),initial_p,t,c1,c4);
yt = mainfunc(x,p,c1,c2,c3,c4,c5,c6);
F = sqrt(sum((yt-y).^2));
disp(x);
end
function yt = mainfunc(x,p,c1,c2,c3,c4,~,~)
yf = c1*c2*c3*c4*sqrt(p);
yt = x(1)+yf;
end
但是Assignment has more non-singleton rhs dimensions than non-singleton subscripts
当我重写时发生错误()mainfunc
(所有其他都相同):
function yt = mainfunc(x,p,c1,c2,c3,c4,c5,c6)
ys = c1*c3*(c5/(c6*sqrt(p)));
yf = c1*c2*c3*c4*sqrt(p);
yt = x(1)+ys+yf;
end
p 是来自ode45
in的曲线 (t,p) myfunc
,它在第一个版本的代码中运行良好,所以没有问题。sqrt(p) 有两个术语会mainfunc
导致问题吗?