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 Center および File Exchange2-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