Problem 2-Pick one of the two problems below. Option 1 -name your code problem2_
ID: 3198854 • Letter: P
Question
Problem 2-Pick one of the two problems below. Option 1 -name your code problem2_option1.m Four cats start at the four corners of a 10m x 10m room. The (xy) starting positions are given below (and a plot showing these positions is shown at the bottom of the page) Fluffy starts at (0, 10) Schrodinger starts at (10, 10) Viserion starts at (10, 0) Garfield starts at (0, 0) - Write a script that will model the following for 15 time steps For each time step (from t 1 to t-15) in the order given: -Fluffy runs 1 meter in the direction of Garfield Schrodinger runs 1 meter in the direction of Fluffy . Viserion runs 1 meter in the direction of Schrodinger - Garfield does not move (he remains in corner (0, 0) eating his lasagna) Hint: determine the x-distance (dx) and the y-distance (dy) between Fluffy and Garfield, and the total distance h- Vdx2 + dy?, Fluffy will move dx/h in the x-direction and dy/h in the y-direction (follow this method for the other cats) Note: 0/0 is NaN in MATLAB-so you may need an if statement to prevent your cats from having locations involving NaN if they catch up to each other Your code should plot the location of each cat at each time step (note you can use the pause) function inside your for loop to do this similar to what you have done for lab #12) Plot Fluffy as a green square 'sg - Plot Viserion as a red circle 'or Plot Schrodinger as a blue triangle b> Plot Garfield as a black star k Your plot should include a legend, title, and axes labels Here is a figure showing the initial position of the cats initial position of cats 10 r 6 x-location (m)Explanation / Answer
%fluffy
f=[0,10];
%Schrodinger
s=[10,10];
%Viserion
v=[10,0];
%Garfield
g=[0,0];
plot(f(1),f(2),'sg',s(1),s(2),'b>',v(1),v(2),'or',g(1),g(2),'*k')
title('Initial position of cats');
xlabel('x-location(m)')
ylabel('y-location(m)')
xlim([0 10])
ylim([0 10])
pause(1)
for i=1:15
%fluffy moves in direction of garfield
h=pdist([f;g],'euclidean');
if h~=0
dx=g(1)-f(1);
dy=g(2)-f(2);
f(1)=f(1)+(dx/h);
f(2)=f(2)+(dy/h);
end
cla
plot(f(1),f(2),'sg',s(1),s(2),'b>',v(1),v(2),'or',g(1),g(2),'*k')
title('Initial position of cats');
xlabel('x-location(m)')
ylabel('y-location(m)')
xlim([0 10])
ylim([0 10])
pause(1)
%scrhodinger in the direction of fluffy
h=pdist([s;f],'euclidean');
if h~=0
dx=f(1)-s(1);
dy=f(2)-s(2);
s(1)=s(1)+(dx/h);
s(2)=s(2)+(dy/h);
end
cla
plot(f(1),f(2),'sg',s(1),s(2),'b>',v(1),v(2),'or',g(1),g(2),'*k')
title('Initial position of cats');
xlabel('x-location(m)')
ylabel('y-location(m)')
xlim([0 10])
ylim([0 10])
pause(1)
%Viserion in the direction of Schrodinger
h=pdist([v;s],'euclidean');
if h~=0
dx=s(1)-v(1);
dy=s(2)-v(2);
v(1)=v(1)+(dx/h);
v(2)=v(2)+(dy/h);
end
cla
plot(f(1),f(2),'sg',s(1),s(2),'b>',v(1),v(2),'or',g(1),g(2),'*k')
title('Initial position of cats');
xlabel('x-location(m)')
ylabel('y-location(m)')
xlim([0 10])
ylim([0 10])
pause(1)
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.