Wednesday, September 17, 2014

Cramer's Rule for solving simultaneous equations

Cramer's Rule
Cramer's rule  is used to find the solution of a system of linear equations with as many equations as unknowns, valid whenever the system has a unique solution. It expresses the solution in terms of the determinants of the (square) coefficient matrix and of matrices obtained from it by replacing one column by the vector of right hand sides of the equations.
Consider the set of linear equations of 3 variable


The above equation can be written in matrix format as  


Then the values of x, y and z can be found with Cramer’s Rule as follows:




Matlab Program for n Variables simultaneous equations using Cramer's Rule

d = input('Enter the determinant matrix = ');
[m,n]=size(d);
while m~=n || det(d)==0
    if m~=n
        disp('Matrix is not Square')
        d = input('Enter the determinant matrix = ');
        [m,n]=size(d);
    else
        disp('Solution do not exist')
        d = input('Enter the determinant matrix = ');
        [m,n]=size(d);
    end
end
v = input('Enter the column vector = ');
[s,t]=size(v);
while (s~=n) || (t~=1)
    fprintf('Matrix is not a column vector of order %f\n',n);
    v = input('Enter the column vector = ') ;
    [s,t]=size(v);
end
delta = det(d);
delta1 = [ v d(:,2:n) ];
x = det(delta1)/delta;
fprintf('\nx1 = %f\n',x);
for c=2:(n-1)
    delta2 = [ d(:,1:c-1) v  d(:,c+1:n)];
    y = det(delta2)/delta;
    fprintf('x%f = %f\n',c,y);
end
delta3 = [ d(:,1:n-1) v];
z = det(delta3)/delta;
fprintf('x%f = %f\n',n,z);

No comments:

Post a Comment