MATLAB Code for solving linear system with gaussian style

Status
Not open for further replies.

azerturk

Newbie level 6
Joined
Nov 11, 2006
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Azerbaijan-Tebriz
Activity points
1,359
I'm new 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
 

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
but the answer is not
x =
9
-36
30
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…