Hi. It's actually not a C++ code, it's a matlab code. But I hope there's somebody here who can help me. There's not really an appropriate forum here for a matlab code.

It's about Gaussian Elimination

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

I already have a code that only needs to be modified.

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

As you can see in the picture, there are 17 equations. The code will do it, but there's an exception. A couple of rows aren't in the proper format.

Like row 4.

F3-10=0 and the code would have to move the 10 -> F3=10

Row 16.

F13+F15-10=0 would become -> F13+F15=10

You simply have to move the numbers to the right. They will become part of b because the formula is Ax=b ( b is to the right, so the number would now be part of b).

I don't know how to modify the code that way that the code will recognize this and move the numbers to the right. I was hoping that someone would know how to do this. Help is really appreciated! Thank you

Recommended Answers

All 3 Replies

You do know that, in matlab, all you need to do to solve a linear system is to write x = A \ b; where b is a column-vector and A is a square matrix. That's it, you don't need to use the function you posted (as I assume you didn't write it, so it means that you are allowed to use whatever you like). As for turning the equations you posted into a standard linear system formulation, well that's the question of the assignment, nobody's going to do that for you.

No, it's not what I have to do. But thanks for the "help".

Thread closed. Please follow discussion here.

@Lemonader please do not multi post same question in different forum sections i the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.