How to calculate the area between two curves + optimization.
2 views (last 30 days)
Show older comments

Hi! I am trying to calculate the two areas shown in red and blue. The two areas are separated by a straight black line.
1) I want to understand how can I calculate these areas? in my specific case (solutions I looked required to have an equations of curves and my green curve is constructed from data points) 2) Is it possible to determine the position of the vertical black line (shift on the x-axis) at which the two enclosed areas (blue + red segments) are equal?
Thank you!
0 Comments
Answers (1)
Star Strider
on 19 Mar 2018
One approach:
x = linspace(50, 150); % Create Data
y = 0.5 + 0.5*tanh(0.1*(x - 98)); % Create Data
intyf = cumtrapz(x, y)/trapz(x,y); % Normalised Integral
intyr = cumtrapz(1 - y)/trapz(1 - y); % Normalised Integral Of ‘Complement’
xdiv = interp1(gradient(intyf-intyr), x, 0); % Interpolate To Rind Inflection Point Of Difference = 0
figure(1)
plot(x, y, '-g')
hold on
plot([xdiv xdiv], ylim, '--k')
hold off
This calculates the normalised integrals of the actual and ‘complementary’ data (‘flipping’ the data vertically to get the complement, in order to calculate the area above the curve), subtracts them, calculates the gradient of the difference, and interpolates to find the inflection point of the gradient. The code assumes your data are always bounded by [0,1].
The calculation of ‘xdiv’, the x-coordinate corresponding to the equality of the areas, is limited by the resolution of your independent and dependent variables. The accuracy can be improved by interpolating on a finer independent variable vector. I leave you to determine the wisdom of this with respect to your data and experiment design.
0 Comments
See Also
Categories
Find more on Fit Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!