How to show every iteration step of lsqnonlin()?

조회 수: 12 (최근 30일)
Nico Lange
Nico Lange 2020년 12월 3일
댓글: Nico Lange 2020년 12월 4일
Hello again,
I am solving a nonlinear compensation problem on a cirlce. So first i am creating 10 points randomly.
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)
sz = 80;
scatter(xrand,yrand,sz,'ob','linewidth',1);
axis ([-1.5 1.5 -1.5 1.5]);
grid on;
hold on
After that, I am using lsqnonlin to solve the equation for a circle and plot the result with the 10 points.
f=@(x)((xrand-x(1)).^2+(yrand-x(2)).^2-x(3).^2)';
x0=[0,0,1];
KreisFit1 = lsqnonlin(f,x0);
figure (1)
phi = linspace(0,2*pi,100);
xFit1 = KreisFit1(3)*cos(phi) + KreisFit1(1);
yFit1 = KreisFit1(3)*sin(phi) + KreisFit1(2);
plot(xFit1,yFit1,'b-','linewidth',3)
hold off
I also want to show every step of the iteration done by lsqnonlin in the same figure.
How can i visualize each Step of the iteration?
Thanks for every help :)

답변 (1개)

Steven Lord
Steven Lord 2020년 12월 3일
You probably want to specify a PlotFcn as part of the options that get passed into lsqnonlin. See this documentation page for more information.
  댓글 수: 3
Steven Lord
Steven Lord 2020년 12월 3일
options = optimoptions(@lsqnonlin,'Display','iter-detailed','MaxfunEvals',35);
You did not specify that you wanted lsqnonlin to use a PlotFcn. Consider something along the lines of the attached.
Nico Lange
Nico Lange 2020년 12월 4일
Sorry, the example didn't really helped. But i understood what you mean with PlotFcn. I've tried it like this
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
But it tells me that there is an invalid data argument at plot(xrand, yrand,'ko').

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by