问题
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)
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.
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....
回答1:
My favorite is lsqcurvefit
from the Optimization toolbox.
From the documentation you see that it requires:
- function handle (fun)
- starting values for the parameters (x0)
- additional non-fitted parameters (xdata) which in your case do not exist
- data values (ydata)
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.
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.
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
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.
来源:https://stackoverflow.com/questions/24382626/how-to-solve-an-overdetermined-set-of-equations-using-non-linear-lest-squares-in