Write a MATLAB function that solves a system of linear equations or calculates t
ID: 3569952 • 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. 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(). As example of what is expected from this function consider the following linear system: 3x + 2y + z = 11 2x + 3y + z = 13 x + y + 4z = 12 In this case you should pass into your function the following augmented matrix: (3 2 1 11 2 3 1 13 1 1 4 12) After performing Gauss-Jordan elimination, your function should return the following reduced system: (1 0 0 1 0 1 0 3 0 0 1 2) from which we can determine that x = 1, y = 3, z = 2. Furthermore, if you pass a double augmented matrix such as (3 2 1 11 -5 2 3 1 13 -4 1 1 4 12 9) Your function should return (1 0 0 1 -2 0 1 0 3 -1 0 0 1 2 3) which solve the original equations 3x + 2y + z = 11 2x + 3y + z = 13 x + y + 4z = 12 as x = 1, y = 3, z = 2 and; at the same time, the equations 3x + 2y + z = -5 2x + 3y + z = -4 x + y + 4z = 9 as x = -2, y = -1, z = 3.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
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.