Gauss Seidel.c
Gauss Seidel.c
Gauss Seidel.c
c Page 1 of 2
Problem: Solve the boundary value problem uxx + uyy = 4x - 2y in square whose vertices are
at the points (0,0),(1,0),(1,10,(0,1) with boundary conditions u(x,0) = 2x; 0<=x<=1, u(x,
1) = 2x-1; 0<=x<=1, u(0,y) + ux|(x=0) = 2-y; 0<=y<=1, u(1-y) = 2-y; 0<=y<=1.
#include<stdio.h>
#include<math.h>
int main()
{
int i,j,s,l=0,k,h;
float o,e,n,m,x[20],y[20],u1[200][200],u[200][200],min=1;
o=0;
e=1;
h=10;
k=10;
m=(e-o)/h;
n=(e-o)/k;
// assume the initial condition
for(i=0;i<=h;i++)
{
for(j=0;j<=k;j++)
u[i][j]=0;
}
x[0]=o;
y[0]=o;
for(i=1;i<=h;i++)
x[i]=m*i;
for(i=1;i<=k;i++)
y[i]=n*i;
for(i=0;i<=h;i++)
{
u[i][0]=2*x[i];
u[i][k]=2*x[i]-1;
}
for(i=0;i<=h;i++)
u[h][i]=2-y[i];
do
{
for(i=1;i<h;i++)
{
for(j=1;j<k;j++)
{
u[0][j]=(((2-y[j])*h)-u[1][j])/(h-1);
u1[i][j]=(u[i+1][j]+u[i-1][j]+u[i][j-1]+u[i]
[j+1])/4;
if(min>fabs(u1[i][j]-u[i][j]))
min=fabs(u1[i][j]-u[i][j]);
u[i][j]=u1[i][j];
}
}
}while(min>0.00001);
printf("The solution is \n");
for(i=0;i<=h;i++)
{
for(j=0;j<=k;j++)
{
printf("u(%d,%d)=%4.3f\t",i,j,u[i][j]);
}
printf("\n\n");
}
}
File: /home/pc-122/ARPAN/Gauss_Seidel.c Page 2 of 2
/* OUTPUT
The solution is
u(0,0)=0.000 u(0,1)=1.988 u(0,2)=1.847 u(0,3)=1.730 u(0,4)=1.622
u(0,5)=1.520 u(0,6)=1.422 u(0,7)=1.330 u(0,8)=1.247 u(0,9)=1.188
u(0,10)=-1.000