How to use least squares method in Matlab?

大兔子大兔子 提交于 2019-11-28 09:10:11

问题


I have 37 linear equations and 36 variables in the form of a matrix equation; A*X=B . The equations don't have an exact answer. I want to use Matlab least square method to find the answers with the least error. I am new to Matlab so any comments will help. Thank you


回答1:


If A is of full rank, i.e. the columns of A are linearly independent, the least-squares solution of an overdetermined system of linear equations

A * x = b

can be found by inverting the normal equations (see Linear Least Squares):

x = inv(A' * A) * A' * b

If A is not of full rank, A' * A is not invertible. Instead, one can use the pseudoinverse of A

x = pinv(A) * b

or Matlab's left-division operator

x = A \ b

Both give the same solution, but the left division is more computationally efficient.

The two latter computation methods can also deal with underdetermined systems of linear equations, but they give different solutions in that case: The pseudoinverse gives the solution where x has the smallest sum of squares, while the left-division operator gives a solution with as many 0 coefficients as possible.




回答2:


The most general way to solve this is to use the pseudoinverse:

X = pinv(A) * B;



回答3:


You can calculate x by:

x = (A'*A)\A'*B



来源:https://stackoverflow.com/questions/30334707/how-to-use-least-squares-method-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!