Need help for discrete time motion in matlab
hello for all, I am trying to write some code by myself and I found out it is hard!! I am trying to create the motion using matlab and to control the motion within certain boundary. In my case, the starting point is (0,1250), and it should have some forward speed and heading angle for the point to move. After some time intervals, the point must have moving out of the boundary (y=1000 or y=1500), and in that case, I have to decide an angle to drag the point back to the boundaries specified (between y=1000 and y=1500). The simulation should stop when the point is moving beyond x=1590. I know there must be hundreds of errors that I may have done when writing the codes. I also well aware that my knowledge in matlab is terribly limited.Thus, I hereby asking help for those who can understand my situation and willing to help me, thanks a lot.....:)
The code that I have written is as below (please don't laugh if u think that I have written something ridiculous.....haha)
The code that I have written is as below (please don't laugh if u think that I have written something ridiculous.....haha)
clc clear xmin=[0; 0]; % Set edges of region want to search in xmax=[3000;3000]; c=3.0E8; % m/s light travel speed f=300E6; % MHz signal frequency GSM900MHz w=2*pi*f; lambda=2*pi*c/w; V=40; delta=5; figure(1) clf % Plot initial and final positions plot(0,1250,'s') axis([0 3000 0 3000]) hold on xlabel('x'); ylabel('y'); title('initial vehicle (square) position'); %starting point of ship x0=0; y0=1250; position=[x0,y0]; for t=1:1:100; %assume for 100 timesteps h(t)=10; x(t)=x0; x(t+1)= x(t)+ delta*V*sin(h(t)); y(t)=y0; y(t+1)= y(t)+ delta*V*cos(h(t)); %To move within the boundary of y=1500 and y=1000 if y(t) < y==1500; h(t)=10; elseif y(t) > y==1000; h(t)=10; else new = [x(t+1), y(t+1)]; old = [x(t), y(t)]; dest = [ (x(t+1)+delta*V), 1250]; a= new-old; b= dest-new; theta= sign(b(1)*a(2)-b(2)*a(1))*acos (dot(a,b)/(norm(a)*norm(b))); h(t)= theta; end %To halt the system when the point exceed x=1590 if x(t)> x==1590; sprintf('the signal is too weak') x(t+1)=x(t); y(t+1)=y(t); end end warning off
0
Comments
Why do you have warnings off? There are many bugs in your code that would be indicated by the warning messages, but you turned them off.
Remove that line from your script, type 'warning on' and re-run it. Fix all the warning that come up.
Addition: the goal or target can be set as x=1590, y=1250.
I've added a while loop that stops after tMax or if the x value gets out of the range. I've also updated the code so that the new calculated value of x is stored in the next time step. It is now doing what you think your code should be doing. However, I think your values for h are not necessarily correct.