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

NEWTON-RAPSON METHOD-8th degree Legendre polynomial

## Newton-Rapson Method to the smallest non negative root
## of the 8th degree Legendre Polynomial
## P8(x)=(1/128)(6435x^8-12012x^6+6930x^4-1260x^2+35)
## where -1<=x<=1.
## for the smallest non negative root, we can ignore
## all the terms except the last two by truncated
## the function to be zero and find
## x=0.167 as the initial smallest non negative
## root.
##Constants and initializations
x=[]; ## Empty array for the iterated x roots
x(1)=0.16700000; ## Initial guess to begin the iteration for the
## smallest non-negative root.
L8=[]; ## Empty array for the Legendre polynomial
L8p=[]; ## Empty array for the derivative of the Legendre polynomial
for i=1:100
##The value of the function at x
L8(i)=(1/128)*(6435*x(i)^8-12012*x(i)^6+6930*x(i)^4-1260*x(i)^2+35);
##The value of the derivative of the function at x
L8p(i)=(1/128)*(6435*8*x(i)^7-12012*6*x(i)^5+6930*4*x(i)^3-1260*2*x(i));
x(i+1)=x(i)-L8(i)/L8p(i); ## the iteration
endfor
## For plot let's define a new variable…

Second Harmonic Generation

gnuplot> set xrange [-180:180] gnuplot> set yrange [-180:180] gnuplot> set pm3d gnuplot> set hidden3d  gnuplot> set title 'SHG' gnuplot> splot sin(cos(x*pi/180))*sin(cos(x*pi/180))/(cos(x*pi/180)*cos(x*pi/180))*sin(cos(y*pi/180))*sin(cos(y*pi/180))/(cos(y*pi/180)*cos(y*pi/180)) title 'N=1
'