Search code examples
c++matlabcurvenumerical-integration

How can I find the area above the curve


I have a question about finding the area of rectangle S2 (above the curve). I want to find S1/S2 like (S - S2)/(S2), where S = S1 + S2.

I have 2 vectors of double (x;y) and I can find S1 + S2:

S = (x.back() - x[0])*(y.back() - y[0]))

Then I want to use numerical integration to find the whole area under the cruve S2, and then deduct z from S2:

z = (x.back() - x[0])*(y[0] - 0) , S2 = S2 - z

My question is: how to use numerical integration if I have no function, but have (x;y). For example, in matlab it looks like this with feval:

% Total area under the curve
ft = fittype('smoothingspline');
cf = fit(x,y,ft);
F = @(x) feval(cf,x);
S2 = quad(F,x(1),x(end));

In C++ I have:

#include "Functions.h"
std::vector<double>AreaRatio(std::vector<double>&x, std::vector<double>&y) {

double S(0.0), z(0.0), S2(0.), R(0.0);

S = (x.back() - x[0])*(y.back() - y[0]);
z = (x.back()*x[0])*(y[0]-0);
S2 = /.../ 
// Numerical methods (any library) to find the area under the curve, 
// but I don't know how to transfer function into function of Numerical integration, 
// because I have only coordinates. 
R = (S - S2) / S2;
return R;
}

Example


Solution

  • Not sure but I think you need to go one step further back to first principles of integration...what it looks like your trying to do is find the area under the graph... to do that you need to treat it as slices [integration is this concept taken to the point where the delta approaches 0]

    So calculate the area as little rectangles or evern better rectangles with triangles on top between each data point...

    i.e.

        for(loop over data)
        {
            area += (data[1] + data[0]) * time/distance between data[1] and data[0]
        }
    

    Once you have that subtract it from y_end * (x_end - x1)

    You'd use numerical integration to give you the values of data - but buy the looks of it your either measuring them or doing something else to generate them.