Radius of Curvature Array

8 views (last 30 days)
Jide Williams
Jide Williams on 20 Apr 2019
Commented: Divya on 30 Jan 2023
The code generates radius of curvatures for three points(coordinates x1,y1 x2,y2 and x3,y3. However, i have a set of coordinates (77) in all, and I am trying to rewrite the code to loop through the coordinates and generate a radius of curvature for every three points within the set of 77, and store the result in another variable say R. I have not been able to do this and I need urgent help: See the radius of curvature code below that generates when radius of curvature for 3 points.
function lambda=mycurvature(xinput, yinput)
global xstar ystar;
xstar=xinput;ystar=yinput;
alpha=atan2(ystar(2)-ystar(1),xstar(2)-xstar(1))-atan2(ystar(3)-ystar(2),xstar(3)-xstar(2));
if abs(alpha)<0.002; %set the curvature to 0 if the points are aligned
R=inf; lambda=0;
%plot(xstar,ystar,'-r',xstar,ystar,'*b','linewidth',2,'markersize',10);
else
initialguess = [1; 1; 1]; % Make a starting guess at the solution
options=optimset('Display','none'); % Option to display output
res = fsolve(@userfun,initialguess,options); % Call optimizer
x0=res(1);y0=res(2);R=res(3); %x-coord of center; y-coord of center; radius of curvature
lambda=1/R; %curvature
%t=0:0.01:2*pi;xx=x0+R*cos(t);yy=y0+R*sin(t);
%plot(xx,yy,'-r',xstar,ystar,'*b','linewidth',2,'markersize',10);
end
%str=sprintf('radius=%6.3f, curvature=%6.3f',R,lambda);
%title(str, 'fontsize', 14);xlabel('x'),ylabel('y');
%axis equal;
function F = userfun(vars)
global xstar ystar;
F = [...
(xstar(1)-vars(1))^2+(ystar(1)-vars(2))^2-(vars(3))^2; ...
(xstar(2)-vars(1))^2+(ystar(2)-vars(2))^2-(vars(3))^2; ...
(xstar(3)-vars(1))^2+(ystar(3)-vars(2))^2-(vars(3))^2; ...
];
xinput are the array of x coordinates for the three points, yinput are the array of y coordinates for the 3 points.
What i am doing that is not working is
x = [array of my x-coordinates of my 77 points]; y =[array of my y-coordinates of my 77 points]
for i = 1:3:length(x)-1
xinput = x(i), x(i+1), x(i+2)
yinput = y(i), y(i+1), y(i+2)
and then removed the function at the first line of code for the radius of curvature code above. This did not work. What am I doing wrong? Please help.

Accepted Answer

darova
darova on 21 Apr 2019
Try not to use global variables if its possible
function [x0, y0, lambda] = mycurvature(xinput, yinput)
alpha=atan2(yinput(2)-yinput(1),xinput(2)-xinput(1))-atan2(yinput(3)-yinput(2),xinput(3)-xinput(2));
if abs(alpha)<0.002; %set the curvature to 0 if the points are aligned
R=inf; lambda=0;
%plot(xinput,yinput,'-r',xinput,yinput,'*b','linewidth',2,'markersize',10);
else
initialguess = [1; 1; 1]; % Make a starting guess at the solution
options=optimset('Display','none'); % Option to display output
res = fsolve(@(vars)userfun(vars,xinput,yinput),initialguess,options); % Call optimizer
x0=res(1);y0=res(2);R=res(3); %x-coord of center; y-coord of center; radius of curvature
lambda=1/R; %curvature
%t=0:0.01:2*pi;xx=x0+R*cos(t);yy=y0+R*sin(t);
%plot(xx,yy,'-r',xinput,yinput,'*b','linewidth',2,'markersize',10);
end
%str=sprintf('radius=%6.3f, curvature=%6.3f',R,lambda);
%title(str, 'fontsize', 14);xlabel('x'),ylabel('y');
%axis equal;
function F = userfun(vars,xinput,yinput)
F = [...
(xinput(1)-vars(1))^2+(yinput(1)-vars(2))^2-(vars(3))^2; ...
(xinput(2)-vars(1))^2+(yinput(2)-vars(2))^2-(vars(3))^2; ...
(xinput(3)-vars(1))^2+(yinput(3)-vars(2))^2-(vars(3))^2; ...
];
Main code:
clc , clear
a = 2;
b = 2;
t = linspace(0,pi/2,30);
x = a*cos(t);
y = b*sin(t);
plot(x,y,'.-r')
hold on
for i = 2:length(x)-1
xinput = [x(i-1) x(i) x(i+1)];
yinput = [y(i-1) y(i) y(i+1)];
[xc, yc, lam] = mycurvature(xinput,yinput);
plot([xc x(i)],[yc y(i)],'.-b')
end
hold off
grid on
  7 Comments
Divya
Divya on 29 Jan 2023
Y I am getting a errors
Divya
Divya on 30 Jan 2023
please reply

Sign in to comment.

More Answers (1)

Jide Williams
Jide Williams on 21 Apr 2019
the code isn't wrong xinput = x(i), x(i+1), x(i+2); and y(i), y(i+1), y(i+2); it just that its not looping all through the 77 points. It should loop through all the 77 points pick three points at a time and finding the Radius of curvature for three points at a time.

Categories

Find more on Graphics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!