- #1
XodoX
- 203
- 0
I have this code for a Gaussian Elimination:
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.
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.