My function file my_rref.m is as follows:
function my_rref(A) [m,n]=size(A); for i=1:m-1 for j=i+1:m if abs(A(j,i))>abs(A(i,i)) t=A(j,:); A(j,:)=A(i,:); A(i,:)=t; end end end for i=1:m-1 for j=i+1:m x=A(j,i)/A(j,j); for k=i:m+1 A(j,k)=A(j,k)-x*A(i,k); end end end for j=m:-1:2 for i=j-1:-1:1 A(i,:)=A(i,:)-A(j,:)*(A(i,j)/A(j,j)); end end for i=1:m A(i,:)=A(i,:)/A(i,i); end disp('Rref of A is') A
In the command prompt, when I define an example matrix A and I enter my_rref(A), the resulting matrix shows A= (matrix consisting of NaNs). How can I fix this to show the values of the Gauss-Jordan elimination product?
Because you chose bad variable names, you didn't see what was causing the error. Look at how i goes with m -- they're rows. And j goes with n -- they're columns. But when you come to index the array, you swap the rows and columns:
A(j,:)=A(i,:);
See how j is the first index, which should be the rows? But you associated j and n with columns, meaning they should come in the second place, not the first index place. This code is closer but it's still messed up because of the other problem : no comments. So because of that I can't figure out what you intend, but at least with proper variable names you should be able to figure it out. So dump your code and start repairing this:
function my_rref(A) [rows, columns]=size(A); for col=1:columns-1 for row=col+1:rows if abs(A(row,col))>abs(A(col,col)) t=A(row,:); A(row,:)=A(col,:); A(col,:)=t; end end end for col=1:columns-1 for row=col+1:rows x=A(row,col)/A(row,row); for k=col:rows+1 A(row,k)=A(row,k)-x*A(col,k); end end end for row=rows:-1:2 for col=row-1:-1:1 A(col,:)=A(col,:)-A(row,:)*(A(col,row)/A(row,row)); end end for col=1:columns A(col,:)=A(col,:)/A(col,col); end disp('Rref of A is') A
Matlabsolutions.com provides guaranteed satisfaction with a
commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain
experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support
to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been
empanelled after extensive research and quality check.
Matlabsolutions.com provides undivided attention to each Matlab
assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services
include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work
done at the best price in industry.