#include #include #include #include #define MAXSIZE 20 #define MAXTRIAL 150 #define TOLX 1e-4 #define TOLF 1e-1 #define R_MAX 2.0 #define R_MIN -5.0 #define RELAX1 0.9 #define RELAX2 0.7 #define RELAX3 0.2 int newt_raph(void (*vecfunc)(double x[], double fvec[], int n, int init, ...), double x[], int n) { /****************************************************************** newt_raph 2006 Ming Pan mpan@princeton.EDU Newton-Raphson method to solve non-linear system adapted from "Numerical Recipes" A relaxation factor "RELAX#" is added to help the Newton-Raphson trials to converge during the initial formation of ice, where the shape of the residual function becomes very difficult ******************************************************************/ int k, i, index[MAXSIZE], Error; double errx, errf, d, fvec[MAXSIZE], fjac[MAXSIZE*MAXSIZE], p[MAXSIZE]; double a[MAXSIZE], b[MAXSIZE], c[MAXSIZE]; Error = 0; for (k=0; k10&&k<=20&&x[i]R_MIN) x[i]+=p[i]*RELAX1; else if (k>20&&k<=60&&x[i]R_MIN) x[i]+=p[i]*RELAX2; else if (k>60&&x[i]R_MIN) x[i]+=p[i]*RELAX3; else x[i]+=p[i]; } // stop if TOLX is satisfied if (errx<=TOLX) { //fprintf(stderr, "Number of Newton-Raphson trials (x criterium with F error = %g): %d\n", errf, k); return (Error); } } Error = 1; #if VERBOSE //fprintf(stderr, "WARNING: Maximum number of trials %d reached in Newton-Raphson search for solution (with F error = %g).\n", MAXTRIAL, errf); //for (i=0; i=0; j--) { factor=c[j]; c[j]=c[j]-b[j+1]*factor; r[j]=r[j]-r[j+1]*factor; factor=b[j]; //b[j]=1.0; r[j]=r[j]/factor; } }