Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. (30 pts) (Problem 9.11): Given the system of equations -3X2 + 7X3-2 x1 + 2x2-

ID: 3591037 • Letter: 2

Question

2. (30 pts) (Problem 9.11): Given the system of equations -3X2 + 7X3-2 x1 + 2x2-x3 3 5x1-2x22 a) Write the equations into the format of Ax = b; b) Compute the determinant of A using the function you wrote in problem 1.c; c) Write a MATLAB function using Cramer's rule to solve x1, X2, and x; d) Write a MATLAB function using Gauss elimination to solve xi, X2, and x3; e) Write a general MATLAB function to check if the solution you got in part c and d is actually the solution of the given equations.

Explanation / Answer

a> Writing the given equations into the format of Ax=b

A = [ 0 -3 7

1 2 -1

5 -2 0 ]

b = [ 2

3

2]

function x = cramer(A,b)
if det(A)==0
error('Matrix is invalid');
end
  
z = size(A,1);
x = zeros(z,1);
  
for i=1:z
B = A;
B(:,i)=b;
x(i) = det(B)/det(A);
end
end

function x = gauss_elimination(A,b)
a = [A b];

[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(m,1);

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

end

function check_solution(A,b,X)
if A*X ~= b
error('Solution is not correct');
  
else
disp('Solution is correct');
end
  
end

Sample run:

>> x = cramer(A,b)

x =

0.9855
1.4638
0.9130

>> check_solution(A,b,x)
Solution is correct

>> x = gauss_elimination(A,b)

x =

0.9855
1.4638
0.9130

>> check_solution(A,b,x)
Solution is correct

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote