Show every step of lsqnonlin won't work

조회 수: 2 (최근 30일)
Nico Lange
Nico Lange 2020년 12월 6일
답변: Stephan 2020년 12월 7일
Hello there,
I want so visualize every step of lsqnonlin() in an example of 10 random generated points for a circle in the same figure.
I start to generate 10 random points and show the curve fitting by lsqnonlin. Now, in the same figure, I want to visualize every iteration step as a circle.
This is my Code:
phi1=1:((2*pi/10.71)):2*pi;
r = -0.5 + (0.5 + 0.5) * rand(1,10);
xrand = sin(phi1) + 0.25 * r;
yrand = cos(phi1) + 0.25 * r;
figure (1)
scatter(xrand,yrand,80,'ob','linewidth',1);
axis ([-1.5 1.5 -1.5 1.5]);
grid on;
hold on
f=@(x)((xrand-x(1)).^2+(yrand-x(2)).^2-x(3).^2)';
x0=[0,0,1];
KreisFit = lsqnonlin(f,x0);
figure (1)
phi = linspace(0,2*pi,100);
xFit = KreisFit(3)*cos(phi) + KreisFit(1);
yFit = KreisFit(3)*sin(phi) + KreisFit(2);
plot(xFit,yFit,'b-','linewidth',3)
hold on
xlabel ('x-Achse');
ylabel ('y-Achse');
title ('Punktwolke für n=10');
legend ('n=10','Ausgleichskreis','Location','northeast');
options = optimoptions(@lsqnonlin,'Display','iter','OutputFcn',@outfun);
[x,resnorm,residual,exitflag,output] = lsqnonlin(f,x0,[],[],options);
function stop = outfun(xrand,yrand,state)
stop=false;
switch state
case 'iter'
figure (1);
hold on;
grid on;
plot(xrand,yrand,'ko')
plot(xrand,((xrand-x(1)).^2+(yrand-x(2)).^2-x(3).^2), 'r');
hold off
case 'interrupt'
case 'init'
case 'done'
otherwise
end
end
Can anyone show me how to do it right?
Thanks

답변 (1개)

Stephan
Stephan 2020년 12월 7일
I think you look for something like this:
phi1=1:((2*pi/10.71)):2*pi;
r = -0.5 + (0.5 + 0.5) * rand(1,10);
xrand = sin(phi1) + 0.25 * r;
yrand = cos(phi1) + 0.25 * r;
figure (1)
scatter(xrand,yrand,80,'ob','linewidth',1);
axis ([-1.5 1.5 -1.5 1.5]);
grid on;
hold on
f=@(x)((xrand-x(1)).^2+(yrand-x(2)).^2-x(3).^2)';
x0=[0,0,1];
KreisFit = lsqnonlin(f,x0);
figure (1)
phi = linspace(0,2*pi,100);
xFit = KreisFit(3)*cos(phi) + KreisFit(1);
yFit = KreisFit(3)*sin(phi) + KreisFit(2);
plot(xFit,yFit,'b-','linewidth',3)
hold on
xlabel ('x-Achse');
ylabel ('y-Achse');
title ('Punktwolke für n=10');
legend ('n=10','Ausgleichskreis','Location','northeast');
options = optimoptions(@lsqnonlin,'Display','iter','OutputFcn',@(x,optimValues,state)outfun(x,optimValues,state,xrand,yrand));
[x,resnorm,residual,exitflag,output] = lsqnonlin(f,x0,[],[],options);
function stop = outfun(x,~,state,xrand,yrand)
stop=false;
switch state
case 'iter'
figure (1);
hold on;
grid on;
plot(xrand,yrand,'ko')
scatter(xrand,((xrand-x(1)).^2+(yrand-x(2)).^2-x(3).^2), 'r');
hold off
case 'interrupt'
case 'init'
case 'done'
otherwise
end
end
However, the problem is that you have extra arguments that you have to give to the outputFcn. Im not sure if this result is exactly what you want, but im sure ith shows how to correct the error and see whats going on step by step

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by