Several Linear Equations with MATLAB

Suppose we want to solve the problem

where Aij is a square nxn matrix, fi is a vector of length n. xi is a vector of length n, whose elements we wish to determine. Take the example with the matrix Aij and vector fi as shown.

Next consider how the problem is solved in MATLAB. The matrix is entered by using the following command.

>>A = [3.2 4.1
-5.0 0.8]

A =
3.2000 4.1000
-5.0000 0.8000

The vector f is entered using the following command.

>>f = [3
4]

f =
3
4

The solution to the problem is found using:

>>x=A\f
x =
-0.60711188204683
1.20555073720729

(Use format long, and then >> x to get the long version of x.) To test the accuracy we calculate the vector that should be zero.

>>y = A*x - f
y = 1.0e-15 *

-0.44408920985006
0

The elements of y are less than 10-15 in one case and zero to machine accuracy in the other. The solution is very accurate.

If we had entered f as a row vector instead of a column vector, i.e. as

f = [3 4]

then upon using

x = A\f

we would have received this message.

??? Error using ==> \
Matrix dimensions must agree.

We then take the transpose of f, and use the command

x = A\f'

to obtain the correct result.