Info

此问题已关闭。 请重新打开它进行编辑或回答。

Help with converting this code to use linspace command and form a vector

1 次查看(过去 30 天)
I just need help getting started with this question Here is the question: Use the linspace command to formulate a vector ShorexPt, with 11 possible x-entry points into the water. The starting point of the vector is LifeGuardx and the final point is Swimmerx. Also, set ShoreyPt = 0. We are only going to optimize the x-position of entry.
% lifeguard.m
clear; % clear any previously defined variables,
clc; % clear the screen in command window
close all ; % close any figures
figure; % get a blank graphics window
xmax = 5 ;
xmin = 0 ;
ymax = 0.5 ;
x=[xmin,xmax];
y=[0,0] ;
plot(x,y), hold on ;
xlabel('\itkm \color{blue}\itWater '); ylabel('\itkm '); ...
title(' \color[rgb]{.5 .5 .2}\it Land');
axis([xmin, xmax, -ymax, ymax]); % define plot limits
x = rand(2,1);
y = ymax * rand(2,1) ;
LifeGuardx = x(1) ;
LifeGuardy = y(1) ;
Swimmerx = xmax-x(2) ;
Swimmery = -y(2) ;
plot(LifeGuardx,LifeGuardy,'x',Swimmerx,Swimmery,'o'), hold on ;
fprintf('\nClick two points on horizontal line in figure.\n');
%Input Point 1
[ShorexPt1,ShoreyPt1,button] = ginput(1);
plot(ShorexPt1,ShoreyPt1,'ro')
%Calculating the distance from lifeguard to shore
DistkmShorePt1 = sqrt((ShorexPt1 - LifeGuardx)^2 + (ShoreyPt1 - LifeGuardy)^2), disp('km')
DistmilesShorePt1 = (DistkmShorePt1 * 0.6241), disp('mi');
%Calculating the distance from shore to swimmer
DistkmSwimmerPt1 = sqrt((ShorexPt1 - Swimmerx)^2 + (ShoreyPt1 - Swimmery)^2), disp('km');
DistmilesSwimmerPt1 = (DistkmSwimmerPt1 * 0.6241), disp('mi');
%Calculation time for the lifeguard to reach the swimmer
TimePt1 = (DistmilesShorePt1 * 7), disp('min')
TimePt2 = (DistmilesSwimmerPt1 * 60), disp('min')
TotalTime1 = TimePt1 + TimePt2
%Input Point 2
[ShorexPt2,ShoreyPt2,button] = ginput(1);
plot(ShorexPt2,ShoreyPt2,'go')
%Calculating the distance from lifeguard to shore
DistkmShorePt2 = sqrt((ShorexPt2 - LifeGuardx)^2 + (ShoreyPt2 - LifeGuardy)^2), disp('km')
DistmilesShorePt2 = (DistkmShorePt2 * 0.6241), disp('mi');
%Calculating the distance from shore to swimmer
DistkmSwimmerPt2 = sqrt((ShorexPt2 - Swimmerx)^2 + (ShoreyPt2 - Swimmery)^2), disp('km');
DistmilesSwimmerPt2 = (DistkmSwimmerPt2 * 0.6241), disp('mi');
%Calculation time for the lifeguard to reach the swimmer
TimePt3 = (DistmilesShorePt2 * 7), disp('min')
TimePt4 = (DistmilesSwimmerPt2 * 60), disp('min')
TotalTime2 = TimePt3 + TimePt4
%Input Point 3
[ShorexPt3,ShoreyPt3,button] = ginput(1);
plot(ShorexPt3,ShoreyPt3,'bo')
%Calculating the distance from lifeguard to shore
DistkmShorePt3 = sqrt((ShorexPt3 - LifeGuardx)^2 + (ShoreyPt3 - LifeGuardy)^2), disp('km')
DistmilesShorePt3 = (DistkmShorePt3 * 0.6241), disp('mi');
%Calculating the distance from shore to swimmer
DistkmSwimmerPt3 = sqrt((ShorexPt3 - Swimmerx)^2 + (ShoreyPt3 - Swimmery)^2), disp('km');
DistmilesSwimmerPt3 = (DistkmSwimmerPt2 * 0.6241), disp('mi');
%Calculation time for the lifeguard to reach the swimmer
TimePt5 = (DistmilesShorePt3 * 7), disp('min')
TimePt6 = (DistmilesSwimmerPt3 * 60), disp('min')
TotalTime3 = TimePt5 + TimePt6
if TimePt1 < TimePt2
plot([LifeGuardx, ShorexPt1], [LifeGuardy,ShoreyPt1]);
plot([Swimmerx, ShorexPt1], [Swimmery,ShoreyPt1]);
end
  3 个评论
Lucas Kimmel
Lucas Kimmel 2016-2-29
编辑:Lucas Kimmel 2016-2-29
@Stephen Cobeldick Thanks for the formatting tip. Any idea on help for this question?

回答(0 个)

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by