/* From: "COMPUTATIONAL PHYSICS, 2nd Ed" by RH Landau, MJ Paez, and CC Bordeianu Copyright Wiley-VCH, 2007. Electronic Materials copyright: R Landau, Oregon State Univ, 2007; MJ Paez, Univ Antioquia, 2007; and CC Bordeianu, Univ Bucharest, 2007. Support by National Science Foundation */ // Newton_Jama.java: solves n nonlin eqs via Newton-Raphson and JAMA import Jama.*; public class Newton_Jama { public static void main(String[] argv) { int n = 9; int it, i; double deriv[][] = new double[n][n], dx[] = new double[n]; double f[] = new double[n], errXi, errX, errF; double eps = 1e-6; // Precision for convergence // Guess values for unknowns (all 0.5 also worked) double x[] = {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1., 1., 1.}; // Iteration loop, break when converge for ( it=1; it <= 100; it++) { F(x,f); // Compute F vector = the n equations RHS dFi_dXj(x, deriv, n); // Compute derivatives dFi/dXj // A = LHS matrix using Jama Matrix A = new Matrix(deriv); System.out.print("dFi_dXj for iteration " + it); A.print(7,4); // Printout of dFi_dXj via Jama // Update solution Matrix B = new Matrix(n,1); //RHS of system for ( i=0; i <= n-1; i++) B.set(i, 0, -f[i]); System.out.print("RHS vector [f(Xguess)] for iteration " + it); B.print(17,4); // Solution vector Matrix sol = A.solve(B); System.out.print("dx corrections for iteration " + it); sol.print(17,4); System.out.println (" x + dx for iteration " + it); for ( i=0; i <= n-1; i++) { dx[i] = sol.get(i,0); x[i] = x[i] + dx[i]; // Increment solution System.out.println("x["+i+"] = "+x[i]);} // Print soltn System.out.println(); errX = errF = errXi = 0.0; // Set errors to 0 for ( i = 0; i <= n-1; i++) { if ( x[i] != 0.) errXi = Math.abs(dx[i]/x[i]); else errXi = Math.abs(dx[i]); if ( errXi > errX) errX = errXi; // Errors if ( Math.abs(f[i]) > errF ) errF = Math.abs(f[i]); } // Check for convergence if ((errX <= eps) && (errF <= eps)) break; } } // N nonlinear equations, enter your system here public static void F(double x[], double f[]) { f[0] = 3*x[3] + 4*x[4] + 4*x[5] - 8.0; f[1] = 3*x[0] + 4*x[1] - 4*x[2]; f[2] = x[6]*x[0] - x[7]*x[1] - 10.0; f[3] = x[6]*x[3] - x[7]*x[4]; f[4] = x[7]*x[1] + x[8]*x[2] - 20.0; f[5] = x[7]*x[4] - x[8]*x[5]; f[6] = Math.pow(x[0],2) + Math.pow(x[3],2) - 1.0; f[7] = Math.pow(x[1],2) + Math.pow(x[4],2) - 1.0; f[8] = Math.pow(x[2],2) + Math.pow(x[5],2) - 1.0; } public static void dFi_dXj(double x[], double deriv[][], int n) { // Compute derivatives of n eqns using central difference int i,j; double temp; double h = 1e-4; double f[] = new double[n]; h = 1e-4; // Differentiation step // Loop for x + h/2 for (j = 0; j <= n-1; j++) { temp = x[j]; x[j] += h/2.; F(x, f); // Store f at x[j] = x[j]+h/2 for (i = 0; i <= n-1; i++) { deriv[i][j] = f[i];} x[j] = temp; //Reset x[j] } // Loop for x - h/2 for (j = 0; j <= n-1; j++) { temp = x[j]; x[j] = x[j]-h/2.; F(x, f); // Store f at x[j] = x[j]-h/2 for (i = 0; i <= n-1; i++) deriv[i][j] = (deriv[i][j]-f[i])/h; x[j] = temp; // Reset x[j] } } }