フィルターのクリア

How to find maximum vertical distance between reference line and a curve?

8 ビュー (過去 30 日間)
shagun chaudhary
shagun chaudhary 2021 年 8 月 19 日
コメント済み: shagun chaudhary 2021 年 8 月 20 日
Hi,
I have plotted curve and reference in one graph. I wanted to find the maximum distance between two the line and coresponding x value.
I have used this code.but my answer is not coming right, so can you please help me . I am also showing the graph.
X=e.X;
Y=e.Y;
plot(X,Y);
hold on;
x=e.x
y=e.y;
plot (x,y);
[M,I] = max(abs(Y-y));
display([M,X(I)]);
  1 件のコメント
Fangjun Jiang
Fangjun Jiang 2021 年 8 月 19 日
Is there an error message? The code looks right, except for a case where Y is a row vector but y is a column vector. Then there would be no error but the result is out of whack.

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

採用された回答

DGM
DGM 2021 年 8 月 19 日
編集済み: DGM 2021 年 8 月 19 日
Something like this:
% sample vectors don't share the same x-positions or length
% assuming both x vectors are monotonic increasing
x1 = [1 2 3 4 5];
y1 = [1 2 3 4 5];
x2 = [1.8 3 3.6 4.5 5];
y2 = [3 3.2 4 4.3 5];
% these are the sample locations in the overlap region
xs = unique(sort([x1,x2]));
xs = xs(xs>=max(x1(1),x2(1)) & xs<=min(x1(end),x2(end)));
% calculate distances, maximize
y1f = interp1(x1,y1,xs);
y2f = interp1(x2,y2,xs);
d = y2f - y1f
d = 1×7
1.2000 1.0333 0.2000 0.4000 0.1333 -0.2000 0
max(d)
ans = 1.2000
% plot the two curves and show the distances
plot(x1,y1,'ro-',x2,y2,'bo-'); hold on
for l = 1:numel(xs)
plot([1 1]*xs(l),[y1f(l) y2f(l)],'m:')
end
  5 件のコメント
DGM
DGM 2021 年 8 月 20 日
[~,idx] = max(d); % get location of max d
maxd = d(idx)
maxx = xs(idx)
shagun chaudhary
shagun chaudhary 2021 年 8 月 20 日
Thank you so much sir. It been great help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by