## mukushla 3 years ago im ridiculous...i wrote this 3-4 years ago and now i cant figure it out !!!! this is for solving n linear equations with n unknowns

1. mukushla

function G =GJ(a,b) w=length(a); if size(a,1)~=size(a,2) disp('This is not a n*n system!!!'); else for j=1:w for i=1:w if a(i,j)~=0 l(j)=i; end end end end c=[a b]; for m=1:w cm=c(m,:); c(m,:)=c(l(m),:); c(l(m),:)=cm; for n=1:w if l(n)==m l(n)=l(m); end end end for i=1:w-1 for j=i+1:w cj=c(j,:); ci=c(i,:); cj=cj-(c(j,i)/c(i,i))*ci; c(j,:)=cj; end end for i=w:-1:2 for j=i-1:-1:1 cj=c(j,:); ci=c(i,:); cj=cj-(c(j,i)/c(i,i))*ci; c(j,:)=cj; end end for i=1:w x(i)=c(i,w+1)/c(i,i); end c fprintf('%f\n',x);

2. mukushla

@experimentX @phi

3. mukushla

any better ideas for that

4. experimentX

is it in matlab?

5. mukushla

yes Gauss-Jordan method

6. experimentX

Hold on ... let me run!!

7. experimentX

what type of input does function take?

8. mukushla

a=n*n matrix b=1*n matrix

9. mukushla

hold on man its not working :) i thought its right

10. experimentX

do you put symbolic values for b?

11. experimentX

OH .. hell. I've been so forgetful.

12. experimentX

Shouldn't this be easy ... since matlab stands for Matrix Laboratory still you managed to code it all. You should have done it in C perhaps ... lol. just joking.

13. mukushla

lol

14. experimentX

you might be interested in this http://projecteuler.net/

15. phi

I assume you know \ Backslash or left matrix divide. A\B is the matrix division of A into B, which is roughly the same as INV(A)*B , except it is computed in a different way. If A is an N-by-N matrix and B is a column vector with N components, or a matrix with several such columns, then X = A\B is the solution to the equation A*X = B computed by Gaussian elimination. A warning message is printed if A is badly scaled or nearly singular. A\EYE(SIZE(A)) produces the inverse of A. Just for giggles: % gaussian elimination (not sophisticated) % ASSUMES square N x N matrix % tries not to divide by 0, but does not swap rows % create a 3x3 and a 3x1 a= rand(3); b= rand(3,1); c= [a b] % create the augmented matrix N= length(a); for ii=2:N jj= ii-1; % the pivot is c(jj,jj) on the diagonal c(1,1) is the 1st pivot % the next statement creates a submatrix (using an outer product) by % the key position (the position that will be zeroed out) for each % row. All positions below the pivot will be zeroed out by the % following statement. if (abs(c(jj,jj))>1e-12) % if non-zero use this pivot x= c(ii:end,jj)/c(jj,jj) * c(jj,jj:end); c(ii:end,jj:end)= c(ii:end,jj:end) - x; end; end; % Now work backwards. First normalize the pivot position to 1 % Then zero out all entries above the pivot position for jj=N:-1:1 ii= jj-1; if (abs(c(jj,jj))>1e-12) % if non-zero use this pivot c(jj,jj:end)= c(jj,jj:end)/c(jj,jj); x= c(1:ii,jj) * c(jj,jj:end); c(1:ii,jj:end)= c(1:ii,jj:end) - x; end; end; c(:,N+1:end) a\b % compare to Matlab's gaussian