I need some help debugging my Matlab code here is what I have got. This program
ID: 3681945 • Letter: I
Question
I need some help debugging my Matlab code here is what I have got. This program is supposed to accept user input for speed and time and returns the distance traveled. For some reason the final answer will not display and I cannot figure out why. Any help is much appreciated!
clc
format compact
clear
prompt= 'what is the speed?';
a= input(prompt);
prompttwo= 'what are the units associated with speed?';
b = menu(prompttwo, '1 for mph', '2 for kph','3 for m/s', '4 for ft/min');
promptthree= 'what is the time?';
c= input(promptthree);
promptfour= 'what are the units associated with time input';
d= menu(promptfour, '1 for seconds','2 for minutes', '3 for hours','4 for days');
promptfive= 'what are the the units in which to display the answer?';
e= menu(promptfive,'1 for miles','2 for kilometers', '3 for meters', '4 for feet');
if b==2;
speedbaseunit= a* 0.621371;
elseif b==3
speedbaseunit= a* 2.23694;
elseif b==4
speedbaseunit= a* 0.0113636;
if d==1
timebaseunit= c* 0.000277778;
elseif d==2
timebaseunit= c* 0.0166666667;
elseif d==4
timebaseunit= c*24
answerinmiles= speedbaseunit*timebaseunit
if e==2
answer= answerinmiles* 1.60934
display(answer)
elseif e==3
answer= answerinmiles* 1609.34
display(answer)
elseif e==4
answer= answerinmiles* 5280
display(answer)
end
end
end
Explanation / Answer
delta_t = linspace(0, 10, 50); %row vector! start_times = t; %the given values velocities = v; %the given values end_times = [ones(length(start_times),1), start_times(:)] * [delta_t; ones(1,length(delta_t))] %matrix multiply intended how_far_traveled = velocities(:) * delta_t; %matrix multiply intended