Question
The function gaussel.m solves the system of linear equations using Gaussian elimination and back substitution algorithms. Modify this function into a function gaussjordan.m, which uses
The function gaussel.m solves the system of linear equations using Gaussian elimination and back substitution algorithms. Modify this function into a function gaussjordan.m, which uses Gauss-Jordan instead of Gaussian elimination to solve the system of linear equations. Use your function to solve the system of equations with A = magic(5), and b = [215; 200; 145; 200; 215].
function [x] = gaussel(A,b) % [x] = gaussel(A,b) % % This subroutine will perform Gaussian elimination % and back substitution to solve the system Ax = b. % INPUT : A - matrix for the left hand side. % b - vector for the right hand side % % OUTPUT : x - the solution vector.
n = length(b); if size(A,1) ~= n || size(A,2) ~= n error('Matrix-vector size mismatch.'); end
% Perform Gaussian elimination for k=2:n for i=k:n m = A(i,k-1)/A(k-1,k-1); for j = 1:n A(i,j) = A(i,j) - m*A(k-1,j); end b(i) = b(i) - m*b(k-1); end end % Perform back substitution x = nan(n,1); % preallocate memory for x x(n) = b(n)/A(n,n); for k=n-1:-1:1 m = 0; for j = k+1:n m = m + A(k,j)*x(j); end x(k) = (b(k)-m)/A(k,k); end % End of function end
function x = gaussel2(A,b) % A = [-3 1 0 -3 % -2 -2 -1 0 % -9 4 1 0 % 4 -5 1 2]; % b = [-13 -9 2 5]'; % x = gaussel(A,b);
% (c) Ruslan L. Davidchack 23-Feb-2021
n = length(b); if size(A,1) ~= n || size(A,2) ~= n error('Matrix-vector size mismatch.'); end % Gaussian Elimination for k = 1:n-1 for i = k+1:n m = A(i,k)/A(k,k); % for j = k:n % A(i,j) = A(i,j) - m*A(k,j); % end A(i,k:n) = A(i,k:n) - m*A(k,k:n); b(i) = b(i) - m*b(k); end end % Back Substitution x = nan(n,1); % x(n) = b(n)/A(n,n); for k = n:-1:1 % m = 0; % for j = k+1:n % m = m + A(k,j)*x(j); % end % x(k) = (b(k)-m)/A(k,k); x(k) = (b(k)-A(k,k+1:n)*x(k+1:n))/A(k,k); end end
show all matlab codes used and a pic of matlab command window. Answer the question correctly please.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started