C#, Numerical Calculation - Calculation Method and Source Program of Funcd

 

using System;

namespace Legalsoft.Truffer
{
    public class Funcd : RealValueFunWithDiff
    {
        public double EPS { get; set; } = float.Epsilon;
        public RealValueFun func { get; set; }
        public double f { get; set; }

        public Funcd(RealValueFun funcc)
        {
            this.EPS = 1.0e-8;
            this.func = funcc;
        }
        public double funk(double[] x)
        {
            return get(x);
        }

        public double get(double[] x)
        {
            return f = func.funk(x);
        }

        public void df(double[] x, double[] df)
        {
            int n = x.Length;
            double[] xh = x;
            double fold = f;
            for (int j = 0; j < n; j++)
            {
                double temp = x[j];
                double h = EPS * Math.Abs(temp);
                //if (h == 0.0)
                if (Math.Abs(h) <= float.Epsilon)
                {
                    h = EPS;
                }
                xh[j] = temp + h;
                h = xh[j] - temp;
                double fh = (xh[0]);
                xh[j] = temp;
                df[j] = (fh - fold) / h;
            }
        }
    }
}
 

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/132219035