如何在Matlab中使用非线性最小二乘法求解超定方程组 [英] How to solve an overdetermined set of equations using non-linear lest squares in Matlab

查看:1098
本文介绍了如何在Matlab中使用非线性最小二乘法求解超定方程组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A11 = cos(x)*cos(y)                           (1)
A12 = cos(x)*sin(y)                           (2)
A13 = -sin(y)                                 (3)
A21 = sin(z)*sin(x)*cos(y) - cos(z)*sin(y)    (4)
A22 = sin(z)*sin(y)*sin(x) + cos(z)*cos(y)    (5)
A23 = cos(x)*sin(z)                           (6)
A31 = cos(z)*sin(x)*cos(z) + sin(z)*sin(x)    (7)
A32 = cos(z)*sin(x)*sin(y) - sin(z)*cos(y)    (8)
A33 = cos(x)*cos(z)                           (9)

我有一组九个方程,只有三个未知数.未知数是x,y和z.我知道A11,A12,A13 .... A33的值.但是这些值可能会有一些噪音,因此我将不得不使用一些优化算法来找到x,y和z的最佳拟合值.

I have a set of nine equations and only three unknowns. The unknowns are x, y and z. I know the values of A11, A12, A13 ....... A33. But these values might have some noise and therefore I will have to use some optimization algorithm to find the best fit values of x,y, and z.

我该如何在Matlab中解决上述超定方程组?

How do I solve the above set of overdetermined equations in Matlab?

我一直在在线搜索,并且遇到了一些功能最为突出的一个.

I have been searching online and I came across a few functions most notably this one .

但是,对于解决问题的最佳方法,我感到非常困惑.需要一点指导....

But I'm very confused as to what's the best way to approach the problem. A little direction is needed....

推荐答案

我最喜欢的是优化工具箱中的lsqcurvefit.

My favorite is lsqcurvefit from the Optimization toolbox.

文档中,您看到它需要:

  • 功能手柄(有趣)
  • 参数(x0)的起始值
  • 在您情况下不存在的其他非拟合参数(xdata)
  • 数据值(ydata)

可以设置选项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.

这篇关于如何在Matlab中使用非线性最小二乘法求解超定方程组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆