フィルターのクリア

How to draw normal line at given points ?

18 ビュー (過去 30 日間)
MMSAAH
MMSAAH 2020 年 3 月 27 日
回答済み: michael scheinfeild 2021 年 8 月 28 日
Hello everyone,
I have a red curve that fit 4 bleu points. I want to draw normal at these points.
Here attached my curve.
and the x, y coordinates of the bleu points are :
x=[142;127;181;234];
y=[251;251;261;255];
So, please, how to get the normal line ?
I will be very grateful if anyone could help me.
  2 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 3 月 27 日
How did you create this curved line fitting through these points?
MMSAAH
MMSAAH 2020 年 3 月 30 日
Hello Ameer,
Here is my code:
x=[142;127;181;234]
y=[251;251;261;255]
p = polyfit(x, y, 1);
v = polyval(p, x);
xint = linspace(127,300,50)';
spl = spline(x,y);
figure
t=plot(y,x,'.',ppval(spl,xint),xint,'r-')
xlim([200 300])
ylim([50 234])

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 31 日
Try this:
x=[142;127;181;234];
y=[251;251;261;255];
[x, idx] = sort(x);
y = y(idx);
points = [x y];
xint = linspace(127,234,50)';
spl = spline(x,y);
tangent_vector = zeros(4,2);
for i=1:4
if i==4 % last polynomial need to evalauted at endpoint
deri_coef = polyder(spl.coefs(i-1,:));
tangent_vector(i, :) = [1 polyval(deri_coef, x(end)-x(end-1))];
else
deri_coef = polyder(spl.coefs(i,:));
tangent_vector(i, :) = [1 polyval(deri_coef, 0)];
end
end
normal_vec = [-tangent_vector(:,2) tangent_vector(:,1)];
start_points = points;
end_points = 10*normal_vec + points;
fig = figure;
ax = axes();
hold(ax);
t = plot(y,x,'.',ppval(spl,xint),xint,'r-');
for i=1:4
p1 = start_points(i,:);
p2 = end_points(i,:);
plot([p1(2) p2(2)], [p1(1) p2(1)], 'r', 'LineWidth', 2);
end
daspect([1 1 1]);
xlim([180 340])
ylim([100 250])
  2 件のコメント
MMSAAH
MMSAAH 2020 年 4 月 1 日
Thank you very much for the answer.It worked perfectly.
However, could you explain me more the for loop ? and why did you evaluated the polynome at endpoint ?
Thank you though!
Ameer Hamza
Ameer Hamza 2020 年 4 月 1 日
For loop is just calculating the tangent vector at each point, since we can calculate the normal vectors from tangent vectors. The spline function output 3 polynomials for 4 data points. So to compute tangent at 4 data points, we need to do it like this
datapoint # 1 -> polynomial # 1 (start point)
datapoint # 2 -> polynomial # 2 (start point) or polynomial # 1 (end point)
datapoint # 3 -> polynomial # 3 (start point) or polynomial # 2 (end point)
datapoint # 4 -> polynomial # 3 (end point)
Which shows where I evaluated the derivative of each polynomial

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

その他の回答 (1 件)

michael scheinfeild
michael scheinfeild 2021 年 8 月 28 日
https://michaelsheinfeild.medium.com/unit-normal-vector-to-curve-d63ef0124acd

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by