Problem
Write a Matlab program to convert from miles per hour(mph) to kilometers per hour(kph) if the user gives the input in mph and from kph to mph if the user gives input in kph. (Hint: Ask the user to enter the value first and then ask the user to select the units giving options.). The conversion factors are as follows:
Miles per hour(mph) to Kilometers per hour(kph) conversion |
Kilometers per hour(kph) to Miles per hour(mph) conversion |
Solution
This problem is similar to our previous post: Convert Temperatures Celsius Fahrenheit using Matlab. Hint has also been given in the question itself and I suggest you to directly see program section and for detailed explanation then see explanation section. Some of the concepts you need to know to solve this problem are as follows:
1) Request User input Matlab
2) fprintf function in Matlab
3) variables in Matlab
4) if elseif else statements in Matlab
5) Comments in Matlab
1) Request User input Matlab
2) fprintf function in Matlab
3) variables in Matlab
4) if elseif else statements in Matlab
5) Comments in Matlab
Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | %Ask the user to enter the value speed = input('Enter the value of speed: '); %Ask the user to select the units of speed fprintf('Select the units\n') fprintf('1) Kilometers Per Hour(KPH)\n') fprintf('2) Miles Per Hour(MPH)\n') units = input('Select your option and enter the number: '); if units == 1 mph = 1.60934*speed; fprintf('%.2f KPH = %.2f MPH\n',speed, mph) elseif units == 2 kph = 0.621371*speed; fprintf('%.2f MPH = %.2f KPH\n',speed, kph) else fprintf('You have entered a wrong input.\n') fprintf('Please try again\n') fprintf('-------RESTART -----------\n\n') mphkph; end |
Line 1: It is a Matlab comment
Line 2: We are asking the input of the user using the Matlab input function and storing it in a variable called as speed.
Line 4: We are commenting using Matlab Comments that we are are going to ask the user the units of speed
Line5-7: We are presenting the user with options using the fprintf statements
Line 8: We are asking the user to select the unit and enter the option which we are storing in units variable. And we are asking the user to give the input using the Input function.
Line 10: I have started the if statement to check whether the user has given the input as 1, if yes then we can understand that the user has chosen the input as KPH and thus the speed needs to be converted to MPH.
Line 11-12: This is under the if statement block. If the user has selected 1 then we will convert the speed from KMPH to MPH using the relation given in the question and we are printing the output to the user screen using the fprintf statement.
Line 13: Here we have used the elseif statement to check whether the user has given the input as 2. If yes then convert from MPH to KPH.
Line 14-15: This is an elseif block and it is similar to the if block where the data given input is being converted from MPH to KPH.
Line 16: Here we are using the else statement to check if the user has not entered any of the options(neither 1 nor 2). Then we(Matlab) will log a error to the user saying that they have entered a wrong input and ask them to try again. We will restart the script. We will use the name of the script to start the script again and the same continues until the user enters a correct input or the user is satisfied with the conversions.
Line 11-12: This is under the if statement block. If the user has selected 1 then we will convert the speed from KMPH to MPH using the relation given in the question and we are printing the output to the user screen using the fprintf statement.
Line 13: Here we have used the elseif statement to check whether the user has given the input as 2. If yes then convert from MPH to KPH.
Line 14-15: This is an elseif block and it is similar to the if block where the data given input is being converted from MPH to KPH.
Line 16: Here we are using the else statement to check if the user has not entered any of the options(neither 1 nor 2). Then we(Matlab) will log a error to the user saying that they have entered a wrong input and ask them to try again. We will restart the script. We will use the name of the script to start the script again and the same continues until the user enters a correct input or the user is satisfied with the conversions.
Input/Output
MPH to KPH Conversion and vice versa |
Try it Yourself
1) Write a program to convert from miles per hour to meters per hour and vice versa. You can get the conversion factor from here: Comment your program to help others
Miles per hour to meters per hour |
Meter per hour to Miles per hour |
Final Note
I have tried to explain every part in this post in such a way that it is easy to understand for everyone. If you have not understood anything or have a doubt then please do contact me or comment below. You can contact me from here: contact me
The above program was high lighted using hilite.me
I would like to cite a source from wikimedia who have a book for Matlab programming. You can get it from here: Matlab Programming- Wikibooks. You can read it if you want to learn Matlab from a free source.
Keywords: conversion, matlab, mph, kph, miles, kilometers, meters