/*********************************************************************** * This program tests the function brown() which is designed to solve * * a nonlinear system of equations * * f0 (x0,x1,...,xn-1) = 0 * * f1 (x0,x1,...,xn-1) = 0 * * ... * * fn-1(x0,x1,...,xn-1) = 0 * * using Brown's method * * * * Scope of program : * * ================== * * The program reads the input from stdin and writes output onto stdout.* * Calls for input are written onto stderr which also receives error * * * * After reading, the input is printed out for control purposes. Then * * Brown's method is performed and, barring premature errors, the end * * results are printed. * * * * If the user wants to test his own systems, he should follow the * * example in nlglstst.c. * * * * Form of the input file : * * ======================== * * bspnummer Number of the example from nlglstst.c * * eps Desired accuracy * * prot Protocol is kept if prot = 1, not kept for prot = 0 * * maxit Maximal number of steps * * x0[0] \ * * x0[1] \ Starting vector for iteration * * ... / * * x0[n-1]/ * * * * The order of the system is prescribed via the number of the test * * example. * * -------------------------------------------------------------------- * * Ref.: "Numerical algorithms with C By Gisela Engeln-Muellges and * * Franck Uhlig, Springer-Verlag, 1996" [BIBLI 11]. * * * * C++ version by J-P Moreau, Paris. * * (www.jpmoreau.fr) * ************************************************************************ Files used: Basis_r.cpp, Brown.cpp, Nlglstst.cpp, Vmblock.cpp, Basis.h, Brown.h, Nlglstst.h, Vmblock.g ----------------------------------------------------------------------*/ #include #include #include "brown.h" #include "nlglstst.h" int main() { int n, /* size of system */ prot, /* Protocol flag */ maxit, /* maximal number of iterations */ fehler, /* error code of brown() */ bspnummer, /* Number of chosen example */ itanz, /* number of iterations performed */ i; /* Loop variable */ REAL eps, /* desired accuracy */ *x0, /* [0..n-1] starting vector */ *x1; /* [0..n-1] approximate solution */ bsptyp1 *beispiel; /* pointer to the system of current example */ void *vmblock; /* List of dynamically allocated vectors */ FILE *fp; /* Output file */ /* -------------------- read input -------------------------------- */ vmblock = vminit(); /* initialize files */ x0 = (REAL *)vmalloc(vmblock, VEKTOR, 5, 0); x1 = (REAL *)vmalloc(vmblock, VEKTOR, 5, 0); if (! vmcomplete(vmblock)) /* allocation successful ? */ { LogError("lack of memory", 0, __FILE__, __LINE__); return 4; } eps = 1e-8; prot = 0; maxit = 100; for (i = 0; i < 5; i++) x0[i] = 1.0; fp = fopen("browntst1.lst","w"); WriteHead(fp," Brown's method for nonlinear systems of equations"); // main loop for (bspnummer=0; bspnummer<11; bspnummer++) { if ((beispiel = nlgls_waehlen(bspnummer)) == NULL) { LogError("not one of the listed examples", 0, __FILE__, __LINE__); return 3; } n = beispiel->n; /* ------------ print input for checking purposes ----------------- */ fprintf(fp," Example n° %d\n",bspnummer); fprintf(fp,"\n" " System to be solved\n" "%s\n" " Starting vector\n", beispiel->fkt_text() ); for (i = 0; i < n; i++) fprintf(fp," %9.3"LZP"e", x0[i]); fprintf(fp,"\n\n" " Error bound = %"LZP"e\n" " Maximal number of iterations = %d\n", eps, maxit ); if (prot) fprintf(fp," Intermediate results are saved.\n"); else fprintf(fp," Intermediate results are not kept.\n"); /* ------------ solve nonlinear system ---------------------------- */ fehler = brown(beispiel->fkt, n, x0, eps, prot, maxit, x1, &itanz); switch (fehler) { case 0: break; case 1: LogError("brown(): too many steps", 10 + fehler, __FILE__, __LINE__); break; case 2: LogError("brown(): linearized system singular", 10 + fehler, __FILE__, __LINE__); break; case 3: LogError("brown(): lack of memory", 10 + fehler, __FILE__, __LINE__); break; case 4: LogError("brown(): wrong input parameter: " "fkt = NULL or n < 1 or maxit < 1", 10 + fehler, __FILE__, __LINE__); break; case 5: LogError("brown(): error calling fkt()", 10 + fehler, __FILE__, __LINE__); break; default: LogError("brown(): other error", 10 + fehler, __FILE__, __LINE__); } // if (fehler != 0) // return 10 + fehler; /* --------------------- print solution --------------------------- */ if (fehler==0) { fprintf(fp,"\n Solution vector\n"); for (i = 0; i < n; i++) fprintf(fp," %f", x1[i]); fprintf(fp,"\n\n Number of iterations: %d\n", itanz); } WriteEnd(fp); } // main loop fclose(fp); printf("\n Results in browntst1.lst.\n"); return 0; } /* Visual C++ version by J-P Moreau */