Professional Documents
Culture Documents
>> syms x1 x2 x3 x4
>> equ1=2*x1+x2+x3-x4==10;
>> equ2=x1+5*x2-5*x3-6*x4==25;
>> equ3=-7*x1+3*x2-7*x3-5*x4==5;
>> equ4=x1-5*x2+2*x3+7*x4==11;
>> sol=solve([equ1,equ2,equ3,equ4],[x1,x2,x3,x4]);
>> x1sol=sol.x1
x1sol =
-87
>> x2sol=sol.x2
x2sol =
486
>> x3sol=sol.x3
x3sol =
46
>> x4sol=sol.x4
x4sol =
348
Question 3 B part
function [x,err]=gauss_jordan_elim(A,b)
A = [2 1 1 -1;1 5 -5 -6;-7 3 -7 -5;1 -5 2 7] % input for augmented matrix A
b = [10 ; 25; 5; 11] % intput for matrix B
[n,m]=size(A); % finding the size of matrix A
err =0; % calculation of error
x=zeros(n,1); % calling fuction zero
if n ~= m
disp('error: n~=m'); % displaying error if found
err = 1;
end % end of the scope of if
if length(b) ~= n % finding the legth of matrix B
disp('error: wrong size of b'); % displaying error, if found
err = 2;
else
if size(b,2) ~= 1
b=b';
end % end of the scope of if-else
if size(b,2) ~= 1
disp('error: b is a matrix'); % displaying erron in matrix B
err = 3;
end
end
if err == 0
Aa=[A,b];
for i=1:n
[Aa(i:n,i:n+1),err]=gauss_pivot(Aa(i:n,i:n+1));
if err == 0
Aa(1:n,i:n+1)=gauss_jordan_step(Aa(1:n,i:n+1),i);
end
end
x=Aa(:,n+1);
end
A=0;
function A1=gauss_jordan_step(A,i) % calling of fuction function
ANSWER
>> gauss_jordan_elim
A =
2 1 1 -1
1 5 -5 -6
-7 3 -7 -5
1 -5 2 7
b =
10
25
5
11
ans =
-87.0000
486.0000
46.0000
348.0000