Calculation of the area between two curves

I am working on a program that compares two curves (as a result of the output of a diode, therefore its voltage / current curve).

I want to calculate the area between these two curves (the BLUE curve is the first diode, and RED is the second).

enter image description here

There are 51 data points for each curve (they always have the same amount of data). What I'm doing at the moment is as follows:

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, bearing in mind that I am not doing anything with Current (vertical axis). If the result is close to 0, for example, 0.05, then the difference between the curves is imperceptible. But I'm sure this is not the right way, I don’t know at all what is the result of the method I wrote ... it seems the difference between the voltage points.

I really suggest if you can help me improve this method.

+5
1

, . , - .

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

All Articles