Code for Gaussian Elimination

In summary: The rest of the code should work as is and you should be able to get the correct solution for your system of equations. In summary, to make this code work for your system of equations, you need to reformat the equations into the standard form Ax=b and then input the A matrix and b vector into the code.
  • #1
XodoX
203
0
I have this code for a Gaussian Elimination:

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;
endX'

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

As you can see, the input is A and b. I'm trying to figure out how I can make this code work for this Gaussian Elimination:

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

It's 17 rows. The code can do 17 rows. But some of the equations aren't in the correct format. Like line 4: F3-10=0 will has to be F3=10. You need to move the 10. You have to move it to the right side to make it part of your b. A is the left side. It's only a few lines, the rest remains 0. You just type and define A as your 17 by 17 matrix inside the code and your B as the B that you ended up with.
Does anybody know how I make this work with this code? I'm not sure what I have to exactly change here! That's a MATLAB code, but it's not much different.
 
Technology news on Phys.org
  • #2
You need to reformat the equations into the standard form Ax=b. This means you will have to move all constants to the right side of the equation and set them equal to 0. For example, for line 4, you would have F3-10=0 which would become F3=10. Once you have all of your equations in the correct format, you can enter the A matrix and b vector into the code.
 

Related to Code for Gaussian Elimination

1. What is Gaussian Elimination?

Gaussian Elimination is a method used to solve systems of linear equations by transforming the system into an equivalent triangular matrix and then solving for the variables by back substitution.

2. Why is Gaussian Elimination important in coding?

Gaussian Elimination is important in coding because it is a widely used algorithm for solving systems of linear equations, which are commonly used in scientific and engineering applications. It is also the basis for other important algorithms, such as LU decomposition and matrix inversion.

3. How does Gaussian Elimination work?

Gaussian Elimination works by using elementary row operations to transform a system of linear equations into an equivalent triangular system. These operations include multiplying a row by a constant, adding one row to another, and swapping rows. Once the system is in triangular form, the variables can be solved for by back substitution.

4. Can Gaussian Elimination be used for matrices of any size?

Yes, Gaussian Elimination can be used for matrices of any size. However, as the size of the matrix increases, the computational complexity also increases. This means that for large matrices, other algorithms may be more efficient.

5. Are there any limitations to Gaussian Elimination?

One limitation of Gaussian Elimination is that it may fail if the matrix is ill-conditioned, meaning that the system of equations is very sensitive to changes in the input values. In addition, Gaussian Elimination may also fail if the matrix is singular, meaning that it does not have an inverse. In these cases, other methods such as LU decomposition or matrix inversion may be more suitable.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
800
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
978
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
938
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
34
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top