what is the best way to calculate the upper area between the closed curve and the line?
alpha = [0:pi/360:2*pi];
x = 2.*(1-cos(alpha)).*cos(alpha);
y = 2.*(1-cos(alpha)).*sin(alpha);
w = [-4:0.1:0.5];
z = 0.5*w;
plot(x,y);
hold on;
grid on;
plot(w,z);
Is it possible to solve it by using trapz, even if it is a closed curve and occupying both positive and negative y axis? I saw a lot of topics about just a closed curve or a curve and a line. But none about a clsed curve and a line.
Thanks.
You just need to use interp1 to calculate the area just for the range you want. Firstly you calculate the area between the curve and the x axis (over):
a1 = abs(trapz(x,y)/2);
Then the area between the line and the x axis (under) for x going from the intersection point to zero:
x3 = [-3.38 0];
r1 = interp1(x2,y2,x3);
a2 = abs(trapz(x3,r1));
And then the same thing for the rest of the curve under the x axis:
x4 = [-3.99 -3.38];
r2 = interp1(x,y,x4);
a3 = abs(trapz(x4,r2))/2;
And the total area would be:
at = a1 + a2 + a3;
And here the whole Code:
clear all;
clc;
z = 0:0.01:2*pi;
x = 2.*(1-cos(z)).*cos(z);
y = 2.*(1-cos(z)).*sin(z);
plot(x,y);
hold on;
grid on;
x2 = -4:0.01:0.5;
y2 = 0.5*x2;
plot(x2,y2);
a1 = abs(trapz(x,y)/2);
x3 = [-3.38 0];
r1 = interp1(x2,y2,x3);
a2 = abs(trapz(x3,r1));
x4 = [-3.99 -3.38];
r2 = interp1(x,y,x4);
a3 = abs(trapz(x4,r2))/2;
at = a1 + a2 + a3;