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.
clc clear V= 40; %m/s delta= 5; %s t= 1 ; x(t)= 0; y(t)= 1250; position= [x(t),y(t)]; if y(t) > 1500 h(t)=-15; else if y(t) < 1000 h(t)=15; else h(t)=10; end end x(t)= x(t)+ delta*V*sin(h(t)); y(t)= y(t)+ delta*V*cos(h(t)); t=t+1; if x(t)>= 1590 sprintf('the signal is too weak') end figure plot(x(t),y(t),'s') axis([0 2000 0 2000]) hold on xlabel(''); ylabel(''); title('Ship Path');The main problem is I don't know how to put a stop when x(t) is > than 1590. And also, nothing show up in my graph. I think I might have done some serious mistakes. Hope that you can guide me through these things. Thanks a lot.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.
clc clear V= 40; %m/s delta= 5; %s tMax = 20; t= 1 ; x(t)= 0; y(t)= 1250; while (t < tMax ) && (x(t) < 1590) position= [x(t),y(t)]; if y(t) > 1500 h =-15; else if y(t) < 1000 h =15; else h =10; end end x(t+1)= x(t)+ delta*V*sin(h); y(t+1)= y(t)+ delta*V*cos(h); t=t+1; if x(t)>= 1590 sprintf('the signal is too weak') end end figure plot(x,y) %axis([0 2000 0 2000]) hold on xlabel(''); ylabel(''); title('Ship Path');