Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么是辛普森';用Python重写的s集成规则给出了不同的结果?_Python_C#_Numerical Integration_Simpsons Rule - Fatal编程技术网

为什么是辛普森';用Python重写的s集成规则给出了不同的结果?

为什么是辛普森';用Python重写的s集成规则给出了不同的结果?,python,c#,numerical-integration,simpsons-rule,Python,C#,Numerical Integration,Simpsons Rule,以下源代码是集成的实现 C#源代码。 using System; public class Simpson { private double Function(double x) { return 1.0 / (1.0 + Math.Pow(x, 5)); //Define the function f(x) } public double Compute(double a, double b, int n) { dou

以下源代码是集成的实现

C#源代码。

using System;

public class Simpson
{
    private double Function(double x)
    {
        return 1.0 / (1.0 + Math.Pow(x, 5)); //Define the function f(x)
    }

    public double Compute(double a, double b, int n)
    {
        double[] x = new double[n + 1];

        double delta_x = (b - a) / n;

        x[0] = a;

        for (int j = 1; j <= n; j++)//calculate the values of x1, x2, ...xn
        {
            x[j] = a + delta_x * j;
        }

        double sum = Function(x[0]);

        for (int j = 1; j < n; j++)
        {
            if (j % 2 != 0)
            {
                sum += 4 * Function(x[j]);
            }
            else
            {
                sum += 2 * Function(x[j]);
            }
        }

        sum += Function(x[n]);

        double integration = sum * delta_x / 3;

        return integration;
    }
}

public class MainClass
{
    public static void Main()
    {
        Simpson simpson = new Simpson();
        double a = 0d;//lower limit a
        double b = 3d;//upper limit b
        int n = 6;//Enter step-length n

        if (n % 2 == 0)//n must be even
        {
            Console.WriteLine(simpson.Compute(a, b, n));
        }
        else
        {
            Console.WriteLine("n should be an even number");
        }

        Console.ReadLine();
    }
}
import math

# function to be integrated
def Function(x):
    return 1.0 / (1.0 + math.pow(x, 5)); #Define the function f(x)

def Simpson(a, b, n):
    x = [0] * (n + 1);
    delta_x = (b - a) / n;
    x[0] = a;

    for j in range(1,n):#calculate the values of x1, x2, ...xn
        x[j] = a + delta_x * j;

    sum = Function(x[0]);

    for j in range( 1, n):
        if (j % 2 != 0):
            sum = sum + 4 * Function(x[j]);
        else:
            sum = sum + 2 * Function(x[j]);

    sum += Function(x[n]);

    integration = sum * delta_x / 3;

    return integration;


# Main Program
a = 0.0; # lower limit a
b = 3.0; # upper limit b
n = 6; # Enter step-length n

if (n % 2 == 0): # n must be even
    print(Simpson(a, b, n));
else:
    print("n should be an even number");

Python源代码。

using System;

public class Simpson
{
    private double Function(double x)
    {
        return 1.0 / (1.0 + Math.Pow(x, 5)); //Define the function f(x)
    }

    public double Compute(double a, double b, int n)
    {
        double[] x = new double[n + 1];

        double delta_x = (b - a) / n;

        x[0] = a;

        for (int j = 1; j <= n; j++)//calculate the values of x1, x2, ...xn
        {
            x[j] = a + delta_x * j;
        }

        double sum = Function(x[0]);

        for (int j = 1; j < n; j++)
        {
            if (j % 2 != 0)
            {
                sum += 4 * Function(x[j]);
            }
            else
            {
                sum += 2 * Function(x[j]);
            }
        }

        sum += Function(x[n]);

        double integration = sum * delta_x / 3;

        return integration;
    }
}

public class MainClass
{
    public static void Main()
    {
        Simpson simpson = new Simpson();
        double a = 0d;//lower limit a
        double b = 3d;//upper limit b
        int n = 6;//Enter step-length n

        if (n % 2 == 0)//n must be even
        {
            Console.WriteLine(simpson.Compute(a, b, n));
        }
        else
        {
            Console.WriteLine("n should be an even number");
        }

        Console.ReadLine();
    }
}
import math

# function to be integrated
def Function(x):
    return 1.0 / (1.0 + math.pow(x, 5)); #Define the function f(x)

def Simpson(a, b, n):
    x = [0] * (n + 1);
    delta_x = (b - a) / n;
    x[0] = a;

    for j in range(1,n):#calculate the values of x1, x2, ...xn
        x[j] = a + delta_x * j;

    sum = Function(x[0]);

    for j in range( 1, n):
        if (j % 2 != 0):
            sum = sum + 4 * Function(x[j]);
        else:
            sum = sum + 2 * Function(x[j]);

    sum += Function(x[n]);

    integration = sum * delta_x / 3;

    return integration;


# Main Program
a = 0.0; # lower limit a
b = 3.0; # upper limit b
n = 6; # Enter step-length n

if (n % 2 == 0): # n must be even
    print(Simpson(a, b, n));
else:
    print("n should be an even number");
输出:

1.07491527775614
1.2408988843135185
C#程序的输出与Python的输出不同

这是为什么?

范围(1,n)
表示
[1,n)
。而
(int j=1;j
范围(1,n)
表示
[1,n)
。而
(int j=1;j