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;
R=inf; lambda=0;
else
initialguess = [1; 1; 1];
options=optimset('Display','none');
res = fsolve(@(vars)userfun(vars,xinput,yinput),initialguess,options);
x0=res(1);y0=res(2);R=res(3);
lambda=1/R;
end
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
Best Answer