Ferdi
Ferdi

Reputation: 35

Integration returning required y value for predefined area

Given the following:

I would like to calculate:

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

Upvotes: 1

Views: 118

Answers (1)

Wolfie
Wolfie

Reputation: 30146

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

Upvotes: 1

Related Questions