% Function Name: speedReport % Inputs (2): - (double) A vector containing the co
ID: 3539694 • Letter: #
Question
% Function Name: speedReport
% Inputs (2): - (double) A vector containing the coefficients of a
% polynomial for a car's velocity at any time (in
% hours).
% - (double) A number representing time (in seconds).
% Outputs (4): - (double) The position of the car at the input time
% - (double) The velocity of the car at the input time
% - (double) The acceleration of the car at the input time.
% - (char) A sentence describing whether the car is slowing
% down, speeding up, neither slowing down or speeding
% up, or has stopped at that time.
%
% Function Description:
% Psychic mathematicians have successfully theorized a polynomial
% expression describing the velocity of someone's car at any time (in
% hours). Given a vector containing the coefficients of a polynomial that
% describes a car's velocity at any time (in hours) as well as a desired
% time (in seconds), your function "speedReport" must output the
% position, velocity, and acceleration of the car at that time and a
% string describing state of the car in regards of its change in speed.
% The fourth output should be determined by one of the following
% scenarios:
%
% 1) If the car stops at that time, the second output should be: 'The
% car has stopped.'
% 2) If the car is moving but neither speeding up nor slowing down,
% then the output should be: 'The car is neither speeding up nor
% slowing down.'
% 3) If the car is speeding up, the second output should be: 'The car
% is speeding up.'
% 4) If the car is slowing down, the second output should be: 'The
% car is slowing down.'
%
% The first part of solving this problem in your function is determining
% the vector of coefficients of the derivative of the velocity
% polynomial. You should also determine the vector of coefficients of the
% integral of the velocity polynomial; the constant that appears when
% taking the integral should be zero. In context of the problem, the
% acceleration of the car at any time (in hours) is the derivative of the
% velocity. The position of the car at any time (in hours) is the
% integral of the velocity.
%
% Notes:
% - You are *not* allowed to use the built-in functions polyder() or
% polyint().
% - The input time can be a negative number.
% - The position, velocity, and acceleration of the car at a desired time
% can be negative.
% - The velocity polynomial describes the car's velocity at any time in
% hours, but the input time is given in seconds. Thus, before
% determining the position, velocity, and acceleration output values,
% the input time should be converted into hours first.
%
% Hints:
% - You might find the polyval() function useful for this problem.
% - When an object is speeding up, both its velocity and acceleration at
% that time is of the same sign. In other words, its velocity and
% acceleration are either both positive or both negative.
% - When an object is slowing down, its velocity and acceleration at that
% time is of opposite signs.
% - When an object is neither speeding nor slowing down, its acceleration
% at that time is zero.
%
% Test Cases:
% vel_coef = [-2.5, 4.75, 14];
%
% [pos1, vel1, acc1, state1] = speedReport(vel_coef, 5000);
% pos1 => 21.7932
% vel1 => 15.7747
% acc1 => -2.1944
% state1 => 'The car is slowing down.'
%
% [pos2, vel2, acc2, state2] = speedReport(vel_coef, 0);
% pos2 => 0
% vel2 => 14
% acc2 => 4.7500
% state2 => 'The car is speeding up.'
%
% [pos3, vel3, acc3, state3] = speedReport(vel_coef, -5760);
% pos3 => -12.9067
% vel3 => 0
% acc3 => 12.7500
% state3 => 'The car has stopped.'
%
% [pos4, vel4, acc4, state4] = speedReport(vel_coef, 3420);
% pos4 => 14.7290
% vel4 => 16.2563
% acc4 => 0
% state4 => 'The car is neither speeding up nor slowing down.'
Explanation / Answer
function [pos, vel, acc, state]= speedReport(vcoff,t)
t= t/(60*60);
ord=length(vcoff);
acoff=[];
pcoff=[];
for k=1:ord
p=vcoff(k)/(ord-k+1);
pcoff=[pcoff p];
a=vcoff(k)*(ord-k);
if k~=ord
acoff=[acoff a];
end
end
pcoff=[pcoff 0];
pos=polyval(pcoff,t);
vel=polyval(vcoff,t);
acc=polyval(acoff,t);
if vel==0
state='The car has stopped.';
elseif acc==0
state='The car is neither speeding up nor slowing down.';
elseif acc>0
state= 'The car is speeding up.';
elseif acc<0
state='The car is slowing down.';
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.