How to plot x,y of a non-function?

14 views (last 30 days)
Dejan Cvijanovic
Dejan Cvijanovic on 6 Oct 2015
Edited: Chad Greene on 6 Oct 2015
Hello all,
Let me first lay out the problem formulation to avoid confusion:
"A submarine is moving at a speed of 10m/s heading West and 5m/s heading North starting from (5000m, 35000m). The motion of another submarine, which is tracking the first one, consists of two velocity legs starting from the origin: 1) in the Northwest direction for 450s and 2) in the Northeast direction for the next 450s with a constant speed of 7 . 1m / s. The second submarine measures the angle to the first one at regular intervals of 30s. The total observation period is 900s. The state of each submarine is represented by its position and velocity"
The code I have to implement that is the following:
tp = 900/30;
x1 =zeros(tp+1,tp+1);
x2 =zeros(tp+1,tp+1);
for t=1:30
x1_x(t) = 5000+5*(30*t);
x1_y(t) = 35000-10*(30*t);
if (30*t)<450
x2_x = -7.1*(30*t)/sqrt(2);
x2_y = 7.1*(30*t)/sqrt(2);
v2_x = -7.1/sqrt(2);
v2_y = 7.1/sqrt(2);
else
x2_x=7.1*(30*t)/sqrt(2);
x2_y=7.1*(30*t)/sqrt(2);
v2_x =7.1*(30*t)/sqrt(2);
v2_y =7.1*(30*t)/sqrt(2);
end
v1_x = -10;
v1_y = 5;
plot(x2_x(:),x2_y(:))
pause(0.01)
end
Now, I am not done the code, and there is still some stuff left to fill in, but I am having trouble plotting the x and y coordinates of each boat. That is, how can I set up an animation-like plot to show the trajectories over time? The path is not a function by virtue. I'm having a lot of trouble just trying to plot all the coordinates over time.That is, how do I plot the trajectories over all time? Do I need to save x and y coordinates for all time???? Please help! (Novice)

Answers (1)

Chad Greene
Chad Greene on 6 Oct 2015
Does this do what you want?
t = 1:30;
x = 50*cosd(4*t)+5*rand(size(t));
y = 40*sind(6*t)-10*cosd(5*t)+3*rand(size(t));
axis([min(x) max(x) min(y) max(y)])
hold on
for k = 1:length(t)
hgray = plot(x(1:k),y(1:k),':','color',[.5 .5 .5]);
hred = plot(x(k),y(k),'o','color','red');
pause(0.1)
if k<length(t)
delete([hgray hred])
end
end
  2 Comments
Dejan Cvijanovic
Dejan Cvijanovic on 6 Oct 2015
No, not quite. The ship moves Northwest and then Northeast. No randomness...
Chad Greene
Chad Greene on 6 Oct 2015
Edited: Chad Greene on 6 Oct 2015
You can define the ship tracks however you'd like. Here's a hot pursuit:
t = 1:60;
x1 = 50*cosd(4*t)+5*rand(size(t));
y1 = 40*sind(5*t)-10*cosd(5*t)+3*rand(size(t));
lag = 10;
x2 = circshift(x1',lag) + 2*rand(size(t))';
y2 = circshift(y1',lag) + 3*rand(size(t))';
x2(1:lag) = NaN;
y2(1:lag) = NaN;
axis([min(x1) max(x1) min(y1) max(y1)])
hold on
for k = 1:length(t)
hreddots = plot(x1(1:k),y1(1:k),'r:');
hredcircle = plot(x1(k),y1(k),'o','color','red');
hblackx = plot(x2(k),y2(k),'kx');
hblackdots = plot(x2(1:k),y2(1:k),'k:');
pause(0.1)
if k<length(t)
delete([hreddots hredcircle hblackx hblackdots])
end
end

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!