Continue to Site

# MATLAB Code for solving linear system with gaussian style

Status
Not open for further replies.

#### azerturk

##### Newbie level 6
It's my first post here & I don't know much about the rules.plz help me
I need a MATLAB Code(program in the form of .m.file) for solving linear system(equations in the form of matrix) with gaussian style
I only have time for about 2 days
best regards

#### carpa

##### Member level 3
if you need to solve equation like "R*x=b" you may
used matlab function "inv" such as "x = inv(R) * b", where R is a square matrix, x and b are both vectors.

#### azerturk

##### Newbie level 6
Re: MATLAB Code for solving linear system with gaussian styl

carpa said:
if you need to solve equation like "R*x=b" you may
used matlab function "inv" such as "x = inv(R) * b", where R is a square matrix, x and b are both vectors.
Thanks but it's not 1 equation & this system has n equations with n variables
so the program shoud ask for 'n' and use n×n matrix & we could only use gauss style

#### jakjoud

##### Full Member level 2
First which kind of matrix you have (which form they have?)?
Second carpa is right 'caus your system must be written in a matricial form then you'll have R*x=b with R a nxn matrix, x and b are vectors containing each one n eleents.
But if n is very large the inv will take a long time to be executed. So you can use another method just give us the exact problem we can probably help you!
There is a c code for such inversion of matrix I did already upload it at

good luck

#### azerturk

##### Newbie level 6
thanks but the problem is not solving the system
we shoud show the use of gause style in solvig the equations
in the other words we shoud write the algorithm with the help of gause style

#### jakjoud

##### Full Member level 2
Re: MATLAB Code for solving linear system with gaussian styl

Ok so here is the algorithm I write it by myself I hope it can help you.

### azerturk

Points: 2

#### azerturk

##### Newbie level 6
thanks alot that's the point but how could we write it in MATLAM (as a m.file) or in C++

#### jakjoud

##### Full Member level 2
Re: MATLAB Code for solving linear system with gaussian styl

Ok again Thes is just made by myself it means that you probably have to verify by yourself.

% The matrix must become on a triangle forme
for (i=0 :n)
r(i,n+1)=y(i) ;
end ;

for (k=1:n-1)
for (i=k+1:n+1)
if (i==k+1)
for (j=k+1:n+1)
r(i,j)=r(I,j)/r(I,i);
end
else
r(i,j)=r(i,j)-r(i,k+1)*r(k+1,j);
end;
end;
end;
% Definition of the second term of problem
for i=1:n,
z(i)=r(i,n+1);
end;
%Solution
for j=1:n
x(j)=z(j);
for k=j+1:n
x(j)=x(j)-r(j,k)*x(k)
end;
end;

### azerturk

Points: 2

#### azerturk

##### Newbie level 6
great .If it's possible plz post me the file that I could run by MATLAB (m file) because I'm not that much familiar with this program

#### jakjoud

##### Full Member level 2
It's not possible because of the extension.
You must first define in the m file you matrix R, the vector y. then you copy the code you past I just did it under Matlab so it must not contains a lot of errors. Just try and we are always here

#### azerturk

##### Newbie level 6
OK I'll try .I forgat sth :fist of all the algorithm needs pivoting

#### jakjoud

##### Full Member level 2
In fact the gauss method is called pivoting one. Because it's about taking the first element of everyline as a pivot and devide the other elements by it. it's a simple principle you just try to make every element under the diagoal null. and when you rewrite your problem the last element of the new y is equal to last element of x. It's more clear in the first doc file

#### azerturk

##### Newbie level 6
I made the m file with the lines below with some edition but it is not giving the right answer

% Method of Gauss for solving linear system: rx=y
n=input('enter the number n for matrix n*n >')
r=input('enter the matrix n*n for r >')
y=input('enter the matrix n*1 for y >')
% The matrix must become on a triangle form
for(i=1:n)
r(i,n+1)=y(i);
end

for(k=0:n-1)
for(i=k+1:n)
if(i==k+1)
for(j=k+1:n+1)
r(i,j)=r(k+1,j)/r(k+1,k+1);
end
else
for(j=k+1:n+1)
r(i,j)=r(i,j)-r(i,k+1)*r(k+1,j);
end
end;
end;
end;
% Definition of the second term of problem
for(i=1:n),
z(i)=r(i,n+1);
end;
%Solution
for(j=n:-1:1)
x(j)=z(j);
for (k=n:-1:j+1)
x(j)=z(j)-r(j,k)*x(k);
end;
end;
x'

I tried with :
r =
1.0000 0.5000 0.3333
0.5000 0.3333 0.2500
0.3333 0.2500 0.2000
y =
1
0
0
x =
9
-36
30

Added after 2 hours 45 minutes:

plz help

Status
Not open for further replies.