/* simeq_newton2.c solve nonlinear system of equations */ /* method: newton iteration using Jacobian */ /* */ /* Given problem A X = Y where X may have terms x1, x2, x3, x4, */ /* x1*x2, x1*x3, x1*x4, x2*x3, x2*x4, x3*x4 */ /* A is 10 by 10 matrix of reals (could be complex) */ /* Y is vector of reals (could be complex) */ /* independent unknowns are x1, x2, x3, x4 */ /* */ /* for testing, generate A using pseudo random numbers */ /* choose x1=1.1 x2=1.2 x3=1.4 x4 =1.5, compute products */ /* compute terms of Y using Y = A X */ /* */ /* Solve by initial guess at values of x1, x2, x3, x4 computing products */ /* X_next = X_initial - J_initial^-1 * (A * X_initial - Y) */ /* in general X_next = X_prev - (J_prev^-1 * (A * X_prev - Y))*b */ /* where 0 < b < 1, often 0.5, for stability */ /* */ /* solved when abs sum each row A * X_next -Y < epsilon */ /* */ /* It may stall, stop if abs(X_next-X_prev) #include #include "udrnrt.h" #include "invert.h" #undef abs #define abs(a) ((a)<0.0?(-(a)):(a)) #define n 10 /* number of equations */ int main(int argc, char *argv[]) { double A[n][n]; double Ja[n][n]; /* inverted in place */ /* derivatives hand coded to load Ja */ double Y[n]; double X_prev[n]; double X_temp[n]; double X_tmp2[n]; double X_next[n]; double X_soln[n]; /* usually not known. test case */ double eps = 1.0e-8; double b = 1.00; /* stability factor */ double resid; /* residual from last iteration */ double presid; /* residual from prior to last iteration */ double err; /* error from test solution */ int i, j, itr; printf("simeq_newton2.c running \n"); /* build test case, using a random A matrix */ for(i=0; i