Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

/*

*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/

POISSON EQUATION FINITE-DIFFERENCE ALGORITHM 12.1


To approximate the solution to the Poisson equation
DEL(u) = F(x,y), a <= x <= b, c <= y <= d,
SUBJECT TO BOUNDARY CONDITIONS:
u(x,y) = G(x,y),
if x = a or x = b for c <= y <= d,
if y = c or y = d for a <= x <= b
INPUT: endpoints a, b, c, d; integers m, n; tolerance TOL;
maximum number of iterations M
OUTPUT: approximations W(I,J) to u(X(I),Y(J)) for each
I = 1,..., n-1 and J=1,..., m-1 or a message that the
maximum number of iterations was exceeded.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define true 1
#define false 0
double F(double X, double Y);
double G(double X, double Y);
double G1(double X, double Y, int K1);
double absval(double);
void INPUT(int *, double *, double *, double *, double *, int *, int *, double *, int *);
void OUTPUT(int, int, double *, double *, double [][26], int);
main()
{
double W[26][26], X[26], Y[26];
double TOL,A,B,C,D,H,K,V,VV,Z,E,ALPHA,BETA;
double w1=1.0,E0=0.1,rho=0.95;
int M,N,NN,M1,M2,N1,N2,I,J,L,LL,OK;
INPUT(&OK, &A, &B, &C, &D, &M, &N, &TOL, &NN);
if (OK) {
M1 = M - 1;
M2 = M - 2;

N1 = N - 1;
N2 = N - 2;
/* STEP 1 */
H = (B - A) / N;
K = (D - C) / M;
/* STEPS 2 and 3 construct mesh points */
/* STEP 2 */
for (I=0; I<=N; I++) X[I] = A + I * H;
/* STEP 3 */
for (J=0; J<=M; J++) Y[J] = C + J * K;
/* STEP 4 */
for (I=1; I<=N1; I++) {
W[I][0] = G1(X[I],Y[0],1);
W[I][M] = G1(X[I],Y[M],2);
}
for (J=0; J<=M; J++) {
W[0][J] = G1(X[0],Y[J],3);
W[N][J] = G1(X[N],Y[J],4);
}
for (I=1; I<=N1; I++)
for (J=1; J<=M1; J++) W[I][J] = 0.0;
/* STEP 5 */
/* use V for lambda, VV for mu */
V = H * H / ( K * K );
VV = 2.0 * ( 1.0 + V );
L = 1;
OK = false;
/* Z is a new value of W(I,J) to be used in computing
the norm of the error E used in place of NORM */
/* STEP 6 */
while ((L <= NN) && (!OK)) {
/* STEPS 7 through 20 perform Gauss-Seidel iterations */
/* STEP 7 */
Z = (-H*H*F(X[1],Y[M1])+G1(A,Y[M1],3)+V*
G1(X[1],D,2)+V*W[1][M2]+W[2][M1])/VV;
E = absval( W[1][M1] - Z );
W[1][M1] = (1.-w1)*W[1][M1]+w1*Z;
/* STEP 8 */
for (I=2; I<=N2; I++) {
Z = (-H*H*F(X[I],Y[M1])+V*G1(X[I],D,2)+
W[I-1][M1]+W[I+1][M1]+V*W[I][M2])/VV;
if ( absval( W[I][M1] - Z ) > E )

E = abs( W[I][M1] - Z );
W[I][M1] =(1.-w1)*W[I][M1]+w1*Z;
}
/* STEP 9 */
Z = (-H*H*F(X[N1],Y[M1])+G1(B,Y[M1],4)+V*
G1(X[N1],D,2)+W[N2][M1]+V*W[N1][M2])/VV;
if ( absval( W[N1][M1] - Z ) > E )
E = abs( W[N1][M1] - Z );
W[N1][M1]=(1.-w1)* W[N1][M1]+w1*Z;
/* STEP 10 */
for (LL=2; LL<=M2; LL++) {
J = M2 - LL + 2;
/* STEP 11 */
Z = (-H*H*F(X[1],Y[J])+G1(A,Y[J],3)+
V*W[1][J+1]+V*W[1][J-1]+W[2][J])/VV;
if ( absval( W[1][J] - Z ) > E )
E = absval( W[1][J] - Z );
W[1][J]=(1.-w1)*W[1][J]+w1*Z;
/* STEP 12 */
for (I=2; I<=N2; I++) {
Z = (-H*H*F(X[I],Y[J])+W[I-1][J]+
V*W[I][J+1]+V*W[I][J-1]+W[I+1][J])/VV;
if ( absval( W[I][J] - Z ) > E )
E = absval( W[I][J] - Z );
W[I][J]=(1.-w1)*W[I][J]+w1*Z;
}
/* STEP 13 */
Z = (-H*H*F(X[N1],Y[J])+G1(B,Y[J],4)+
W[N2][J]+V*W[N1][J+1]+V*W[N1][J-1])/VV;
if ( absval( W[N1][J] - Z ) > E )
E = absval( W[N1][J] - Z );
W[N1][J]=(1.-w1)*W[N1][J]+w1*Z;
}
/* STEP 14 */
Z = ( -H * H * F( X[1],Y[1] ) + V * G1( X[1], C,1 ) +
G1( A, Y[1], 3) + V * W[1][2] + W[2][1] ) / VV;
if ( absval( W[1][1] - Z ) > E )
E = absval( W[1][1] - Z );
W[1][1]=(1.-w1)*W[1][1]+w1*Z;
/* STEP 15 */
for (I=2; I<=N2; I++) {
Z = (-H*H*F(X[I],Y[1])+V*G1(X[I],C,1)+

W[I+1][1]+W[I-1][1]+V*W[I][2])/VV;
if ( absval( W[I][1] - Z ) > E )
E = absval( W[I][1] - Z );
W[I][1]=(1.-w1)*W[I][1]+w1*Z;
}
/* STEP 16 */
Z = (-H*H*F(X[N1],Y[1])+V*G1(X[N1],C,1)+
G1(B,Y[1],4)+W[N2][1]+V*W[N1][2])/VV;
if ( absval( W[N1][1] - Z ) > E )
E = absval( W[N1][1] - Z );
W[N1][1]=(1.-w1)*W[N1][1]+w1*Z;
/* STEP 17 */
if((E/E0)<=1.)
{
rho=E/E0;
w1=2./(1+sqrt(1.-rho*rho));
printf("Error=%e, rho=%e, w=%e\n",E,rho,w1);
}
E0=E;
if (E<=TOL) {
/* STEP 18 */
printf("w=%e\n",w1);
OUTPUT(N1, M1, X, Y, W, L);
/* STEP 19 */
OK = true;
}
else
/* STEP 20 */
L++;
}
/* STEP 21 */
if (L > NN)
printf("Method fails after iteration number %d\n", NN);
}
system("pause");
return 0;
}
/* Change F for a new problem */
double F(double X, double Y)

{
double f;
f = 4.;
return f;
}
/* Change G for a new problem */
double G1(double X, double Y, int K1)
{
double g;
switch(K1)
{ case 1:
g = (X-0.)*(X-0.);
return g;
break;
case 2:
g = (X-2.)*(X-2.);
return g;
break;
case 3:
g = (Y-0.)*(Y-0.);
return g;
break;

case 4:
g = (Y-1.)*(Y-1.);
return g;
break;}
}
void INPUT(int *OK, double *A, double *B, double *C, double *D, int *M, int *N, double *TOL, int
*NN)
{
int I, FLAG;
char AA;
char NAME[30];
FILE *INP;

printf("This is the Finite-Difference Method for Elliptic Equations.\n");


*OK = false;
printf("Have the functions F(x,y) and G(x,y) been created\n");
printf("immediately preceding the INPUT procedure? Answer Y or N.\n");
scanf("\n%c", &AA);
if ((AA == 'Y') || (AA == 'y')) {
*OK =false;
while (!(*OK)) {
printf("Input endpoints of interval [A,B] on X-axis\n");
printf("separated by a blank.\n");
scanf("%lf %lf", A, B);
printf("Input endpoints of interval [C,D] on Y-axis\n");
printf("separated by a blank.\n");
scanf("%lf %lf", C, D);
if ((*A >= *B) || (*C >= *D))
printf("Left endpoint must be less than right endpoint.\n");
else *OK = true;
}
*OK = false;
while (!(*OK)) {
printf("Input number of intervals n on the X-axis and m\n");
printf("on the Y-axis separated by a blank\n");
printf("Note that both n and m should be larger than 2.\n");
scanf("%d %d", N, M);
if ((*M <= 2) || (*N <= 2))
printf("Numbers must exceed 2.\n");
else *OK = true;
}
*OK = false;
while (!(*OK)) {
printf("Input the Tolerance.\n");
scanf("%lf", TOL);
if (*TOL <= 0.0)
printf("Tolerance must be positive.\n");
else *OK = true;
}
*OK = false;
while (!(*OK)) {
printf("Input the maximum number of iterations.\n");
scanf("%d", NN);
if (*NN <= 0)

printf ("Number must be a positive integer.\n");


else *OK = true;
}
}
else {
printf("The program will end so that the functions ");
printf("F and G can be created.\n");
*OK = false;
}
}
void OUTPUT(int N1, int M1, double *X, double *Y, double W[][26], int L)
{
int I, J, FLAG;
char NAME[30];
FILE *OUP;
printf("Choice of output method:\n");
printf("1. Output to screen\n");
printf("2. Output to text file\n");
printf("Please enter 1 or 2.\n");
scanf("%d", &FLAG);
if (FLAG == 2) {
printf("Input the file name in the form - drive:name.ext\n");
printf("for example: A:OUTPUT.DTA\n");
scanf("%s", NAME);
OUP = fopen(NAME, "w");
}
else OUP = stdout;
fprintf(OUP, "POISSON EQUATION FINITE-DIFFERENCE METHOD\n\n");
fprintf(OUP, " I J
X(I)
Y(J)
W(I,J)\n\n");
for (I=1; I<=N1; I++)
for (J=1; J<=M1; J++)
fprintf(OUP, "%3d %2d %11.8f %11.8f %13.8f\n", I, J, X[I], Y[J], W[I][J]);
fprintf(OUP, "Convergence occurred on iteration number: %d\n", L);
fclose(OUP);
}
/* Absolute Value Function */
double absval(double val)
{
if (val >= 0) return val;

else return -val;


}

/*
*
*
*
*
*
*
*
*
*
*
*
*
*/

HEAT EQUATION BACKWARD-DIFFERENCE ALGORITHM 12.2


To approximate the solution to the parabolic partial-differential
equation subject to the boundary conditions
u(0,t) = u(l,t) = 0, 0 < t < T = max t,
and the initial conditions
u(x,0) = F(x), 0 <= x <= l:
INPUT: endpoint l; maximum time T; constant ALPHA; integers m, N.
OUTPUT: approximations W(I,J) to u(x(I),t(J)) for each
I = 1, ..., m-1 and J = 1, ..., N.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define pi 4*atan(1)
#define true 1
#define false 0
double F(double X);
double F1(double X);
void INPUT(int *, double *, double *, double *, int *, int *);
void OUTPUT(double, double, int, double *, double);
main()
{
double W[25], L[25], U[25], Z[25];
double FT,FX,ALPHA,H,K,VV,T,X;
int N,M,M1,M2,N1,FLAG,I1,I,J,OK;
INPUT(&OK, &FX, &FT, &ALPHA, &N, &M);
if (OK) {
M1 = M - 1;
M2 = M - 2;
N1 = N - 1;
/* STEP 1 */
H = FX / M;
K = FT / N;

VV = ALPHA * ALPHA * K / ( H * H );
/* STEP 2 */
for (I=1; I<=M1; I++) W[I-1] = F( I * H );
/* STEP 3 */
/* STEPS 3 through 11 solve a tridiagonal linear system
using Algorithm 6.7 */
L[0] = 1.0 + 2.0 * VV;
U[0] = -VV / L[0];
/* STEP 4 */
for (I=2; I<=M2; I++) {
L[I-1] = 1.0 + 2.0 * VV + VV * U[I-2];
U[I-1] = -VV / L[I-1];
}
/* STEP 5 */
L[M1-1] = 1.0 + 2.0 * VV + VV * U[M2-1];
/* STEP 6 */
for (J=1; J<=N; J++) {
//chen + source + BC
for (I=1;I<=M1;I++)
W[I-1] = W[I-1]+ K*F1( I * H );
//W[M1-1]=W[M1-1]+VV*100. ;
//
/* STEP 7 */
/* current t(j) */
T = J * K;
Z[0] = W[0] / L[0];
/* STEP 8 */
for (I=2; I<=M1; I++)
Z[I-1] = ( W[I-1] + VV * Z[I-2] ) / L[I-1];
/* STEP 9 */
W[M1-1] = Z[M1-1];
/* STEP 10 */
for (I1=1; I1<=M2; I1++) {
I = M2 - I1 + 1;
W[I-1] = Z[I-1] - U[I-1] * W[I];
}
}
/* STEP 11 */
OUTPUT(FT, X, M1, W, H);
}
/* STEP 12 */

system("pause");
return 0;
}
/* Change F for a new problem */
double F(double X)
{
double f;
double l=1.5;
// f = 0;
f =sin(pi * X/l) ;
return f;
}
/* Change F for a new problem */
double F1(double X)
{
double f;
double l=1.5 ,K=1.04,r=5.0,c=.056,rho=10.6;
f=r/(c*rho);
//f=l*K/(r*rho);
// f = 2.;
// f = sin(pi * X);
return f;
}
void INPUT(int *OK, double *FX, double *FT, double *ALPHA, int *N, int *M)
{
int FLAG;
char AA;
printf("This is the Backward-Difference Method for Heat Equation.\n");
printf("Has the function F been created immediately\n");
printf("preceding the INPUT procedure? Answer Y or N.\n");
scanf("\n%c", &AA);
if ((AA == 'Y') || (AA == 'y')) {
printf("The lefthand endpoint on the X-axis is 0.\n");
*OK =false;
while (!(*OK)) {
printf("Input the righthand endpoint on the X-axis.\n");
scanf("%lf", FX);
if (*FX <= 0.0)

printf("Must be positive number.\n");


else *OK = true;
}
*OK = false;
while (!(*OK)) {
printf("Input the maximum value of the time variable T.\n");
scanf("%lf", FT);
if (*FT <= 0.0)
printf("Must be positive number.\n");
else *OK = true;
}
printf("Input the constant alpha.\n");
scanf("%lf", ALPHA);
*OK = false;
while (!(*OK)) {
printf("Input integer m = number of intervals on X-axis\n");
printf("and N = number of time intervals - separated by a blank.\n");
printf("Note that m must be 3 or larger.\n");
scanf("%d %d", M, N);
if ((*M <= 2) || (*N <= 0))
printf("Numbers are not within correct range.\n");
else *OK = true;
}
}
else {
printf("The program will end so that the function F can be created.\n");
*OK = false;
}
}
void OUTPUT(double FT, double X, int M1, double *W, double H)
{
int I, J, FLAG;
char NAME[30];
FILE *OUP;
printf("Choice of output method:\n");
printf("1. Output to screen\n");
printf("2. Output to text file\n");
printf("Please enter 1 or 2.\n");
scanf("%d", &FLAG);
if (FLAG == 2) {

printf("Input the file name in the form - drive:name.ext\n");


printf("for example: A:OUTPUT.DTA\n");
scanf("%s", NAME);
OUP = fopen(NAME, "w");
}
else OUP = stdout;
fprintf(OUP, "THIS IS THE BACKWARD-DIFFERENCE METHOD\n\n");
fprintf(OUP, " I
X(I) W(X(I),%12.6e)\n", FT);
for (I=1; I<=M1; I++) {
X = I * H;
fprintf(OUP, "%3d %11.8f %14.8f\n", I, X, W[I-1]);
}
fclose(OUP);
}

You might also like