이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Finding distance between two curves at diffrent points
조회 수: 22 (최근 30일)
이전 댓글 표시
Praveen Patnaik
2020년 3월 12일
1.Suppose the curves are defined, how do i find the distance between two curves at diffrent points.
Curve 1= f(x)
curve 2 = g(x)
Do i need to define points on one of the curve to find out the required distance???
For exmple I am posting an image.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276770/image.png)
Important note-
I need distance at diffrent points between two curves and not single point nor am I looking for minimum distance between curves.
Perpendicular distance from tangent as shown in image.
댓글 수: 2
Praveen Patnaik
2020년 3월 12일
It is just a general concept.... These are just images. Suppose the function of these curves are defined . Then how will we do it???
채택된 답변
darova
2020년 3월 12일
With use of polyxpoly
clc,clear
x = 0:0.1:10;
y1 = sin(x); % data 1
y2 = sqrt(x); % data 2
% index of point
ix = 70;
x0 = x(ix);
y0 = y2(ix);
% tangent vector
dy = diff(y2(ix:ix+1));
dx = diff(x(ix:ix+1));
% normal vector
xv = [0 dy]*50+x0;
yv = [0 -dx]*50+y0;
% intersection point
[xc,yc] = polyxpoly(xv,yv,x,y1);
plot(x,y1,x,y2)
hold on
plot(xv,yv)
plot(xc,yc,'or')
hold off
axis equal
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276846/image.png)
댓글 수: 28
Praveen Patnaik
2020년 3월 13일
Can this also be done for other points on green line??
any command that can take plot 10 points on green line randomly and then after we get those 10 points we would store them and then apply tangent and normal to get distance???.....
Praveen Patnaik
2020년 3월 13일
편집: darova
2020년 6월 20일
clc,clear
x = (0:0.1:10)';
y1 = sin(x); % data 1
y2 = sqrt(x); % data 2
% index of point
ind = randi(length(x),10,1); % 10 random points
XC = ind*nan;
YC = ind*nan;
for i = 1:length(ind)
ix = ind(i);
x0 = x(ix);
y0 = y2(ix);
% tangent vector
dy = diff(y2(ix:ix+1));
dx = diff(x(ix:ix+1));
% normal vector
xv = [0 dy]*50+x0;
yv = [0 -dx]*50+y0;
% intersection point
[xc,yc] = polyxpoly(xv,yv,x,y1);
if ~isempty(xc)
XC(i) = xc;
YC(i) = yc;
end
end
plot(x,y1,x,y2)
hold on
plot([XC x(ind)]',[YC y2(ind)]','.-r')
hold off
axis equal
This above script you sent worked......(Thanks a ton)
Any modification that I can get on above script so that I would get the value of length of those normal lines and add it as a sum and store it in one variable
darova
2020년 3월 13일
x(ind) and y2(ind) are points on a curve
XC and YC are intersection points
D = hypot(x(ind)-XC,y(ind)-YC); % some of them can be NaN (no intersections)
ii = isnan(D);
sumD = sum(D(~ii)); % sum only number (without NaN)
Praveen Patnaik
2020년 3월 13일
The y variable is not defined.....sorry for bothring you , but i am running the code line by line and trying to understand .
Praveen Patnaik
2020년 3월 13일
sorry got the answer right.....its y2 in place of y....
Thanks a Ton Darova.
Praveen Patnaik
2020년 3월 15일
With this similar approach Can you look into another optimization problem I have posted...???
Kindly look into the post as I am unable to understand the answer to solve it...
Kindly look into it and write the script for same if you can understand it....
darova
2020년 3월 15일
I don't know how to solve the problem you posted. The only idea i have it's to use my script
Ron Herman
2020년 5월 11일
편집: Ron Herman
2020년 5월 11일
Can you tell me in your script why you multiplied 50 in normal vector???
can you explain what is xv,yv???
what is dy and dx??
unable to understand how you found out tangent and normal vector.
Can you write few comments on these codes so I may get better clarity??
thank you in advance
darova
2020년 5월 11일
- Can you tell me in your script why you multiplied 50 in normal vector???
Because dx and dy are small so i wanted to the line longer
- can you explain what is xv,yv???
xv and yv are coordinates of two points (line between (x(i),y(i)) and (x(i)+50*dx,y(i)+50*dy))
- what is dy and dx??
Look on the picture
- unable to understand how you found out tangent and normal vector.
Tangent vector is just a difference between (i) and (i+1) points
Normal vector is tangent vector rotated on 90 degree. If tangent vector [dx,dy] then normal vector is [dy;-dx]
Here is a simple scheme
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/292075/image.png)
Image Analyst
2020년 5월 11일
편집: darova
2020년 5월 11일
Pretty smart and simple solution. Learned something new. THanks
Ron Herman
2020년 6월 19일
I am following the code you have given but I am facing a problem at a cetain point. For reference I put the image.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/319171/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/319171/image.png)
I am drawing normal from small curve based on tangent at certain points. As per code the normal when we draw goes only in one direction from left to right. Problem arises when the small curve is to the right of big circle ,since tangent can only be drawn from left to right and not in both directions. When curve is towards right of big circle the normal from small curve dosent intersect the big circle but when small curve is to the left of big circle the normal from small curve intersects big circle as shown in figure.
Any modification in above code that will enable normal line to extend in both direction so that we will always get an intercept.
Ron Herman
2020년 6월 19일
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/319174/image.png)
The code that you wrote is for the blue line. Can any modificaion be done so that the normal line will extend in both the direction. (it means a normal line that it will extend along red line as well as blue line)
Ron Herman
2020년 6월 20일
Thanks a ton darova it worked. I have understood the way how code works. Its quite innovative
Ron Herman
2020년 6월 20일
편집: Ron Herman
2020년 6월 21일
I modified the code and made my own curves, but I am getting an unexpected output as shown in figure. Can you point out the mistake as code is exacly not working as shown in above example.
% End part of code
plot(x,y,x1,y1)
hold on
plot([XC x1(ind)]',[YC y1(ind)]','.-r') % x(ind)& y2(ind)are points on the curve
hold off % XC and YC are intersection points
axis equal
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/319498/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/319501/image.png)
For single values of "ind" like ind=[20] or ind=[80] I am getting normal from curve intersecting the circle but when I take two index values ind= [20 80] in "ind" I dont get desired output.
What is the reason??? Is it something to do with polyxpoly?? ( I read its syntax but unable to find the problem here)
Ron Herman
2020년 6월 21일
Thanks a ton. You have always been a great help with your response .Let me know if I could be of any help from my end.
Ron Herman
2020년 7월 3일
편집: Ron Herman
2020년 7월 4일
When I write this all in one code I get the desired output but same doesnot happen when I break this code into functions (Why)????
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326075/image.png)
Breaking above code into functions doesnot give me the desired result (Why)????
Ron Herman
2020년 7월 4일
편집: Ron Herman
2020년 7월 4일
I suppose I have got the code correct.
x1 becomes x_1 inside intercection function as x_1=x1+a [both x1 and a are sent in parameter of intercection() ]
I am getting the arcs at desired position and even points on the arc at desired position but when I break the whole programme into opti(), intersection(), obj() as shown above I get intersection for only 3 points.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326690/image.png)
Writing the code as one without breaking it into function
XC= [54.0414 53.6743 53.2747 52.8450 52.3849 51.8917 ]
YC= [ 10.2174 11.9993 13.6638 15.2397 16.7520 18.2235 ]
Vs
Writing the code by breaking it into function
XC= [ NaN NaN 53.2747 52.8450 52.3849 51.8917 ]
YC= [ NaN NaN 13.6638 15.2397 16.7520 18.2235 ]
XC,YC= coordinates of intercepts on the quarter circle
Due to this Sum value is affected.
darova
2020년 7월 4일
If you make some changes to your code
add line inside intersection function to see normal lines you draw
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326777/image.png)
replace hold on command
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326780/image.png)
you will see that (xv,yv) vector are just too short
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326783/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326786/image.png)
Ron Herman
2020년 7월 4일
Thanks a ton your comments gave me insites .
Since vectors were short instead of multiplying 50 I increased the number.
xv = [0 dy]*150+x0;
yv = [0 -dx]*150+y0;
Ron Herman
2020년 11월 17일
편집: Ron Herman
2020년 11월 17일
I need the following output
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/416948/image.png)
I am unable to get the following output for some reason I an unable to draw horizontal line in all 3 points
(horizontal line from middle point is not getting constructed)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/416953/image.png)
For some reason index =50 or at ind=50 no line is being constructed. I donot the reason for this yet.
clc,clear
r = 10; % arc length
R = 55; % radius of a circle
aa = 60*pi/180; % arc angle
ap = 0*pi/180; % arc position angle
k=0;
%xa=60;
% a=10;
% b=50;
ind=[25 50 75];
t = linspace(0,pi);
[x,y] = pol2cart(t,R); % circle data
t1 = linspace(0,aa)-aa/2+ap;
[x1,y1] = pol2cart(t1,r); % arc data
xc=65;
yc=15;
%shifting the arc mid point to (65,15)
mp=length(x1)/2;
delx=xc-x1(mp);
dely=yc-y1(mp);
x1=x1+delx;
y1=y1+dely;
XC = ind*nan;
YC = ind*nan;
for i = 1:length(ind)
ix = ind(i);
x0 = x1(ix);
y0 = y1(ix);
% minor difference
dy = diff(y1(ix:ix+1));
dx = diff(x1(ix:ix+1));
% horizontal
xv = [dx -dx]*1500+x0;
yv = [0 0]*1500+y0;
%line(xv,yv)
% intersection point
[xc,yc] = polyxpoly(xv,yv,x,y);
if ~isempty(xc)
XC(i) = xc
YC(i) = yc
end
end
plot(x,y,x1,y1)
hold on
plot([XC;x1(ind)],[YC;y1(ind)],'.-r') % x1(ind)& y1(ind)are points on the curve
hold off % XC and YC are intersection points
axis equal
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)