Write a MATLAB function that solves a system of linear equations or calculates t
ID: 3569717 • Letter: W
Question
Write a MATLAB function that solves a system of linear equations or calculates the inverse of a matrix by Gauss-Jordan elimination.Your function should accept a matrix as an argument and return a matrix of the same dimension.
Note: Your function should be written to handle matrices of any size. Test your function with matrices of different sizes for which you know the solution to ensure that your function works correctly. Use variable names that relate to their function and include comments that explain your program logic. Do not use any built-in MATLAB functions except size(), input(), and zeros().
****WILL RATE****
Explanation / Answer
function [y]=gauss_jordan(A)
a=A;
[m n]=size(A);
for i=1:m-1
if a(i,i)==0
flag=0;
j=i+1;
while flag==0 && j<=m
if a(j,i)~=0
flag=1;
temp=a(j,:);
a(j,:)=a(i,:);
a(i,:)=temp;
end
end
end
end
for i=1:m
for j=1:m
if j==i || a(j,i)==0
continue;
else
ratio=a(i,i)/a(j,i);
a(j,:)=a(i,:)-ratio*a(j,:);
end
end
end
for i=1:m
a(i,:)=a(i,:)/a(i,i);
end
y=a;
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.