sample5_1.cs

来自「C#的数值计算的几乎所有算法」· CS 代码 · 共 37 行

CS
37
字号
/*
 * 示例程序Sample5_1: NLEquations类的求非线性方程实根的对分法
 */

using System;
using CSharpAlgorithm.Algorithm;

namespace CSharpAlgorithm.Sample
{
    class Class1
    {
        // 建立NLEquation的子类,在其中重载函数func
        class NLEq : NLEquations
        {
            protected override double Func(double x)
            {
                double z = (((((x-5.0)*x+3.0)*x+1.0)*x-7.0)*x+7.0)*x-20.0;
                return z;
            }
        }

        [STAThread]
        static void Main(string[] args)
        {
            // 求解
            NLEq nleq = new NLEq();
            double[] x = new double[6];
            int n = nleq.GetRootBisect(6, x, -2, 5, 0.2, 0.0001);
            Console.WriteLine("求得如下" + n + "个根:");
            for (int i=0; i<=n-1; i++)
            {
                Console.WriteLine(x[i]);
            }
        }
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?