# matrix division in matlab.

Status
Not open for further replies.

#### ramani

##### Member level 4
hi to all,

i need to solve ax=b.
where a is a n×n tridiagonal matrix. b is n×n matix. and x is n×n matrix.
what command in matlab i have to use to get the value of x.

shall i use b/a;
but if i use like this i am getting a wrong value.
what is the correct syntax.

a=[1,2,0;3,1,2;0,3,1];
>> a

a =

1 2 0
3 1 2
0 3 1
b=[3,3,3;3,3,3;3,3,3];
>> b

b =

3 3 3
3 3 3
3 3 3
b/a

ans =

-0.2727 1.0909 0.8182
-0.2727 1.0909 0.8182
-0.2727 1.0909 0.8182

which is wrong one.

even i use b./a i am getting wrong answer.

#### echo47

Beware the non-commutativity of matrix multiplication and division. You solved xa=b instead of ax=b.

Try a matrix left division:

a = [1,2,0; 3,1,2; 0,3,1];
b = [3,3,3; 3,3,3; 3,3,3];
x = a \ b;
a * x

ans =
3.00000000000000 3.00000000000000 3.00000000000000
3.00000000000000 3.00000000000000 3.00000000000000
3.00000000000000 3.00000000000000 3.00000000000000

#### Quibbler

##### Junior Member level 1
If A X = B
then A-1 A X = A-1 B
where A-1 is the inverse of A

so X = A-1 B

So you have to find the inverse of A - never used matlab so don't know how to do it. But here's a basic routine that will invert the NxN matrix E and return the inverted matrix X.

Code:
DEFINT I-N
FOR I = 1 TO N
FOR J = 1 TO N
F(I, J) = E(I, J)
X(I, J) = 0!
NEXT J
X(I, I) = 1!
NEXT I
FOR J = 1 TO N
FOR I = 1 TO N
IF I <> J THEN
F(J, I) = F(J, I) / F(J, J)
X(J, I) = X(J, I) / F(J, J)
END IF
NEXT I
X(J, J) = X(J, J) / F(J, J)
F(I, I) = 1!
FOR K = 1 TO N
IF K <> J THEN
FOR L = 1 TO N
IF L <> J THEN F(K, L) = F(K, L) - F(K, J) * F(J, L)
X(K, L) = X(K, L) - F(K, J) * X(J, L)
NEXT L
F(K, J) = 0!
END IF
NEXT K
NEXT J

#### echo47

Yes, inverse works fine too.
In MATLAB, the inverse of square matrix A is inv(A).

Status
Not open for further replies.