// 通过非线性方程组构造牛顿迭代 public NewtonMethod(NonlinearEquations e, int uNum, int eNum, int mItera, double preci, double[] sVal) { theUnknowNum = uNum; theEquationNum = eNum; theMaxIterativeTime = mItera; thePrecision = preci; theStartValue = new double[uNum]; for (int i = 0; i < uNum; i++) { theStartValue[i] = sVal[i]; } // 构造非线性方程组 theEquations = new NonlinearEquations(); theEquations.Clear(); for (int i = 0; i < eNum; i++) { theEquations.AddEquation(e[i]); } // 构造雅各比方程组 theJacobiEquations = new NonlinearEquation[theEquationNum, theUnknowNum]; for (int i = 0; i < theEquationNum; i++) { for (int j = 0; j < theUnknowNum; j++) { theJacobiEquations[i, j] = new NonlinearEquation(theUnknowNum); theJacobiEquations[i, j] = PartialDifferential(theEquations[i], j); } } theEndValue = new double[uNum]; }
private void btnConfirm_Click(object sender, EventArgs e) { txtPrint.Text = ""; txtResult.Text = ""; txtWarn.Text = ""; if (cboUnoNum.Text == string.Empty) { txtWarn.Text = "错误!未选择未知数的个数!"; return; } unknowNum = Int32.Parse(cboUnoNum.Text); if (unknowNum == 1) { MonoHighPowerForm f0 = new MonoHighPowerForm(); f0.Owner = this; f0.ShowDialog(); if (f0.isCancelled1 == true) { txtWarn.Text = "已取消!"; return; } warning = f0.warning1; if (warning != "") { txtWarn.Text = warning; return; } try { xInit = f0.xInit1; monoCoefficient = new double[f0.itemNum]; monoCoefficient = f0.monoCoefficient1; monoPower = new int[f0.itemNum - 1]; monoPower = f0.monoPower1; monoEqua = new MonoHighPowEqua(precision, xInit, monoCoefficient, monoPower); txtPrint.Text = monoEqua.MonoEquaToString(); } catch (Exception) { txtWarn.Text = "错误!已关闭或输入格式有误!"; return; } } else { startValue = new double[unknowNum]; for (int i = 0; i < unknowNum; i++) { startValue[i] = 1; } DataInputForm f2 = new DataInputForm(); f2.Owner = this; f2.ShowDialog(); if (f2.isCancelled == true) { txtWarn.Text = "已取消!"; return; } warning = f2.warning1; if (warning != "") { txtWarn.Text = warning; return; } equationNum = f2.equationNum1; coefficient = new double[equationNum, unknowNum]; coefficient = f2.coefficient1; power = new int[equationNum, unknowNum]; power = f2.power1; constant = new double[equationNum]; constant = f2.constant1; newEquations = new NonlinearEquation[equationNum]; try { for (int i = 0; i < equationNum; i++) { newEquations[i] = new NonlinearEquation(unknowNum); for (int j = 0; j < unknowNum; j++) { newEquations[i].theCoefficient[j] = coefficient[i, j]; newEquations[i].thePower[j] = power[i, j]; } newEquations[i].theConstant = constant[i]; } equations.Clear(); equations.AddEquations(newEquations); // 构造牛顿迭代法 newton = new NewtonMethod(equations, unknowNum, equationNum, maxIterativeTime, precision, startValue); // 将方程组打印出来 for (int i = 0; i < equationNum; i++) { txtPrint.Text += equations[i].EquationToString() + "\r\n"; } } catch (Exception) { txtWarn.Text = "警告!未知错误!"; return; } } }