Search code examples
matlabareanumerical-integration

Integration returning required y value for predefined area


Given the following:

  • Time series with x (time [s]) and y (here discharge [m³/s])

  • Value V1 (same units integrated y), smaller than the integral over all of x. In this case a small volume [m³].

I would like to calculate:

  • The y value y_V1 such that the integral between the line y = y_V1 and the curve y equals V1.

    The following plot shows this, the orange region is V1, I want the circled value on the y axis:

    Find corrsponding y

  • The V1 must be placed around the peak.

I think this must be an iterative process, where also a the fitting criteria (and the degree of exactness) must be set by the user.

Until now, I haven't found a way to start; besides the pure integration.

The idea is to specify an area. The y value left and right of the peak which envelops this area should be calculated.


Edit

This is the result, if the accepted answer is applied.

result


Solution

  • You can do this by decreasing some y value until your area target is met. See the comments below for details.

    % Input data
    x = 0:0.01:pi;
    y = sin(x);
    
    target = 1;     % Target area
    
    yi = max( y );  % Initialise yi to be max possible y
    dy = 0.001;     % Step change in yi
    
    Ai = 0;         % Area each iteration
    thresh = 0;     % Threshold for stopping loop
    while target - Ai > thresh && yi >= min(y)
        yi = yi - dy;
        ix = y >= yi;
        % Approximate integral above the line
        Ai = trapz( x(ix), y(ix) - yi ); 
    end
    
    % Plot
    figure(1); clf; hold on
    plot( x, y );
    patch( x(ix), y(ix), [1,0.5,0.5], 'facealpha', 0.5 );
    plot( x, ones(size(x))*yi, '--', 'linewidth', 2 )
    xlim( [min(x),max(x)] )
    

    Output:

    plot