How to find the are between two graphs?

2 ビュー (過去 30 日間)
Angelavtc
Angelavtc 2020 年 4 月 27 日
回答済み: Angelavtc 2020 年 4 月 29 日
Hello to all!
How can I find the area in purple between the two graphs in green and blue? is there a exchange file that can do this for me? Considering y limits as [-3000,3000] ?
I attach the points data (x1,y1 definies one curve and x2,y2 the other).
Thanks in advance!
  1 件のコメント
KSSV
KSSV 2020 年 4 月 27 日
Read about polyarea, trapz.

サインインしてコメントする。

採用された回答

Angelavtc
Angelavtc 2020 年 4 月 29 日
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%1st graph %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t=25
x1=Array_jan_13{t}.Volume;
y1=Array_jan_13{t}.Price;
%x2=Array_jan_13_s{t}.Volume_s
%y2=Array_jan_13_s{t}.Price_s
line1 = 2500*ones(1, length(x1));
%line2 = 2500*ones(1, length(x2));
x_inter=intersectPoints_jan_13{t}.x0;
y_inter=intersectPoints_jan_13{t}.y0;
hFig = figure
plot(x1, y1, 'r-', 'LineWidth', 2)
hold on
%plot(x2, y2, 'g-', 'LineWidth', 2)
%hold on
plot(x1, line1, 'b', 'LineWidth', 2);
hold on
%plot(x2, line2, 'b', 'LineWidth', 2);
%hold on
plot(x_inter,y_inter,'r.','markersize',18)
%CALCULATE THE AREA ABOVE THE DEMAND CURVE UNTIL THE INTERSECTION POINT
n=50000
%INTERPOLATE VALUES
x1_new=linspace(x1(1),x_inter,n);
y1_new=interp1(x1,y1,x1_new);
line_fine1 = 2500*ones(1, length(x1_new));
hFig = figure
plot(x1_new, y1_new, 'r-', 'LineWidth', 2)
hold on
plot(x1_new, line_fine1, 'b', 'LineWidth', 2);
hold on
plot(x_inter,y_inter,'r.','markersize',18)
%Gives the position of the first element in y1_new that meets the
%condition of being less than the condition
indexLeft = find(y1_new < 2500, 1, 'first');
% Put up vertical where the are will be calculated
xline(x1_new(indexLeft), 'Color', 'm', 'LineWidth', 2);
%the last element in demand that intersects which will be the last element
%where the area will be calculated
xline(x1_new(end), 'Color', 'y', 'LineWidth', 2);
% COMPUTE AREAS BETWEEN CURVE AND FLAT BLUE LINE
% Find that area below the line at 2500
%asigna los valores de x_fine desde el primer elemento hasta
%uno antes de que la curva y fuera mayor a 1
x_area=x1_new(indexLeft : end);
y_area=line_fine1(indexLeft : end)-y1_new(indexLeft : end);
area_Loss_up = trapz(x_area, y_area)
% To double check, compute area by summing also.
area_Loss2_up = sum(y_area) * abs(x1_new(2) - x1_new(1)) % Should be fairly close.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%2ND GRAPH %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Calculate the area between the y=2500 and supply curve
%until the intersection point
t=25
x2=Array_jan_13_s{t}.Volume_s;
y2=Array_jan_13_s{t}.Price_s;
line2 = 2500*ones(1, length(x2));
x_inter=intersectPoints_jan_13{t}.x0
y_inter=intersectPoints_jan_13{t}.y0
hFig = figure
plot(x2, y2, 'r-', 'LineWidth', 2)
hold on
plot(x2, line2, 'b', 'LineWidth', 2);
hold on
plot(x_inter,y_inter,'r.','markersize',18)
%CALCULATE THE AREA ABOVE THE DEMAND CURVE UNTIL THE INTERSECTION POINT
n=50000;
%INTERPOLATE VALUES
x2_new=linspace(x_inter, x2(end),n);
y2_new=interp1(x2,y2,x2_new);
line_fine2 = 2500*ones(1, length(x2_new));
hFig = figure
plot(x2_new, y2_new, 'r-', 'LineWidth', 2)
hold on
plot(x2_new, line_fine2, 'b', 'LineWidth', 2);
hold on
plot(x_inter,y_inter,'r.','markersize',18)
%Gives the position of the last element in y2_new that meets the
%condition of being less than the condition
indexLeft = find(y2_new < 2500, 1, 'last');
% Put up vertical where the are will be calculated
xline(x2_new(indexLeft), 'Color', 'm', 'LineWidth', 2);
%the first element in demand that intersects which will be the last element
%where the area will be calculated
xline(x2_new(1), 'Color', 'y', 'LineWidth', 2);
% COMPUTE AREAS BETWEEN CURVE AND FLAT BLUE LINE
% Find that area below the line at 2500
x_area_s=x2_new(1 : indexLeft);
y_area_s=line_fine2(1 : indexLeft)-y2_new(1 : indexLeft);
area_Loss_s_up = trapz(x_area_s, y_area_s)
% To double check, compute area by summing also.
area_Loss2_s_up= sum(y_area_s) * abs(x2_new(2) - x2_new(1))
Total_area_up=area_Loss_up+area_Loss_s_up
Total_area2_up=area_Loss2_up+area_Loss2_s_up

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by