EAT 2016F IcaEngineering httpsallspringboarduakronedud2Melco
Solution
% matlab program to solve the system of equations
% -3x2+7x3 = 2
% x1+2x2-x3 = 3
% 5x1-2x2 = 2
A = [0 -3 7
1 2 -1
5 -2 0];
B = [2 3 2]\';
disp(\'Solution using MATLAB:\');
X = inv(A)*B
detterminant = det(A)
%% Now solving using Cramer\'s rule
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = [0 -3 7
1 2 -1
5 -2 0];
B = [2 3 2]\';
Ax1 = [2 -3 7
3 2 -1
2 -2 0];
Ax2 = [0 2 7
1 3 -1
5 2 0];
Ax3 = [0 -3 2
1 2 3
5 -2 2];
detA = det(A);
detAx1 = det(Ax1);
detAx2 = det(Ax2);
detAx3 = det(Ax3);
x1 = detAx1/detA;
x2 = detAx2/detA;
x3 = detAx3/detA;
disp(\'Solution from Cramers rule:\');
x1
x2
x3
%% Now solving using Gauss elimination
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a= [0 -3 7 2;1 2 -1 3;5 -2 0 2];
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(j,:);a(j,:)=a(z,:);
a(z,:)=t;
end
end
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
x=zeros(1,m);
for s=m:-1:1
c=0;
for k=2:m
c=c+a(s,k)*x(k);
end
x(s)=(a(s,n)-c)/a(s,s);
end
disp(\' Solution from Gauss elimination method:\');
x\'
% Program ends here
After running the above program you will get the following output
OUTPUT:
Solution using MATLAB:
X =
0.9855
1.4638
0.9130
detterminant =
-69
Solution from Cramers rule:
x1 =
0.9855
x2 =
1.4638
x3 =
0.9130
Solution from Gauss elimination method:
ans =
0.9855
1.4638
0.9130

