• C#,数值计算——函数计算,Dfridr的计算方法与源程序


    1 文本格式

    using System;

    namespace Legalsoft.Truffer
    {
        ///


        /// 通过Ridders的多项式外推方法返回函数func在点x处的导数。
        /// 输入值h作为估计的初始步长;它不需要很小,而是应为x上的增量,
        /// 在此增量上func将发生实质性变化。误差估计导数返回为err。
        /// Returns the derivative of a function func at a point x by Ridders' method of polynomial extrap-
        /// olation.The value h is input as an estimated initial stepsize; it need not be small, but rather
        /// should be an increment in x over which func changes substantially.An estimate of the error in
        /// the derivative is returned as err.
        ///

        public class Dfridr
        {
            public Dfridr()
            {
            }

            public static double dfridr(UniVarRealValueFun func, double x, double h, ref double err)
            {
                const int ntab = 10;
                const double con = 1.4;
                const double con2 = (con * con);
                const double big = double.MaxValue;
                const double safe = 2.0;

                double ans = 0.0;
                double[,] a = new double[ntab, ntab];
                //if (h == 0.0)
                if (Math.Abs(h) <= float.Epsilon)
                {
                    throw new Exception("h must be nonzero in dfridr.");
                }
                double hh = h;
                a[0, 0] = (func.funk(x + hh) - func.funk(x - hh)) / (2.0 * hh);
                err = big;
                for (int i = 1; i < ntab; i++)
                {
                    hh /= con;
                    a[0, i] = (func.funk(x + hh) - func.funk(x - hh)) / (2.0 * hh);
                    double fac = con2;
                    for (int j = 1; j <= i; j++)
                    {
                        a[j, i] = (a[j - 1, i] * fac - a[j - 1, i - 1]) / (fac - 1.0);
                        fac = con2 * fac;
                        double errt = Math.Max(Math.Abs(a[j, i] - a[j - 1, i]), Math.Abs(a[j, i] - a[j - 1, i - 1]));
                        if (errt <= err)
                        {
                            err = errt;
                            ans = a[j, i];
                        }
                    }
                    if (Math.Abs(a[i, i] - a[i - 1, i - 1]) >= safe * err)
                    {
                        break;
                    }
                }
                return ans;
            }
        }
    }
     

    2 代码格式

    1. using System;
    2. namespace Legalsoft.Truffer
    3. {
    4. ///
    5. /// 通过Ridders的多项式外推方法返回函数func在点x处的导数。
    6. /// 输入值h作为估计的初始步长;它不需要很小,而是应为x上的增量,
    7. /// 在此增量上func将发生实质性变化。误差估计导数返回为err。
    8. /// Returns the derivative of a function func at a point x by Ridders' method of polynomial extrap-
    9. /// olation.The value h is input as an estimated initial stepsize; it need not be small, but rather
    10. /// should be an increment in x over which func changes substantially.An estimate of the error in
    11. /// the derivative is returned as err.
    12. ///
    13. public class Dfridr
    14. {
    15. public Dfridr()
    16. {
    17. }
    18. public static double dfridr(UniVarRealValueFun func, double x, double h, ref double err)
    19. {
    20. const int ntab = 10;
    21. const double con = 1.4;
    22. const double con2 = (con * con);
    23. const double big = double.MaxValue;
    24. const double safe = 2.0;
    25. double ans = 0.0;
    26. double[,] a = new double[ntab, ntab];
    27. //if (h == 0.0)
    28. if (Math.Abs(h) <= float.Epsilon)
    29. {
    30. throw new Exception("h must be nonzero in dfridr.");
    31. }
    32. double hh = h;
    33. a[0, 0] = (func.funk(x + hh) - func.funk(x - hh)) / (2.0 * hh);
    34. err = big;
    35. for (int i = 1; i < ntab; i++)
    36. {
    37. hh /= con;
    38. a[0, i] = (func.funk(x + hh) - func.funk(x - hh)) / (2.0 * hh);
    39. double fac = con2;
    40. for (int j = 1; j <= i; j++)
    41. {
    42. a[j, i] = (a[j - 1, i] * fac - a[j - 1, i - 1]) / (fac - 1.0);
    43. fac = con2 * fac;
    44. double errt = Math.Max(Math.Abs(a[j, i] - a[j - 1, i]), Math.Abs(a[j, i] - a[j - 1, i - 1]));
    45. if (errt <= err)
    46. {
    47. err = errt;
    48. ans = a[j, i];
    49. }
    50. }
    51. if (Math.Abs(a[i, i] - a[i - 1, i - 1]) >= safe * err)
    52. {
    53. break;
    54. }
    55. }
    56. return ans;
    57. }
    58. }
    59. }

  • 相关阅读:
    中文符号雨python
    SpringBoot 集成 Nacos
    python3发送Gratuitous ARP更新vip绑定关系
    【数百句与酒有关的名句】酒在诗歌中起到催化剂的作用,激起亢奋的情绪,使得创作灵感频频出现。
    15天深度复习JavaWeb的详细笔记(五)——JavaScript
    一百八十四、大数据离线数仓完整流程——步骤三、在Hive中建基础库维度表并加载MySQL中的维度表数据
    电子信息类毕业生找工作有多难?研究所难进?大厂太卷?
    Masked Label Prediction: Unified Message Passing Model for Semi-Supervised Classification
    DNS/ICMP协议/NAT技术
    HOT100自查题集
  • 原文地址:https://blog.csdn.net/beijinghorn/article/details/134244489