I need MATLAB code no handwritten solution required.
Write a MATLAB script to solve the following equations using the
Cramer’s rule, and compare your results with MATLAB’s root finding
method ( 25 pts. ).
8x1 − 2x2 − 1x3 = 5
− 2x1 + 9x2 − 4x3 − x4 = 2
− x1 − 3x2 + 7x3 − x4 − 2x5 = 1
− 4x2 − 2x3 + 12x4 − 5x5 = 1
− 7x3 − 3x4 − 15x5 = 5
Code:
clc;
clear all;
A = [8 -2 -1 0 0;
-2 9 -4 -1 0;
-1 -3 7 -1 -2;
0 -4 -2 12 -5;
0 0 -7 -3 -15];
B = [5;2;1;1;5];
x = ones(5,1);
a_det = det(A);
for i = 1:5
C = A;
C(:,i) = B;
x(i,1) = det(C)/a_det;
end
x_matlab = A\B;
disp('x by cramers rule:');
disp(x);
disp('x by matlab:');
disp(x_matlab);
Output:
Hope this helps.
Get Answers For Free
Most questions answered within 1 hours.