[Matlab] solving systems of linear equations

Source: Internet
Author: User

Transferred from: http://silencethinking.blog.163.com/blog/static/911490562008928105813169/

Ax=b or Xa=b
In MATLAB, when solving linear equations, the division operator "/" and "\" are mainly used in the previous chapters. Such as:

    • The x=a\b expresses the solution of the ax=b of the matrix equation;
    • X=B/A represents the solution of the matrix equation xa=b.

For the x=a\b of the equations, A and B are required with the same number of rows, X and B have the same number of columns, and the number of rows equals the number of columns of matrix A, and the equation x=b/a the same.

If matrix A is not a square and its dimension is MXN, then there are:

    • M=n the equation and solve the exact solution;
    • M>n the super-definite equation, seeking the least squares solution;
    • M<n the indefinite equation, seeking the basic solution, where at most there are m non-0 elements.

For different situations, MATLAB will use different algorithms to solve.

The set of equations

The set of equations consists of n equations of n unknowns, and the equations have a unique set of solutions, the general form of which is matrix, and the vectors are written in the following form:

Where A is a phalanx and B is a column vector;

In the textbook of linear algebra, the most commonly used solutions to equations are:

    • Using Cramer formula to solve the method;
    • Using matrix to solve the inverse method, namely x=a-1b;
    • Using Gaussian to eliminate the law;
    • It is solved by Lu method.

Generally speaking, the result of the above four methods is not very different for a matrix with a few dimensions and a small number of conditions. The real meaning of the first three solutions is in its theory, not the actual numerical calculation. In Matlab, for the sake of the stability of the algorithm, the calculation of determinant and inverse is mostly based on LU decomposition.
In MATLAB, the command to solve such a group of equations is very simple, directly using the expression: x=a\b.
In the MATLAB instruction interpreter after confirming that the variable A is non-singular, it is LU decomposition, and finally gives the solution x, if the condition number of matrix A is large, MATLAB will remind the user to pay attention to the reliability of the solution.
If the matrix A is singular, then the solution of the ax=b does not exist, or exists but is not unique, and if the matrix A is near singular, Matlab will give a warning message, if a is singular, the result is INF, and a warning message is given if matrix A is a pathological matrix.
Note: In solving the equation, try not to use the Inv (A) *b command, but the a\b solution should be used. Because the latter is faster and more accurate than the former, especially when the dimension of matrix A is relatively large. In addition, the applicable line of the division command is stronger, and the least squares solution can be given for non-square A.

Super-Set equations

For the equation group ax=b,a is the NXM matrix, if column A is full rank and n>m. There is no exact solution to the equations, and the equations are called super-set equations. The frequently encountered problem of linear super-set equations is the curve fitting of data. For the super-definite equation, in Matlab, the left-side command (x=a\b) is used to find its least-squares solution, and it can be obtained by generalized inverse, that is, X=PINV (A), the resulting solution does not necessarily satisfy the ax=b,x only the least square meaning of the solution. The left method is based on singular value decomposition, and the solution obtained is the most reliable, and the generalized inverse method is based on the direct householder transformation of the original super-fixed equation, and its algorithm is less reliable and singular value, but faster.

An example of solving the super-set equations

a=[2-1 3;3 1-5;4-1 1;1 3-13]
A=
2-1 3
3 1-5
4-1 1
1 3-13
B=[3 0 3-6] ';
Rank (A)
ans=
3
x1=a\b
x1=
1.0000
2.0000
1.0000
X2=PINV (A)b
X2=
1.0000
2.0000
1.0000
A
X1-b
ans=
1.0e-014
-0.0888
-0.0888
-0.1332
0
Visible X1 is not the exact solution of the equation ax=b, and the solution obtained by X2=PINV (A) *b is the same as X1.

Under-Set equations

The number of unknowns in the uncertain equations is more than the number of equations, but theoretically there are infinite solutions. MATLAB will seek a basic solution in which the maximum number of m non-0 elements can be. The special solution is computed by the column main element QR decomposition.

Cases Urealyticum Under-set equations

A=[1-2 1 1;1-2 1-1;1-2 1 5]
A=
1-2 1 1
1-2 1-1
1-2 1-1
1-2 1 5
B=[1-1 5] '
x1=a\b
Warning:rank deficient,rank=2 tol=4.6151e-015
x1=
0
-0.0000
0
1.0000
X2=PINV (A) *b
X2=
0
-0.0000
0.0000
1.0000

Non-negative least squares solution of the equation group

Under some conditions, it is meaningless to find a negative number for the solution of the linear equations. Although the equations can obtain exact solutions, they cannot take negative solutions. In this case, the exact solution of the nonnegative least squares Cheby equation is more meaningful. In MATLAB, the common function nnls of non-negative least squares solution is obtained, and its invocation format is:

    • X=nnls (A, B) returns the least-squares solution of the ax=b equation, and the solution of the equation is limited to the x condition;
    • X=NNLS (a,b,tol) specifies the error TOL to solve, the default value of TOL is Tol=max (size (A))norm (a,1)EPs, the larger the 1 norm of the matrix, the greater the error of the solution;
    • [X,w]=nnls (A, B) when x (i) =0, W (i) <0; present X (i) >0, W (i) 0, and return a bidirectional amount of W.
A non-negative least-squares solution for solving the equations by example

a=[3.4336-0.5238 0.6710
-0.5238 3.2833-0.7302
0.6710-0.7302 4.0261];
b=[-1.000 1.5000 2.5000];
[X,w]=nnls (A, B)
x=
0
0.6563
0.6998
w=
-3.6820
-0.0000
-0.0000
x1=a\b
x1=
-0.3569
0.5744
0.7846
AX-b
ans=
1.1258
0.1437
-0.1616
A
X1-b
ans=
1.0e-0.15
-0.2220
0.4441
0

?

[Matlab] solving systems of linear equations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.