public static vector newton(Func<vector,vector> f, vector x, double eps=1e-3, double dx=1e-7){ int n=x.size; vector fx=f(x); vector z,fz; while(true){ matrix J=jacobian(f,x,fx); // define the Jacobian matrix with partial derivatives qrdecomposition qrJ=new qrdecomposition(J); // use QR-decomposition matrix B=qrJ.inverse(); // construct the inverse of our QR-decomposition vector Dx=-B*fx; // solve the Newton's algorithm in matrix form (using eq. 5) double s=1; while(true){ // backtracking linesearch z=x+Dx*s; // creating left-hand side of eq. 8 fz=f(z); // creating left-hand side of eq. 8 if(fz.norm()<(1-s/2)*fx.norm()){ // stop if step is good break; } if(s<1.0/32){ // stop if minimum step-size is reached break; } s/=2; // backtrack using half the step-size } x=z; fx=fz; if(fx.norm()<eps)break; // until converged } return x; }//newton
}//broyden public static int broyden (Func <vector, vector> f, ref vector x, double eps = 1e-3) { vector fx = f(x), z, fz; matrix J = jacobian(f, x, fx); var qrJ = new qrdecomposition(J); matrix B = qrJ.inverse(); int nsteps = 0; while (++nsteps < 999) { vector Dx = -B * fx; double s = 1; while (true) { z = x + Dx * s; fz = f(z); if (fz.norm() < (1 - s / 2) * fx.norm()) { break; } if (s < 1.0 / 32) { J = jacobian(f, x, fx); qrJ = new qrdecomposition(J); B = qrJ.inverse(); break; } s /= 2; } vector dx = z - x; vector df = fz - fx; if (dx.dot(df) > 1e-9) { vector c = (dx - B * df) / dx.dot(df); B.update(c, dx); } //vector c=(dx-B*df)/(df%df); B.update(c,df); //vector c=(dx-B*df)/(dx%(B*df)); B.update(c,B*df); x = z; fx = fz; if (fx.norm() < eps) { break; } } return(nsteps); }//broyden
static void Main() { int n = 7, m = n; var A = new matrix(n, m); var rnd = new System.Random(); for (int i = 0; i < A.size1; i++) { for (int j = 0; j < A.size2; j++) { A[i, j] = 2 * (rnd.NextDouble() - 1); } } A.print("random matrix A:"); var b = new vector(m); for (int i = 0; i < b.size; i++) { b[i] = rnd.NextDouble(); } b.print("random vector b:\n"); var qra = new qrdecomposition(A); var x = qra.solve(b); x.print("solution x to system A*x=b:\n"); var Ax = A * x; Ax.print("check: A*x (should be equal b):\n"); if (vector.approx(Ax, b)) { WriteLine("test passed"); } else { WriteLine("test failed"); } var B = qra.inverse(); var C = (B * A); C.print("A^-1*A="); var D = (A * B); D.print("A*A^-1="); }
public static vector newton (Func <vector, vector> f, vector x, double eps = 1e-3, double dx = 1e-7) { int n = x.size; vector fx = f(x), z, fz; while (true) { matrix J = jacobian(f, x, fx); qrdecomposition qrJ = new qrdecomposition(J); matrix B = qrJ.inverse(); vector Dx = -B * fx; double s = 1; while (true) // backtracking linesearch { z = x + Dx * s; fz = f(z); if (fz.norm() < (1 - s / 2) * fx.norm()) { break; } if (s < 1.0 / 32) { break; } s /= 2; } x = z; fx = fz; if (fx.norm() < eps) { break; } } return(x); } //broyden