您可以使用fminunc
这样最小化功能:
x = linspace(-2,2,1000);
f0 = -x.^2;
ftest1 = @(a) a(1)*exp(-a(2)*x.^2);
fmin1 = @(a) trapz(x,(ftest1(a) - f0).^2);
[a1,fval] = fminunc(fmin1,rand(1,2));
plot(x,f0,x,ftest1(a1));
如果您希望最小化两个不同的(向量)变量,是否有类似的方法?:
x = linspace(-2,2,1000);
f0 = -x.^2;
ftest2 = @(a,b) a(1)*exp(-b(1)*x.^2) + a(2)*exp(-b(2)*x.^2);
fmin2 = @(a,b) trapz(x,(ftest2(a,b) - f0).^2);
%[a2,b2,fval] = fminunc(...