Skip to main content

JACOBI AND GAUSS-SIEDEL SCHEMES


##The formulas and the iteration procedure
##below are taken from the Devies Book
##Eqn 5.89
##Constants and Initializations
dx=1E-3; ##Increment in x.
c1=1-2.5*dx; ## c1,c2,c3,c4 are constants in Eqn.5.89
c2=1+2.5*dx;
c3=-10*dx*dx;
c4=1/(2+c3);
## Set up the initial profile
x=0.0:0.01:1.0;
yold=0:100;
hold on
plot(x,yold);
xlabel('x');
ylabel('y');
##Impose the boundary conditions
Nsteps=1000;
yoldJac=yold; ## old y values for Jacobi Iteration Scheme
yoldGauSied=yold; ## old y values for Gauss-Siedel Scheme
ynewJac=yoldJac; ## new y values for Jacobi Iteration Scheme
ynewGauSied=yoldGauSied; ## new y values for Gauss-Siedel Scheme
for n=1:Nsteps
for i=2:100
ynewJac(i)=c4*(c1*yoldJac(i+1)+c2*yoldJac(i-1)+c3*x(i));
ynewGauSied(i)=c4*(c1*yoldGauSied(i+1)+c2*ynewGauSied(i-1)+c3*x(i));
endfor
yoldJac=ynewJac;
yoldGauSied=ynewGauSied;
endfor
plot(x,ynewJac,'g*',x,ynewGauSied,'m@');
title('y vs x');
xlabel('x');
ylabel('y');
legend('y(initial)','Jacobi','Gauss-Siedel');
axis([0,1.5]);
hold off
save -text JACOBIGAUSSSIEDEL.dat
print ('-dpsc','JACOBIGAUSSSIEDEL.ps')

Comments

Popular posts from this blog

Simple Euler Method

##Usage:Call Octave from terminal ##and then call EulerMethodUmitAlkus.m ##from octave and finally ##press enter. That's all. ##Simple Euler Method ##Constants and initializations x=[]; ## initial empty vector for x y=[]; ## initial empty vector for y x(1)=1; ## initial value of x y(1)=1; ## initial value of y h=1E-3; ## increment in x dery=[]; ## 1st derivative of y wrt x n=1; ## inital loop index for while ## enter the while loop for the interval x=[1,2] while (x(n)<=2) x(n+1)=x(n)+h; dery(n+1)=x(n)*x(n)-2*y(n)/x(n); ##given y(n+1)=y(n)+h*dery(n); ##Euler method n++; endwhile ##exit from the 1st while loop ##Modified Euler Method ##Constant and initializations ymid=[]; ## empty vector function evaluated at x midpoint xmid=[]; ## empty vector func. of midpoints in x ymid(1)=1; ## inital value for ymid. derymid=[]; ## derivative of y at midpoints ##Enter the 2nd while loop n=1; while (x(n)<=2) xmid(n)=x(n)+h/2; derymid(n)=xmid(n)*xmid(n)-2*ymid(...