calculate area between curves

how to calculate area between these two curves??

1 Comment

Can you include your data? You may also want to clarify if you need to consider the negative area (blue above red region).

Sign in to comment.

 Accepted Answer

Hi!
You can use trapz function inside MATLAB for this task. It will perform the integration taking the intersection points and will give you the area under the curve.
Here is a snippet of code that might help
x = linspace(0, 10, 100);
y1 = sin(x);
y2 = cos(x);
%Plot the curves
plot(x, y1)
hold on
plot(x, y2)
% Find intersection points (assuming they are known)
idx1 = 20;
idx2 = 70;
% Calculate the area
area = trapz(x(idx1:idx2), abs(y1(idx1:idx2) - y2(idx1:idx2)));
disp(['Area between curves: ', num2str(area)]);
You can also refer to the trapz function in the MATLAB for further usage according to your code. I hope this might help you!
Thanks

5 Comments

pmax =[71.42*42 80*42 90.3*42 94.71*42 101.64*42 104.16*42 108.315*42 109.62*42 113.85*42 116.095*42 119.88*42 122.1*42 123.76*42 126.56*42 129.39*42 129.39*42 129.39*42 129.39*42 126.56*42 123.76*42 122.1*42 116.095*42 109.62*42 90.3*42 80*42 71.428*42 ];
t =[7 8 9.10 9.20 9.30 9.40 9.50 10.00 10.10 10.30 10.45 11.00 11.15 11.45 12.15 13.20 14 14.30 15 15.30 16 16.30 17 18 19 19.85 ];
hold on
plot(t,pmax,'r')
hold off
data=[0 6 900;
6 7 2400;
7 15 3000;
15 18 1800;
18 22 3000;
22 24 1800];
power=data(:, 3)
Dt=data(:,2) - data(:,1)
Power_Generated=power.*Dt
Total_power1=sum(Power_Generated)
Total_power2=power.*Dt;
DATA=[data(:,1) data(:,2) power];
Average_load=Total_power2/sum(Dt);
peak_load=max(power);
Daily_LF=Average_load/peak_load*100;
Results=[Average_load peak_load*ones(size(Daily_LF)) Daily_LF];
L=length(data);
timeinterval=data(:,1:2) ;
t = sort(reshape(timeinterval,1,2*L));
for n = 1:L
P(2*n-1) = power(n);
P(2*n) = power(n);
end
hold on
plot(t,P,'b')
hold off
xlim([0 24])
grid on
grid minor
it doesn't work
Try this out.
pmax =[71.42*42 80*42 90.3*42 94.71*42 101.64*42 104.16*42 108.315*42 109.62*42 113.85*42 116.095*42 119.88*42 122.1*42 123.76*42 126.56*42 129.39*42 129.39*42 129.39*42 129.39*42 126.56*42 123.76*42 122.1*42 116.095*42 109.62*42 90.3*42 80*42 71.428*42 ];
t =[7 8 9.10 9.20 9.30 9.40 9.50 10.00 10.10 10.30 10.45 11.00 11.15 11.45 12.15 13.20 14 14.30 15 15.30 16 16.30 17 18 19 19.85 ];
hold on
plot(t,pmax,'r')
hold off
data=[0 6 900;
6 7 2400;
7 15 3000;
15 18 1800;
18 22 3000;
22 24 1800];
power=data(:, 3)
Dt=data(:,2) - data(:,1)
Power_Generated=power.*Dt
Total_power1=sum(Power_Generated)
Total_power2=power.*Dt;
DATA=[data(:,1) data(:,2) power];
Average_load=Total_power2/sum(Dt);
peak_load=max(power);
Daily_LF=Average_load/peak_load*100;
Results=[Average_load peak_load*ones(size(Daily_LF)) Daily_LF];
L=length(data);
timeinterval=data(:,1:2) ;
t = sort(reshape(timeinterval,1,2*L));
for n = 1:L
P(2*n-1) = power(n);
P(2*n) = power(n);
end
hold on
plot(t,P,'b')
hold off
xlim([0 24])
grid on
grid minor
% Finding the area under the intersection
intersection = min(P(1:L), pmax(1:L)); % Select the minimum values between P and pmax
area_under_intersection = trapz(t(1:L), intersection); % Calculate the area using the trapz function
disp(['Area under intersection: ', num2str(area_under_intersection)]);
In the code, I assume that the arrays P and pmax have the same number of elements as the length of data (denoted by L). By indexing P and pmax with (1:L), we ensure that the arrays have compatible sizes for the intersection calculation.
Please make sure that the arrays P and pmax have the correct sizes and try running the updated code.
Thanks :)
i want to ask the area calculated here is the selected section??

Sign in to comment.

More Answers (1)

Anamika
Anamika on 17 Jul 2023
To calculate the area between two curves in MATLAB, you can use the `trapz` function. Here's a step-by-step guide:
1. Define the x-values and the two curves, let's call them `y1` and `y2`. Make sure the curves have the same length and correspond to the same x-values.
2. Calculate the absolute difference between the two curves to obtain the vertical distance between them. Let's call it `diff_y`.
3. Use the `trapz` function to integrate the `diff_y` over the x-range. This will give you the area between the curves.
Here's an example code which shows this process:
% Defining the x-values
x = 0:0.1:10;
% Defining the two curves
y1 = sin(x);
y2 = cos(x);
% Calculating the difference between the two curves
diff_y = abs(y1 - y2);
% Calculate the area between the curves using trapz
area_between_curves = trapz(x, diff_y);
% Display the result
disp(area_between_curves);
In this example, it's calculating the area between the curves `y1` and `y2`, which are the sine and cosine functions, respectively. The `trapz` function integrates the absolute difference between the curves over the range of x-values.

Products

Tags

Asked:

on 11 Jul 2023

Answered:

on 17 Jul 2023

Community Treasure Hunt

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

Start Hunting!