简体   繁体   English

计算两条曲线之间的面积

[英]Calculating area between two curves

I am working on a program that compares two curve (resulted from diode output so its Voltage/Current curve). 我正在研究一个比较两条曲线的程序(由二极管输出产生的电压/电流曲线)。

I want to calculate the area between this two curves (BLUE curve is first diode and RED is the second one). 我想计算这两条曲线之间的面积(蓝色曲线是第一个二极管,RED是第二个曲线)。

在此输入图像描述

There are 51 data points for each curve (they always have same amount of data poitns). 每条曲线有51个数据点(它们总是具有相同数量的数据)。 What I am doing at the moment is like this: 我现在正在做的是这样的:

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;
}

This code somehow works, having in mind I do nothing with Current (vertical axis). 这段代码以某种方式工作,记住我对Current(垂直轴)什么都不做。 If the result is somehing near 0 eg 0.05 then the difference between curves is negetable. 如果结果接近0,例如0.05,那么曲线之间的差异是可以得到的。 But I am sure this is not the correct way, I totaly have no idea what is the result of the method I wrote...seems to be difference between Voltage points only. 但我确信这不是正确的方法,我完全不知道我写的方法的结果是什么......似乎只是电压点之间的区别。

I really appriciate if you can help me improve this method. 如果你能帮助我改进这种方法,我真的很高兴。

First subtract one diode from the other do get the difference in values. 首先从另一个中减去一个二极管,得到值的差异。 Then use the trapezoidal rule, which considers the area under a piece-wise linear function. 然后使用梯形规则,该规则考虑分段线性函数下的面积。

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM