更新时间:2023-11-25 18:45:04
我最喜欢的是优化工具箱中的lsqcurvefit
.
My favorite is lsqcurvefit
from the Optimization toolbox.
从文档中,您看到它需要:
可以设置选项optimset
,您可以在其中指定几种经过良好测试的算法之一.也可以在此处设置最大迭代次数或函数公差或参数值公差的最小阈值.
Options can be set optimset
where you can specify one of several well tested algorithms. Also the maximal number of iterations or the minimal threshold on function tolerance or parameter value tolerance can be set there.
如果偶然您不应该使用优化工具箱,则可以始终使用fminsearch
并直接最小化最小平方sum((ydata-fun(x)).^2)
.
If by chance you should not have the optimization toolbox, you can always use fminsearch
and minimize the least squares sum((ydata-fun(x)).^2)
directly.
在这种情况下编写函数fun
(另请参见文档)并使用问题代码的示例为:
And an example of writing function fun
(also see documentation) in the case here and using the code from the question would be:
function r = fun(p, xdata)
x = p(1);
y = p(2);
z = p(3);
% code from here
A11 = cos(x)*cos(y)
A12 = cos(x)*sin(y)
A13 = -sin(y)
A21 = sin(z)*sin(x)*cos(y) - cos(z)*sin(y)
A22 = sin(z)*sin(y)*sin(x) + cos(z)*cos(y)
A23 = cos(x)*sin(z)
A31 = cos(z)*sin(x)*cos(z) + sin(z)*sin(x)
A32 = cos(z)*sin(x)*sin(y) - sin(z)*cos(y)
A33 = cos(x)*cos(z)
% everything in one matrix
r = [A11, A12, A13, A21, A22, A23, A31, A32, A33];
end
人们看到,标量和矢量值函数之间没有真正的区别. Matlab会自动计算数据差异并求和.
One sees that there is no real difference between a scalar and a vectorial valued function. Matlab automatically computes the difference to the data and sums over it.