5

我正在开发一个比较两条曲线的程序(由二极管输出导致其电压/电流曲线)。

我想计算这两条曲线之间的面积(蓝色曲线是第一个二极管,红色是第二个)。

在此处输入图像描述

每条曲线有 51 个数据点(它们始终具有相同数量的数据点)。我现在正在做的事情是这样的:

public double CalculateArea(double[,] pin1, double[,] pin2)
{
    double voltageArea = 0;
    double currentArea = 0; //Current (Vertical axis) not yet!
    double max = 0;
    double min = 0;

    for (int i = 0; i < pin1.GetLength(0); i++)
    {
        max = Math.Max(Math.Abs(pin1[i, 0]), Math.Abs(pin2[i, 0]));
        min = Math.Min(Math.Abs(pin1[i, 0]), Math.Abs(pin2[i, 0]));

        voltageArea += max - min;
    }

    return voltageArea;
}

该代码以某种方式起作用,请记住我对当前(垂直轴)不做任何事情。如果结果接近 0,例如 0.05,那么曲线之间的差异是可以忽略的。但我确信这不是正确的方法,我完全不知道我写的方法的结果是什么......似乎只是电压点之间的差异。

如果你能帮助我改进这种方法,我真的很感激。

4

1 回答 1

3

首先从另一个二极管中减去一个二极管得到值的差异。然后使用梯形规则,它考虑分段线性函数下的区域。

class Program
{
    /// <summary>
    /// Calculate integral with trapezoidal rule
    /// </summary>
    /// <param name="h">The step size in x-axis</param>
    /// <param name="y">The array of values to integrate</param>
    /// <returns>The area under the curve y[i,0]</returns>
    public static double Integrate(double h, double[,] y)
    {
        int N=y.GetLength(0);

        double sum=(y[0, 0]+y[N-1, 0])/2;

        for(int i=1; i<N-1; i++)
        {
            sum+=y[i, 0];
        }

        return h*sum;
    }

    static void Main(string[] args)
    {
        int N = 100;
        double[,] y=new double[N, 1];

        for(int i=0; i<y.GetLength(0); i++)
        {
            y[i, 0]=5+5*Math.Sin(2*Math.PI*i/(N-1));
        }

        double x_min=0.5;
        double x_max=3.5;
        double h = (x_max-x_min)/N;

        double area=Integrate(h, y);
        // expected answer is   area = 15.00
        // actual answer is     area = 14.85
    }
}
于 2012-07-02T13:19:17.480 回答