matlab problem plotting in for loop-all values are converging to zero

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

Found the solution! The plot function with hold on has to be outside of the nested for loop and inside the other. The plot works perfect and allows me to plot all 10 different plots on one figure.

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

답변 (1개)

Torsten
Torsten 2017년 11월 9일

0 개 추천

Try "hold on" before the for-loop and use plot(xmatrix(i+1),ymatrix(i+1),'-gs') inside the loop.
Best wishes
Torsten.

댓글 수: 2

Thanks for the advice! That was helpful but when I do that it sadly gets rid of every line including the ones connecting each point. I tried finding a solution to this too but was unsuccessful. Any advice on that? Thanks again
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.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 11월 9일

댓글:

2017년 11월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by