How to show every iteration step of lsqnonlin()?

21 views (last 30 days)
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 :)

Answers (1)

Steven Lord
Steven Lord on 3 Dec 2020
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 Comments
Steven Lord
Steven Lord on 3 Dec 2020
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 on 4 Dec 2020
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').

Sign in to comment.

Categories

Find more on Dates and Time 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!