How to show every iteration step of lsqnonlin()?

21 vues (au cours des 30 derniers jours)
Nico Lange
Nico Lange le 3 Déc 2020
Commenté : Nico Lange le 4 Déc 2020
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 :)

Réponses (1)

Steven Lord
Steven Lord le 3 Déc 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 commentaires
Steven Lord
Steven Lord le 3 Déc 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 le 4 Déc 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').

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by