/* pde12_eq.c uniform grid boundary value PDE */ /* known solution for(testing method */ /* u(x) = sin(x)/4 + cos(x)/4 */ /* */ /* differential equation to solve */ /* d^2u/dx^2 + 3 du/dx + 2 u(x) = c(x,y) */ /* c(x,y) = -sin(x)/2+cos(x) */ /* uniform grid 0 to 9/2 Pi */ /* set up and solve system of linear equations */ #include #include #include #include "mpf_nuderivd.h" #define digits 100 #include "simeq.h" #undef abs #define abs(x) ((x)<0.0?(-(x)):(x)) #define nx 27 /* including boundary at 0 and nx-1 */ static double xmin = 0.0; static double pi = 3.1415926535897932384626433832795028841971; static double xmax; /* = pi * 9.0/2.0; */ static double hx; #define neqn (nx-2) /* DOF number of equations */ #define cs neqn /* RHS */ static double xg[nx]; /* nx-2 internal, non boundary cells */ static double cxx[nx]; static double cx[nx]; static double us[neqn]; /* solution vector */ static double ut[neqn*(neqn+1)]; /* includes RHS */ static double U(double x) { /* solution for(boundary and optional check */ return sin(x)/4.0 + cos(x)/4.0; } /* end U */ static double c(double x) { /* RHS of differential equation to solve */ return -sin(x)/2.0 + cos(x); } /* end c */ static int S(int i) /* zero based index includes boundary */ { /* this is row of ut[] */ return (i-1); /* index into us and ut, RHS, no boundary */ } /* end S */ static void print() { double error = 0.0; double max_error = 0.0; double avg_error = 0.0; double val; int i; printf("\n"); printf("exact solution u, computed us, error\n"); for(i=0; i max_error) max_error = error; } printf("xg[%2d]=%7.5f, u=%15.6e, us=%15.6e, err=%15.6e\n", i, xg[i], U(xg[i]), val, error); } printf("avg_error=%g, max_error=%g\n", avg_error/(double)neqn, max_error); } /* end print */ static void init_matrix() { int i, ii, k; /* cs is RHS, constant column subscript */ /* zero internal cells */ for(i=1; i0.001) { printf("row i=%2d, is bad err=%g\n", i, sum); } } /*i */ } /* end check_rows */ static void check_soln() /* check when solution unknown */ { double smaxerr, err, x, uxx, ux; int i, j, ii, jj, k; /* uxx(x) + 3 ux(x) + 2 u(x) = c(x) */ printf("check_soln against PDE\n"); smaxerr = 0.0; for(i=1; i smaxerr) smaxerr = abs(err); } /* i x */ printf("check soln against PDE max error=%g\n", smaxerr); } /* end Check_Soln */ /* write_soln2 for gnuplot */ static void write_soln2(double Ux[], char file_name[]) { FILE * Fout; int i; Fout = fopen(file_name, "w"); if(Fout==NULL) { printf("ERROR unable to open %s for writing \n", file_name); exit(1); } printf("writing %s\n", file_name); for(i=0; i