/** * 求非线性方程组一组实根的拟牛顿法 * * 调用时,须覆盖计算方程左端函数f(x)值及其偏导数值的虚函数 * double Func(double[] x, double[] y) * * @param n - 方程的个数,也是未知数的个数 * @param x - 一维数组,长度为n,存放一组初值x0, x1, …, xn-1, * 返回时存放方程组的一组实根 * @param t - 控制h大小的变量,0<t<1 * @param h - 增量初值 * @param nMaxIt - 迭代次数 * @param eps - 控制精度 * @return bool 型,求解是否成功 */ public bool GetRootsetNewton(int n, double[] x, double t, double h, int nMaxIt, double eps) { int i, j, l; double am, z, beta, d; double[] y = new double[n]; // 构造矩阵 Matrix mtxCoef = new Matrix(n, n); Matrix mtxConst = new Matrix(n, 1); double[] a = mtxCoef.GetData(); double[] b = mtxConst.GetData(); // 迭代求解 l = nMaxIt; am = 1.0 + eps; while (am >= eps) { Func(x, b); am = 0.0; for (i = 0; i <= n - 1; i++) { z = Math.Abs(b[i]); if (z > am) { am = z; } } if (am >= eps) { l = l - 1; if (l == 0) { return(false); } for (j = 0; j <= n - 1; j++) { z = x[j]; x[j] = x[j] + h; Func(x, y); for (i = 0; i <= n - 1; i++) { a[i * n + j] = y[i]; } x[j] = z; } // 调用全选主元高斯消元法 LEquations leqs = new LEquations(mtxCoef, mtxConst); Matrix mtxResult = new Matrix(); if (!leqs.GetRootsetGauss(mtxResult)) { return(false); } mtxConst.SetValue(mtxResult); b = mtxConst.GetData(); beta = 1.0; for (i = 0; i <= n - 1; i++) { beta = beta - b[i]; } if (beta == 0.0) { return(false); } d = h / beta; for (i = 0; i <= n - 1; i++) { x[i] = x[i] - d * b[i]; } h = t * h; } } // 是否在有效迭代次数内达到精度 return(nMaxIt > l); }