In: Computer Science
Part A - For Loops
Charlie, an avid mountain biker, has purchased a new multisport watch that measures data such as horizontal position, time, heart rate and elevation. The associated software for the watch allows for users to design their own apps that record new measurements. Charlie is interested in measuring their elevation gain and total distance travelled.
1. Write a MATLAB function that recieves vectors for horizontal position and elevation and outputs a plot of elevation vs. horizontal position. (1 mark)
2. Write a function that receives the vectors for horizontal position and elevation and outputs total distance travelled. You can assume that each point has been recorded sufficiently close together that a linear approximation incurs insignificant error. Your solution must use a for loop and is not allowed to use the in-built sum or diff functions. Hint: Calculate the distance between each coordinate and sum these distances together.
3. Elevation gain is a term used to describe the total vertical distance climbed up during uphill sections (downhill elevation changes are ignored). Write a function that receives an elevation vector and calculates the total elevation gain for the trip. Your solution must use a for loop and is not allowed to use the in-built sum or diff functions.
function partAQ1(xPosition,Elevation)
plot(xPosition,Elevation)
%uncomment the below 3 lines to show title and labels
% xlabel('Horizontal Position')
% ylabel('Elevation')
% title('Elevation vs Horizontal Position')
%%%%%%%%%%%%%%%%%%%%%%%%%%
function Distance = partAQ2(xPosition,Elevation)
Distance = 0;
for i = 1:length(xPosition)-1
%if xPosition is x coordinates, and Elevation is
y coordinates
%distance between 2 points is given by
sqrt((x1-x2)^2 + (y1-y2)^2)
dist = sqrt((xPosition(i+1)-xPosition(i))^2 +
(Elevation(i+1)-Elevation(i))^2);
Distance = Distance + dist;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%
function ElevationGain = partAQ3(Elevation)
ElevationGain=0;
for i=1:length(Elevation)-1
if Elevation(i+1)>Elevation(i)%add only if
there's an increase
ElevationGain = ElevationGain +
(Elevation(i+1)-Elevation(i-1));
end
end