a banerjee
08-24-2008, 08:37 AM
Hi,
I am trying to solve the following 2D Poisson equation using the mglin function from numerical recipe in C (2nd Edition).
(d^2/dx^2 + d^2/dy^2)u = f
where f = -2*x*(1-x) - 2*y*(1-y)
& 0 < x,y < 2
Boundary conditions:
u = 0 for x=0, x=2, y=0, y=2
Following is my code:
-----------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
#include "nrutil.c"
#include "nrutil.h"
#include "mglin.c"
#include "relax.c"
#include "slvsml.c"
#include "rstrct.c"
#include "copy.c"
#include "interp.c"
#include "fill0.c"
#include "resid.c"
#include "addint.c"
main()
{
int m = 1025;
double h = 2.0/(double)(m-1);
double **u;
u = dmatrix(1, m, 1, m);
int maxcyc = 2;
int i,j;
double x,y;
/*............Initializing u[i][j] = rhs[i][j].............*/
for(i=1;i<=m;i++)
for(j=1;j<=m;j++)
{
x = h*(i-1);
y = h*(j-1);
u[i][j] = -2*x*(1-x) -2*y*(1-y);
}
/************************************************** **********/
/*............Calling the mglin routine to solve the 2D PDE.....*/
mglin(u, m, maxcyc);
/************************************************** *****************/
}
----------------------------------------------------------------------
Changes made in slvsml.c, resid.c,relax.c:
1. slvsml.c :
In line 2: double h = 0.5 is replaced by h = 1.0
2. resid.c:
In line 3: double h = 1.0/(n-1) is replaced by 2.0/(n-1)
3. relax.c:
In line 3: double h = 1.0/(n-1) is replaced by 2.0/(n-1)
--------------------------------END--------------------------------------------
However, this does not give me the result which is x*(2-x)*y*(2-y)
Do you see where my mistake is?
Please let me know.
Thanks,
Arunima.
I am trying to solve the following 2D Poisson equation using the mglin function from numerical recipe in C (2nd Edition).
(d^2/dx^2 + d^2/dy^2)u = f
where f = -2*x*(1-x) - 2*y*(1-y)
& 0 < x,y < 2
Boundary conditions:
u = 0 for x=0, x=2, y=0, y=2
Following is my code:
-----------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
#include "nrutil.c"
#include "nrutil.h"
#include "mglin.c"
#include "relax.c"
#include "slvsml.c"
#include "rstrct.c"
#include "copy.c"
#include "interp.c"
#include "fill0.c"
#include "resid.c"
#include "addint.c"
main()
{
int m = 1025;
double h = 2.0/(double)(m-1);
double **u;
u = dmatrix(1, m, 1, m);
int maxcyc = 2;
int i,j;
double x,y;
/*............Initializing u[i][j] = rhs[i][j].............*/
for(i=1;i<=m;i++)
for(j=1;j<=m;j++)
{
x = h*(i-1);
y = h*(j-1);
u[i][j] = -2*x*(1-x) -2*y*(1-y);
}
/************************************************** **********/
/*............Calling the mglin routine to solve the 2D PDE.....*/
mglin(u, m, maxcyc);
/************************************************** *****************/
}
----------------------------------------------------------------------
Changes made in slvsml.c, resid.c,relax.c:
1. slvsml.c :
In line 2: double h = 0.5 is replaced by h = 1.0
2. resid.c:
In line 3: double h = 1.0/(n-1) is replaced by 2.0/(n-1)
3. relax.c:
In line 3: double h = 1.0/(n-1) is replaced by 2.0/(n-1)
--------------------------------END--------------------------------------------
However, this does not give me the result which is x*(2-x)*y*(2-y)
Do you see where my mistake is?
Please let me know.
Thanks,
Arunima.