[SOLVED] Solution of Linear Equation using MATLAB

Status
Not open for further replies.

pancho_hideboo

Advanced Member level 5
Joined
Oct 21, 2006
Messages
2,847
Helped
767
Reputation
1,536
Reaction score
733
Trophy points
1,393
Location
Real Homeless
Activity points
17,490
Code:
A = [ 1, 2, -1, -2, -3;
      2, 4,  0, -3, -1;
      1, 2,  1,  0,  1;
      3, 6, -2, -5, -7;
      2, 4, -3, -5, -8]
  
b = [6, 7, 2, 16, 14].'

size(A)=5x5
rank(A)=3
det(A)=0

So A*x = b does not have unique solution.

Solution x is x = d + t1*c1 + t2*c2.
Here
d = [5, 0, -3, 1, 0].'
c1 = [-2, 1, 0, 0, 0].'
c2 = [2, 0, -3, 1, 1].'
t1 and t2 are any values.

How can I get this solution by MATLAB ?

Solution by pseudo inverse matrix of A gives following.
Code:
pinv(A)*b 
=>
    0.5932
    1.1864
    0.0508
   -0.0169
   -1.0169
 
Last edited:

General solution can be given as following.
Code:
x = pinv(A)*b + (eye(5)-pinv(A)*A)*[t1,t2,t3,t4,t5].’

https://en.wikipedia.org/wiki/Moore–Penrose_inverse

Code:
d = pinv(A)*b % particular solution
c = eye(size(A, 2)) - pinv(A)*A % homogeneous solutions
fprintf( 2, 'rank(c)=%d\n', rank(c, 1e-5) );
R = rref(c) % reduced row echelon form

t = randn(size(c, 2), 1);
x = d + c*t; % general solution for inhomogeneous equation
fprintf(1, 'A*x-b'), A*x-b
gives followings.
Code:
d =
    0.5932
    1.1864
    0.0508
   -0.0169
   -1.0169

c =
    0.8136   -0.3729   -0.1017    0.0339    0.0339
   -0.3729    0.2542   -0.2034    0.0678    0.0678
   -0.1017   -0.2034    0.7627   -0.2542   -0.2542
    0.0339    0.0678   -0.2542    0.0847    0.0847
    0.0339    0.0678   -0.2542    0.0847    0.0847

rank(c)=2

R =
    1.0000         0         0   -0.4687    0.0260
         0    1.0000         0   -0.9375    0.0521
         0         0    1.0000   -0.6458   -0.3160
         0         0         0         0         0
         0         0         0         0         0

A*x-b
ans =
  1.0e-013 *
   -0.0178
   -0.1243
   -0.0533
   -0.0355
         0
c(homogeneous solutions) are not independent.

Code:
M = [A, -b]; % augmented matrix

ZR = null(M, 'r') % rational basis for the null space
d = ZR(1:end-1, end) % particular solution
c = ZR(1:end-1, 1:end-1) % homogeneous solutions

t = randn(size(c, 2), 1);
x = d + c*t; % general solution for inhomogeneous equation
fprintf(1, 'A*x-b'), A*x-b
gives followings.
Code:
ZR =
    -2     2     5
     1     0     0
     0    -3    -3
     0     1     1
     0     1     0
     0     0     1

d =
     5
     0
    -3
     1
     0

c =
    -2     2
     1     0
     0    -3
     0     1
     0     1

A*x-b
ans =
  1.0e-014 *
         0
   -0.0888
   -0.0222
   -0.1776
   -0.1776
These d and c are coincident with theoretical solutions.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…