Area between two curves without intersection

5 views (last 30 days)
Crocola Cool
Crocola Cool on 31 May 2021
Edited: Paul on 1 Jun 2021
Hi everyone.
I would like to calculate the area between two curves (see attachment).
I have used the trapz and polyarea function but these do not work because the curves are superimposed on each other without intersection.
Could someone please help me?
x=[0,-1.66128688049154,-3.71843384492024,-6.03903044153544,-8.52179344691878,-11.0684783490837,-13.5546470968919,-15.8324222826000,-17.7183932651871,-19.0241469744085,-19.5928500159198,-19.2715861063891,-18.0007426019886,-15.8645181604802,-13.1050363442789,-10.1023488165208,-7.36091774112053,-5.20617947547990,-3.73171179000825,-2.80200973068434,-2.17303027950468,-1.64687923319577,-1.14657359693582,-0.683525804975150,-0.261378364022391,0.167766706401400,0.646305657504070,1.13828938838504,1.49690490570653,1.50928713174259,0.991312332299208];
y=[0,3.80978510632932,8.62533289690098,13.7146367945814,18.4791657883180,22.5818350036559,26.0354921546852,29.1006123032565,31.9144606689929,33.8206797037834,33.4764658683987,29.5025727265991,21.5010392613040,10.9350767496541,13.3409505060801,18.4784450622125,22.0669438223010,22.8065275890501,20.8225751873008,17.4709163499800,14.5397350670517,13.2179835701845,13.4862976982229,14.3068759442281,14.3461752980756,12.9833980247855,10.3519556168192,7.26333666757206,4.38583660003191,2.05034193641872,0.472418853310666];

Answers (2)

darova
darova on 31 May 2021
Make sure curves have the same start and end
xx = linspace(x1(1),x1(end),100); % new mesh
y11 = interp1(x1,y1,xx); % interpolate curve1
y22 = interp1(x2,y2,xx); % interpolate curve2
A = trapz(xx,abs(y22-y11)); % calculate positive area
  2 Comments
Torsten
Torsten on 31 May 2021
x and y are not two different curves, but one curve given in a (x,y) representation (like e.g. (x,y) = (cos(t),sin(t)) for a circle)
Crocola Cool
Crocola Cool on 31 May 2021
#Darova
Thanks for your feedback but it doesn't work with your proposal. You should not separate the x and y data. this said, (x,y).
#code
x=[0,-1.66128688049154,-3.71843384492024,-6.03903044153544,-8.52179344691878,-11.0684783490837,-13.5546470968919,-15.8324222826000,-17.7183932651871,-19.0241469744085,-19.5928500159198,-19.2715861063891,-18.0007426019886,-15.8645181604802,-13.1050363442789,-10.1023488165208,-7.36091774112053,-5.20617947547990,-3.73171179000825,-2.80200973068434,-2.17303027950468,-1.64687923319577,-1.14657359693582,-0.683525804975150,-0.261378364022391,0.167766706401400,0.646305657504070,1.13828938838504,1.49690490570653,1.50928713174259,0.991312332299208];
y=[0,3.80978510632932,8.62533289690098,13.7146367945814,18.4791657883180,22.5818350036559,26.0354921546852,29.1006123032565,31.9144606689929,33.8206797037834,33.4764658683987,29.5025727265991,21.5010392613040,10.9350767496541,13.3409505060801,18.4784450622125,22.0669438223010,22.8065275890501,20.8225751873008,17.4709163499800,14.5397350670517,13.2179835701845,13.4862976982229,14.3068759442281,14.3461752980756,12.9833980247855,10.3519556168192,7.26333666757206,4.38583660003191,2.05034193641872,0.472418853310666];
time=[10,39,69,99,129,158,188,218,248,277,307,337,367,397,426,456,486,516,545,575,605,635,665,694,724,754,784,813,843,873,903];
figure(1)
plot(x,y,'-x');
tq=min(time):1:max(time);
interp_x = interp1(time,x,tq);
interp_y= interp1(time,y,tq);
figure(2)
plot(interp_x,interp_y,'-O')
A=trapz(tq,abs(interp_y-interp_x))

Sign in to comment.


Paul
Paul on 31 May 2021
I think this is what you're looking for:
x=[0,-1.66128688049154,-3.71843384492024,-6.03903044153544,-8.52179344691878,-11.0684783490837,-13.5546470968919,-15.8324222826000,-17.7183932651871,-19.0241469744085,-19.5928500159198,-19.2715861063891,-18.0007426019886,-15.8645181604802,-13.1050363442789,-10.1023488165208,-7.36091774112053,-5.20617947547990,-3.73171179000825,-2.80200973068434,-2.17303027950468,-1.64687923319577,-1.14657359693582,-0.683525804975150,-0.261378364022391,0.167766706401400,0.646305657504070,1.13828938838504,1.49690490570653,1.50928713174259,0.991312332299208];
y=[0,3.80978510632932,8.62533289690098,13.7146367945814,18.4791657883180,22.5818350036559,26.0354921546852,29.1006123032565,31.9144606689929,33.8206797037834,33.4764658683987,29.5025727265991,21.5010392613040,10.9350767496541,13.3409505060801,18.4784450622125,22.0669438223010,22.8065275890501,20.8225751873008,17.4709163499800,14.5397350670517,13.2179835701845,13.4862976982229,14.3068759442281,14.3461752980756,12.9833980247855,10.3519556168192,7.26333666757206,4.38583660003191,2.05034193641872,0.472418853310666];
plot(x,y,'-+')
p=polyshape(x,y);
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
plot(p)
p.area
ans = 202.0491
  8 Comments
Torsten
Torsten on 1 Jun 2021
I'd estimate the length of the big region as 10 and its height as 4, and 10x4 = 40. So no, the area of this example will be much smaller than the area for the first one.
Paul
Paul on 1 Jun 2021
Edited: Paul on 1 Jun 2021
Plotting both shows that area in example 2 is much smaller than in example 1.
x1=[0,-1.66128688049154,-3.71843384492024,-6.03903044153544,-8.52179344691878,-11.0684783490837,-13.5546470968919,-15.8324222826000,-17.7183932651871,-19.0241469744085,-19.5928500159198,-19.2715861063891,-18.0007426019886,-15.8645181604802,-13.1050363442789,-10.1023488165208,-7.36091774112053,-5.20617947547990,-3.73171179000825,-2.80200973068434,-2.17303027950468,-1.64687923319577,-1.14657359693582,-0.683525804975150,-0.261378364022391,0.167766706401400,0.646305657504070,1.13828938838504,1.49690490570653,1.50928713174259,0.991312332299208];
y1=[0,3.80978510632932,8.62533289690098,13.7146367945814,18.4791657883180,22.5818350036559,26.0354921546852,29.1006123032565,31.9144606689929,33.8206797037834,33.4764658683987,29.5025727265991,21.5010392613040,10.9350767496541,13.3409505060801,18.4784450622125,22.0669438223010,22.8065275890501,20.8225751873008,17.4709163499800,14.5397350670517,13.2179835701845,13.4862976982229,14.3068759442281,14.3461752980756,12.9833980247855,10.3519556168192,7.26333666757206,4.38583660003191,2.05034193641872,0.472418853310666];
x2=[0,-1.58143962981297,-3.03392588365747,-4.31293028513336,-5.43316939640765,-6.43266612032772,-7.34751230187266,-8.19610716571275,-8.97053149511526,-9.63779510279898,-10.1519335567333,-10.4585616901778,-10.5342395749024,-10.3758946306706,-9.98217498098106,-9.34944176705073,-8.47251867761138,-7.35956551245431,-6.05095668830785,-4.62502866942295,-3.18355609435549,-1.82611854945020,-0.611557284260889,0.477054737792964,1.47779860348098,2.40953293828952,3.22796956291444,3.81329910953911,4.00379047888411,3.66637669160807,2.77294873115999,1.43501593689118];
y2=[0,-0.0914589891686976,-0.169258183169858,-0.137641932843007,0.115356308463203,0.650321396134900,1.44445426860180,2.40398635583359,3.39379566406802,4.25865376154095,4.84684849930363,5.03502448735570,4.83627564548227,4.37126677838120,3.80309691225722,3.29016826646019,2.94696153078223,2.83659552454877,2.98508831160315,3.38247203682421,3.96326780273134,4.58673760893439,5.02975831885881,5.09870485313676,4.70518969374756,3.90891301840727,2.89444462683932,1.89004771352925,1.07492003950258,0.526156770959942,0.222079396840722,0.0776605885521645];
plot(x1,y1,'-x',x2,y2,'-o'),grid

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!