How to solve linear equation using Matlab?

2.3k Views Asked by At
A= [1 2 3;3 4 5;5 6 7], 
B=[1;1;1].

I need to solve the equation $AX=B$. Here am using Matlab code like

 X=linsolve(A,B). 

But, using this a warning is occurred...

"Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. 
RCOND = 1.541976e-18."

How to correct it?

1

There are 1 best solutions below

0
On

(1) First and easiest way to solve it: NOT using Matlab for this easy linear system that you can solve with a pencil and a piece of paper. :-) For instance, by means of Gaussian elimination

$$ \begin{pmatrix} 1 & 2 & 3 & | & 1 \\ 3 & 4 & 5 & | & 1 \\ 5 & 6 & 7 & | & 1 \end{pmatrix} \longrightarrow \dots \longrightarrow \begin{pmatrix} 1 & 0 & -1 & | & -1 \\ 0 & 1 & 2 & | & 1 \end{pmatrix} $$

Which tells you that the solution set of the system are all vectors of the form $(-1, 1, 0) + z (1, -2, 1)$.

(2) Secondly, if you're lazy to do those previous calculations, you can even ask Matlab to do them for you: the command "rref" reduces matrices to row-echelon form:

A = [1 2 3; 3 4 5; 5 6 7]

A =

 1     2     3
 3     4     5
 5     6     7

b = [1 1 1]'

b =

 1
 1
 1

Ab = [A b]

Ab =

 1     2     3     1
 3     4     5     1
 5     6     7     1

rref(Ab)

ans =

 1     0    -1    -1
 0     1     2     1
 0     0     0     0

(3) The reason why Matlab doesn't like this system is because its matrix $A$ is not full rank (you can see that row of zeros appearing over there). Hence, the system could have no solution at all (if we hadn't that $0$ on the fourth column, third row); or, as it happens in the present situation, have an infinite number of them. Despite of this, Matlab tries hard and finds a solution, but warning you that, since the matrix $A$ is singular, "results may be inaccurate".

X = A\b Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.850372e-18.

X =

-4.0000 7.0000 -3.0000

(4) So, it's always good to know in advance which kind of system have you got: the Rouché-Capelli theorem (also known as "Rouché-Frobenius") tells you everything you need to know about linear systems. And Matlab can help you to do the computations:

rank(A)

ans =

 2

rank(Ab)

ans =

 2

This tells you that the solution set is not empty and it's a straight line (dimension of the solution set $= 3 - \mathrm{rank}\ A = 1$).