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

编程入门 行业动态 更新时间:2024-10-14 14:17:58

C#,数值计算——函数计算,Epsalg的计算方法与<a href=https://www.elefans.com/category/jswz/34/1768784.html style=源程序"/>

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

1 文本格式

using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// Convergence acceleration of a sequence by the algorithm.Initialize by
    /// calling the constructor with arguments nmax, an upper bound on the
    /// number of terms to be summed, and epss, the desired accuracy.Then make
    /// successive calls to the function next, with argument the next partial sum
    /// of the sequence. The current estimate of the limit of the sequence is 
    /// returned by next.The flag cnvgd is set when convergence is detected.
    /// </summary>
    public class Epsalg
    {
        private double[] e { get; set; }
        private int n { get; set; }
        private int ncv { get; set; }
        public bool cnvgd { get; set; }
        /// <summary>
        /// Numbers near machine underflow and overflow limits.
        /// </summary>
        private double eps { get; set; }
        private double small { get; set; }
        private double big { get; set; }
        private double lastval { get; set; }
        private double lasteps { get; set; }

        public Epsalg(int nmax, double epss)
        {
            this.e = new double[nmax];
            this.n = 0;
            this.ncv = 0;
            thisvgd = false;
            this.eps = epss;
            this.lastval = 0.0;
            small = float.MinValue * 10.0;
            big = double.MaxValue;
        }

        public double next(double sum)
        {
            e[n] = sum;
            double temp2 = 0.0;
            for (int j = n; j > 0; j--)
            {
                double temp1 = temp2;
                temp2 = e[j - 1];
                double diff = e[j] - temp2;
                if (Math.Abs(diff) <= small)
                {
                    e[j - 1] = big;
                }
                else
                {
                    e[j - 1] = temp1 + 1.0 / diff;
                }
            }
            n++;
            double val = (n & 1) != 0 ? e[0] : e[1];
            if (Math.Abs(val) > 0.01 * big)
            {
                val = lastval;
            }
            lasteps = Math.Abs(val - lastval);
            if (lasteps > eps)
            {
                ncv = 0;
            }
            else
            {
                ncv++;
            }
            if (ncv >= 3)
            {
                cnvgd = true;
            }
            return (lastval = val);
        }

    }
}
 

2 代码格式

using System;namespace Legalsoft.Truffer
{/// <summary>/// Convergence acceleration of a sequence by the algorithm.Initialize by/// calling the constructor with arguments nmax, an upper bound on the/// number of terms to be summed, and epss, the desired accuracy.Then make/// successive calls to the function next, with argument the next partial sum/// of the sequence. The current estimate of the limit of the sequence is /// returned by next.The flag cnvgd is set when convergence is detected./// </summary>public class Epsalg{private double[] e { get; set; }private int n { get; set; }private int ncv { get; set; }public bool cnvgd { get; set; }/// <summary>/// Numbers near machine underflow and overflow limits./// </summary>private double eps { get; set; }private double small { get; set; }private double big { get; set; }private double lastval { get; set; }private double lasteps { get; set; }public Epsalg(int nmax, double epss){this.e = new double[nmax];this.n = 0;this.ncv = 0;thisvgd = false;this.eps = epss;this.lastval = 0.0;small = float.MinValue * 10.0;big = double.MaxValue;}public double next(double sum){e[n] = sum;double temp2 = 0.0;for (int j = n; j > 0; j--){double temp1 = temp2;temp2 = e[j - 1];double diff = e[j] - temp2;if (Math.Abs(diff) <= small){e[j - 1] = big;}else{e[j - 1] = temp1 + 1.0 / diff;}}n++;double val = (n & 1) != 0 ? e[0] : e[1];if (Math.Abs(val) > 0.01 * big){val = lastval;}lasteps = Math.Abs(val - lastval);if (lasteps > eps){ncv = 0;}else{ncv++;}if (ncv >= 3){cnvgd = true;}return (lastval = val);}}
}

更多推荐

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

本文发布于:2023-11-15 10:51:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598439.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:源程序   数值   计算方法   函数   Epsalg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!