matlab problem plotting in for loop-all values are converging to zero
Afficher commentaires plus anciens
Hello I am having trouble with plotting my function. Basically, I have a code that takes a starting point (a point on a circle) and a goal location (at the center of the circle) and with some formulas, a robot could find its way to the goal location. The code is all working great besides the plot... when I use the plot function in the four loop (commented out by three comments) it shows what is in Figure 1. I'm not sure why it's plotting those lines to the center or how to get rid of them. When I use the plot function outside of the function (commented out by 6 comments) I obtain Figure 2... which is what I want. The reason I need it plotted within the loop is because I want to have for h=1:10 eventually so I can show all 10 plots on one figure. If there is any advice for that also it'd be appreciated. The code was too large so I just posted my for loop, but also attached the downloadable .m file to make it easier. Thanks for the help in advance!

Figure 1

Figure 2
% if true
for h = 1:1
% xmatrix(1) = x(h);
% ymatrix(1) = y(h);
for i = 1:200
dt=0.05;
%calculating initial position
xmatrix(1) = x(h);
ymatrix(1) = y(h);
theta(i) = pi/4;
%finding initial row alpha beta
row(i)=sqrt((xmatrix(i)-goalx)^2+(ymatrix(i)-goaly)^2);
alpha(i)=-theta(i)+atan2(ymatrix(i)-goaly,xmatrix(i)-goalx);
beta(i)=-theta(i)-alpha(i);
b=[row(i);alpha(i);beta(i)];
c=A*b;
%derivative of row alpha beta
drow(i) = c(1,1);
dalpha(i) = c(2,1);
dbeta(i) = c(3,1);
%new row alpha beta
roww(i) = row(i)+drow(i)*dt;
alphaa(i) = alpha(i)-dalpha(i)*dt;
betaa(i) = beta(i)+dbeta(i)*dt;
%using newly obtained row alpha beta to get new points
xmatrix(i+1) = roww(i)*cos(betaa(i));
ymatrix(i+1) = -roww(i)*sin(betaa(i));
theta(i+1) = betaa(i)-alphaa(i);
%%%plot(xmatrix, ymatrix,'-gs');
hold on
if row(i)<=0.5
end
end
end
%%%%%%plot(xmatrix, ymatrix,'-gs');
%hold on
% end
1 commentaire
tyler taverne
le 9 Nov 2017
Réponses (1)
Torsten
le 9 Nov 2017
0 votes
Try "hold on" before the for-loop and use plot(xmatrix(i+1),ymatrix(i+1),'-gs') inside the loop.
Best wishes
Torsten.
2 commentaires
tyler taverne
le 9 Nov 2017
Torsten
le 9 Nov 2017
I think the most time-efficient way is to store your values for xmatrix and ymatrix in two-dimensional arrays (first dimension: i, second dimension: h) and plot the corresponding columns after the double loop.
Best wishes
Torsten.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!