Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function to convert the speed of a vessel in knots to speed in kmh, mph,

ID: 2072424 • Letter: W

Question

Write a function to convert the speed of a vessel in knots to speed in kmh, mph, or fps. Notes: Name your function knot Conversion(). Your function should accept two input arguments: a numerical variable Speed and a string variable Unit. The input variable Speed is to pass the speed of the vessel in knots and the input variable Unit is the unit in which the speed is being converted to. The input variable Unit can ONLY take one of these characters: 'k' or 'K' (for kilometers per hour, kmh), 'm' or 'M1 (for miles per hours, mph), and 'f' or 'F' (for feet per second, fps). The conversion rates between knots and other units are as follows: 1 knot = 1.852 kmh, 1 knot = 1.1508 mph, and 1 knot = 1.6878 fps. The output of your function is variable y. The output is just one numeric variable representing the speed of the vessel in the chosen unit. Your function should return the error message: 'Error', if the input Speed is negative or the input Unit is not one of the three acceptable conversion choices (i.e., 'k' or 'K', 'm' or 'M', or 'f' or 'F'). Solution function y = knot Conversion() % your code goes here

Explanation / Answer

Matlab Code:

function y = KnotConversion(Speed,Unit)
if Speed<0%checks for speed, if fails displays the error message and exits the function
disp('invalid speed');
return;
end
if Unit~='k' && Unit~='K' && Unit~='m' && Unit~='M' && Unit~='f' && Unit~='F'%checks for units, if fails displays the error message and exits the function
disp('invalid units');
return;
end
if Unit=='k' || Unit=='K'
y=1.852*Speed;
elseif Unit=='m' || Unit=='M'
y=1.508*Speed;
elseif Unit=='f' || Unit=='F'
y=1.852*Speed;
end
end

Command Window:

>> KnotConversion(42,'f');
>> ans

ans =

77.7840

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote