Area between two curves that intersect

9 ビュー (過去 30 日間)
Harry Smith
Harry Smith 2019 年 2 月 21 日
コメント済み: Harry Smith 2019 年 2 月 21 日
Hi, i'm trying to find the area between these two plots, i need to find the area of each individual section where the plots overlap. For some reason my 'trapz' commands aren't working. All help would be appreciated. If more info is needed about the program just let me know and i'll see what i can do.
lprofile=xlsread(fname_lprofile);
x1=lprofile(:,1)
y1=lprofile(:,2)
y2=lprofile(:,3)
plot(x1,y1,x1,y2)
x3=2
ex_area=0
fl_area=0
while x3>1 & x3<(n1-1)
if lprofile(x3,2)>lprofile(x3,3)
a1=trapz (lprofile(x3,1),lprofile(x3,2),2)
ex_area=ex_area+a1
x3=x3+1
elseif lprofile(x3,3)>lprofile(x3,2)
a2=trapz(x3,3)
fl_area=fl_area+a2
x3=x3+1
else
x3=x3+1
end
end

採用された回答

Rik
Rik 2019 年 2 月 21 日
You are using the loop incorrectly, and you don't need it anyway. You are doing an integration over a single point, which is unlikely to work in any context. You could sum the values and divide by the delta x, but by not using array operations you're not taking advantage of the full potential of Matlab.
The method I show below will have a rounding error that increases with the number of crossings, but it shouldn't get too bad, especially if you're using a finer x scale.
% lprofile=xlsread(fname_lprofile);
% x1=lprofile(:,1);
% y1=lprofile(:,2);
% y2=lprofile(:,3);
% plot(x1,y1,x1,y2)
%generate random data instead
x1=linspace(0,2*pi,300);
y1=cos(x1);
y2=sin(x1);
plot(x1,y1,x1,y2)
low=y1-y2;low(low<0)=0;
hi=y2-y1;hi(hi<0)=0;
A_low=trapz(x1,low);
A_hi=trapz(x1,hi);
  1 件のコメント
Harry Smith
Harry Smith 2019 年 2 月 21 日
Hi,
thank you for your quick answer to my question, it seems so simple when you point it out. I'm used to coding in python, rather than matlab, which is probably why i was trying to loop through it and why i didn't use matrix manipulation (plus matrices is a topic i'm fairly new to).
many thanks.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by