Dumbo
Dumbo

Reputation: 14122

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).

enter image description here

There are 51 data points for each curve (they always have same amount of data poitns). 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). If the result is somehing near 0 eg 0.05 then the difference between curves is negetable. 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.

Upvotes: 5

Views: 2118

Answers (1)

John Alexiou
John Alexiou

Reputation: 29264

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

Upvotes: 3

Related Questions