1,105,380 Community Members

Gaussian Elimination/ Matlab code

Member Avatar
Lemonader
Newbie Poster
19 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I was wondering if someone can help me with this.

http://img189.imageshack.us/i/50468910.png/

I'm looking for the matlab code for this one. Wikipedia has an example:

i := 1
j := 1
while (i ≤ m and j ≤ n) do
  Find pivot in column j, starting in row i:
  maxi := i
  for k := i+1 to m do
    if abs(A[k,j]) > abs(A[maxi,j]) then
      maxi := k
    end if
  end for
  if A[maxi,j] ≠ 0 then
    swap rows i and maxi, but do not change the value of i
    Now A[i,j] will contain the old value of A[maxi,j].
    divide each entry in row i by A[i,j]
    Now A[i,j] will have the value 1.
    for u := i+1 to m do
      subtract A[u,j] * row i from row u
      Now A[u,j] will be 0, since A[u,j] - A[i,j] * A[u,j] = A[u,j] - 1 * A[u,j] = 0.
    end for
    i := i + 1
  end if
  j := j + 1
end while

I want to say this is sufficient for the example in the picture. Does anybody know for sure?

Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

Is your assignment to write the solver or simply to solve that set of equations?

If it's the latter, just use http://www.mathworks.com/help/techdoc/ref/rref.html, it's essentially doing the same thing by a slightly different method.

Member Avatar
Lemonader
Newbie Poster
19 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi!

I think this would work:

clear all
close all
%A=[1,1,1;1,-2,2;1,2,-1]
%B=[0;4;2];

A=[2,1,-1,4,2,5;-3,-1,2,5,9,4;-2,1,2,6,6,3;1,2,3,5,4,6;8,9,4,4,1,3;2,3,5,1,9,7]
B=[8;-11;-3;5;7;8]

%A=[1,0,0;1,0,0;1,0,0]
%B=[1;1;1];

% SET UP COUNTERS
COUNTER_AMS=0
COUNTER_DIVISION=0

% GET SIZE OF MATRIX
N=size(A,1);

for k=1:N-1
	i_test=k;
	CONDITION=0;
	SINGULAR_MATRIX=0;
	
	while CONDITION==0
		if (A(i_test,k)~=0)
				CONDITION=1;
				i=i_test;
		else
			i_test=i_test+1;
			COUNTER_AMS=COUNTER_AMS+1;
		end
		
		if i_test>N
			'ERROR MATRIX NON SINGULAR'
			pause
			%exit
		end
	end
	
	% PERMUTE RAW IF i different then k
	if i~=k
		TEMP=A(i,:);
		A(i,:)=A(k,:);
		A(k,:)=TEMP;
		
		TEMP=B(i);
		B(i)=B(k);
		B(k)=TEMP;
	end
	
	
	for j=k+1:N
		p_jk=A(j,k)/A(k,k);
		COUNTER_DIVISION=COUNTER_DIVISION+1;
		
		A(j,:)=A(j,:)-p_jk*A(k,:);
		B(j)=B(j)-p_jk*B(k);
		COUNTER_AMS=COUNTER_AMS+4;
	end
end

if A(N,N)==0
	'MATRIX SINGULAR'
	pause
	%exit
end


% ALGORITHM OF BACKWARD SUBSTITUTION
X(N)=B(N)/A(N,N);
COUNTER_DIVISION=COUNTER_DIVISION+1;

for i=N-1:-1:1
		SUM=0;
		for j=i+1:N
			SUM=SUM+A(i,j)*X(j);
			COUNTER_AMS=COUNTER_AMS+2;
		end
		X(i)=(B(i)-SUM)/A(i,i);
		COUNTER_AMS=COUNTER_AMS+1;
		COUNTER_DIVISION=COUNTER_DIVISION+1;
end


X'

COUNTER_AMS
COUNTER_DIVISION
N*(N+1)/2
N^3+N^2-5*N/6

This would work for a matrix, but not for this matrix in the picture. Because equations aren't provided in the proper format. Line 4 has : F3-10=0. So the code would have to move the 10 so that it will be F3=10
Do you know how I could do this? There's another equation where it has to move the 10 to the right.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article