How to Calculate Area Between a Curve and Two Lines?

Hi Everyone,
I am trying to calculate area (yellow area in attached figure) between curve and vertical and horizental axies lines. the code is provided as below. Could anyone help me?
Thank you in advance for you help.
Thank you,
clear all
clc
close all
%%
m=60;L=1;J=60;g=9.81;beta2=411.67;
KPb=2.15;KDb=0.75;
Time_Delay_Brain=0.19;
Time_Delay_Exo=0.05;
%%
for j=1:length(Time_Delay_Brain)
for jj=1:length(Time_Delay_Exo)
if Time_Delay_Exo(jj) ==0.05
n=11;
else
n=8;
end
%%
tau1=Time_Delay_Brain(j);tau2=Time_Delay_Exo(jj);
W = linspace(0, n*pi, 1000);omega=W;
Kpe=@(omega)J.*cos(omega.*tau2).*omega.^2 - KDb.*beta2.*omega.*sin(omega.*tau1 - omega.*tau2) + L.*cos(omega.*tau2).*g.*m - KPb.*beta2.*cos(omega.*tau1 - omega.*tau2);
Kde=@(omega)(omega.^2.*J.*sin(omega.*tau2) - KDb.*beta2.*omega.*cos(omega.*tau1 - omega.*tau2) + m.*g.*L.*sin(omega.*tau2) + KPb.*beta2.*sin(omega.*tau1 - omega.*tau2))./omega;
%%
D_Curve_PD_KPe=Kpe(omega);
D_Curve_PD_KDe=Kde(omega);
%%
plot(D_Curve_PD_KPe,D_Curve_PD_KDe,'color',[2*Time_Delay_Exo(jj) 2*Time_Delay_Brain(j) Time_Delay_Exo(jj) *Time_Delay_Brain(j)],'LineWidth', 0.2)
axis([0,18000,0,2500]);
hold on
end
hold on
end
plot([0 18000],[0 0],'color',[1 0 0],'LineWidth', 3)
plot([0 0],[0 2500],'color',[1 0 0],'LineWidth', 3)

 Accepted Answer

At the end of your code, add...
x = D_Curve_PD_KPe;
y = D_Curve_PD_KDe;
cropLogical = x > 0;
x = x(cropLogical);
y = y(cropLogical);
% define vertices of polyshape and plot
x = [0 0 x x(end) 0];
y = [0 y(1) y 0 0];
ps = polyshape(x,y);
plot(ps, 'facecolor', 'y');
% compute and output area
a1 = polyarea(x,y);
fprintf('area = %.2f\n', a1);
Output:
area = 16482962.65

3 Comments

Thank you for your respone, I appreciate it. However, as I change the Time_Delay_Exo from 0.05 to 0.25, I will get the wrong answer. Actually, I need two conditions for cropLogical = x > 0; (for example cropLogical = x > 0 && cropLogical = y > 0; simultaneously) I will copy past the code again. Basicly, I need the area of curve that mentioned in following attached figures.
Thank you!
clear all
clc
close all
%%
m=60;L=1;J=60;g=9.81;beta2=411.67;
KPb=2.15;KDb=0.75;
% Time_Delay_Brain=0.19:0.001:0.2;
% Time_Delay_Exo=0.05:0.02:0.25;
Time_Delay_Brain=0.19;
Time_Delay_Exo=0.25;
%%
figure('DefaultAxesFontSize',15,'DefaultAxesFontName','Times')
for j=1:length(Time_Delay_Brain)
for jj=1:length(Time_Delay_Exo)
if Time_Delay_Exo(jj) ==0.05
n=11;
else
n=8;
end
%%
tau1=Time_Delay_Brain(j);tau2=Time_Delay_Exo(jj);
W = linspace(0, n*pi, 1000);omega=W;
Kpe=@(omega)J.*cos(omega.*tau2).*omega.^2 - KDb.*beta2.*omega.*sin(omega.*tau1 - omega.*tau2) + L.*cos(omega.*tau2).*g.*m - KPb.*beta2.*cos(omega.*tau1 - omega.*tau2);
Kde=@(omega)(omega.^2.*J.*sin(omega.*tau2) - KDb.*beta2.*omega.*cos(omega.*tau1 - omega.*tau2) + m.*g.*L.*sin(omega.*tau2) + KPb.*beta2.*sin(omega.*tau1 - omega.*tau2))./omega;
%%
D_Curve_PD_KPe=Kpe(omega);
D_Curve_PD_KDe=Kde(omega);
%%
plot(D_Curve_PD_KPe,D_Curve_PD_KDe,'color',[2*Time_Delay_Exo(jj) 2*Time_Delay_Brain(j) Time_Delay_Exo(jj) *Time_Delay_Brain(j)],'LineWidth', 0.2)
xlabel ('Exo Kp','FontSize',18, 'FontName', 'Times')
ylabel ('Exo Kd','FontSize',18, 'FontName', 'Times')
title('Kd vs Kp for Analytical Solution', 'FontName', 'Times','FontSize',14)
% axis([0,18000,0,2500]);
hold on
end
hold on
end
x = D_Curve_PD_KPe;
y = D_Curve_PD_KDe;
cropLogical = x > 0 ; %(I need "cropLogical = y > 0" Condition as well)
x = x(cropLogical);
y = y(cropLogical);
% define vertices of polyshape and plot
x = [0 0 x x(end) 0];
y = [0 y(1) y 0 0];
ps = polyshape(x,y);
plot(ps, 'facecolor', 'y');
% compute and output area
a1 = polyarea(x,y);
fprintf('area = %.2f\n', a1);
It sounds like you want the area in region I -- with 0,0 at the bottom-left. In this case, modify my code by changing
cropLogical = x > 0 ;
to
cropLogical = x > 0 & y > 0 ;
Thank you so much. It does work! You saved me

Sign in to comment.

More Answers (0)

Products

Release

R2019a

Tags

Asked:

N/A
on 15 Aug 2021

Commented:

N/A
on 16 Aug 2021

Community Treasure Hunt

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

Start Hunting!