Solving Linear Equations-MATLAB

Source: Internet
Author: User

Solving Linear Equations Using MATLAB
Ax = B or XA = B

In Matlab, the Division operators "/" and "\" described in the previous chapter are used to solve linear equations. For example:

X = A \ B indicates the solution of the matrix equation Ax = B;

X = B/A indicates the solution of matrix equation XA = B.

For Equations x = A \ B, it is required that a and B use the same number of rows. X and B have the same number of columns. The number of rows equals to the number of columns in matrix, the same is true for equation x = B/.


If matrix A is not a square matrix and its dimension is m × N, the following are available:

M = n exact definite equations for exact solutions;

M> N hyperdefinite equation, seeking the least squares solution;

M <n Indefinite Equation, seeking for basic solutions, where at most m non-zero elements.



MATLAB uses different algorithms to solve different problems.



1. Exact Equations

The exact equations are composed of n equations with n unknown numbers. The equations have a unique set of solutions. They can be used as matrices, and vectors are written as follows:

Ax = B where A is a matrix and B is a column vector;

In Linear Algebra textbooks, the most common solutions to equations are:

(1) Use the Cramer formula to solve the problem;

(2) using matrix inverse solution, that is, x = A-1b;

(3) Use Gaussian elimination;

(4) use the Lu method to solve the problem.

In general, the results of the above four solutions are not very different for matrices with low dimensions and few conditions. The true significance of the first three solutions lies in its theory, rather than the actual numerical calculation. In Matlab, for the sake of algorithm stability, the calculation of the determinant and inverse is mostly based on the Lu decomposition.

In Matlab, the command for solving such equations is very simple and the expression x = A \ B is used directly.

After confirming that variable A is not singular, the command interpreter of MATLAB performs Lu decomposition on it and finally gives the solution x. If matrix A has a large number of conditions, MATLAB will remind users to pay attention to the reliability of the obtained solutions.

If matrix A is singular, the solution of Ax = B does not exist or exists but is not unique. If matrix A is close to a singular, Matlab will give a warning. If matrix A is strange, the calculation result is INF and a warning is given. If matrix A is a pathological matrix, a warning is also given.

Note: When solving the equation, try not to use the inv (a) * B command, but use the \ B solution. Because the latter is faster and more accurate than the former, especially when the dimension of matrix A is relatively large. In addition, the division command is applicable to non-square matrix A and can also provide a least square solution.


Ii. hyperdefinite Equations

For the equations Ax = B, A is a n × M matrix, if column A is full rank and N> M. The system does not have an exact solution. In this case, the system is called an over-fixed system. Curve Fitting of data is a common problem in linear hyperdefinite equations. For hyperdefinite equations, in MATLAB, the left division command (x = A \ B) is used to find its Least Square solution. Generalized Inversion can also be used to obtain the equation, that is, x = pinv (A), the resulting solution is not necessarily Ax = B, and X is only a solution in the sense of least square. The left division method is based on Singular Value Decomposition, and the resulting solution is the most reliable. The generalized inverse method is based on the direct Householder transformation of the original hyperdefinite equation, the algorithm is less reliable than the singular value solution, but the speed is faster;

[Example 1]

Solving hyperdefinite 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 ()

Ans =

3

X1 = A \ B

X1 =

1.0000

2.0000

1.0000

X2 = pinv (a) * B

X2 =

1.0000

2.0000

1.0000

X1-b *

Ans =

1.0e-014

-0.0888

-0.0888

-0.1332

0

It can be seen that X1 is not the exact solution of equation Ax = B. The solution obtained by x2 = pinv (a) * B is the same as that obtained by X1.


Iii. undefined Equations

The number of unknown equations is more than the number of equations, but theoretically there is an infinite solution. MATLAB will seek a basic solution, where a maximum of m non-zero elements can be found. The special solution is obtained by column Principal Component QR decomposition.

[Example 2]

Undefined 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


Iv. Non-negative Least Square solutions of Equations

Under some conditions, it is meaningless to obtain a negative number for the solution of the linear equations. Although the equations can obtain precise solutions, negative values cannot be obtained. In this case, the non-negative Least Square solution is more meaningful than the exact solution of the equation. In Matlab, the commonly used function nnls for solving non-negative Least Squares is called in the following format:

(1) x = nnls (a, B) returns the least square solution of the equation Ax = B. The process of solving the equation is restricted under the condition of X;

(2) x = nnls (a, B, Tol) Specify the tol to solve the error. The default tol value is Tol = max (SIZE (A) * norm (A, 1) * For EPS, the greater the-1 norm of the matrix, the greater the error in solution;

(3) [x, w] = nnls (a, B) when X (I) = 0, w (I) <0; when X (I)> 0, W (I) 0, and a bidirectional W is returned at the same time.

[Example 3] finding the non-negative Least Square SOLUTION OF THE EQUATIONS

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

A * X-B

Ans =

1.1258

0.1437

-0.1616

X1-b *

Ans =

1.0e-0.15.

-0.2220

0.4441

0

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.