#include <iostream>
#include <math.h>
using namespace std;
const double b = 1;
const int n = 20;
const double pi=3.141592;
double f(double x, double y) { return 2; }
double u_exact(double x, double y) { return x*x; }
double g0(double x) { return 0.; }
double g1(double y) { return 1.0); }
double g2(double x) { return y(1-y)); }
double g3(double y) { return 0.0; }
double f(double x, double y) { return 6*x*y*(1-y)-2*x^3; }
double u_exact(double x, double y) { return x*x*cos(4*y); }
void PoissonOnSquare(double (*g[4])(double), double u[n+2][n+2], int numIter) {
int i, j, k;
double h;
h = b/(n+1);
for (i=0; i <=n+1; i++) {
u[i][0] = (*g[0])(i*h);
u[n+1][i] = (*g[1])(i*h);
u[i][n+1] = (*g[2])(i*h);
u[0][i] = (*g[3])(i*h);
}
for (i=1; i <= n; i++)
for (j=1; j <= n; j++) u[i][j] = 0.0;
for (k=1; k <= numIter; k++)
for (i=1; i <= n; i++)
for (j=1; j <= n; j++)
u[i][j] = 0.25*(u[i-1][j]+u[i+1][j]+u[i][j-1]+u[i][j+1]-h*h*f(i*h,j*h));
}
int main() {
double (*g[4])(double), u[n+2][n+2];
int i, j;
g[0] = &g0; g[1] = &g1; g[2] = &g2; g[3] = &g3;
PoissonOnSquare(g, u, 500);
for (i=1; i <= n; i=i+4) {
for (j=1; j <= n; j=j+4) { cout.precision(4); cout.width(10); cout << right << u[i][j]; }
cout << "\n";
}
cout << "\n *** Rozwiązanie dokładne ***\n";
for (i=1; i <= n; i=i+4) {
for (j=1; j <= n; j=j+4) { cout.precision(4); cout.width(10); cout << right << u_exact(i*b/(n+1), j*b/(n+1))<< " "; }
cout << "\n";
}
system("pause");
}
czy dobrze zaimplementowałam warunki brzegowe w takim kodzie? bo chciałabym mieć :
x=0 -> a1=1, b1=0, c1=0
a dla x=1 -> a1=1,b1=0 i c1=y(1-y)