// simeq_newton2.java 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 // x1^2 x2^2 x3^2 x4^2 // 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)0) System.out.println("simeq_newton2 running n="+n+", nlin="+nlin); eps = ueps; maxiter = uiter; b = ub; // setup nonlinear terms for(int i=n; i=0) X[i] *= X[var2[i]]; // -1 flag should not be used } for(int i=0; i2) { for(int i=0; i0) System.out.println("simeq_newton2 itr "+itr+", prev="+presid+ " residual="+resid); if(resid=0) Ja[i][j] += A[i][k]*X[var2[k]]; } else if(var2[k]==j) { if(var1[k]>=0) Ja[i][j] += A[i][k]*X[var1[k]]; } } // end k } // end j } // end i if(monitor>4) { System.out.println("Ja computed "); for(int i=0; i5) { System.out.println("Ja inverted "); for(int i=0; i3) { for(int i=0; i=0) X_next[i] *= X_next[var2[i]]; } if(monitor>2) { for(int i=0; i2) System.out.println(" "); } // end iteration if(monitor>0) System.out.println("simeq_newton2.java finished"); } } // end class simeq_newton2