ee 8
EENG 1910: Project I – Introduction to Electrical Engineering
Assignment-8
1. Create a MATLAB function that will take as inputs the radius (r) and height (h) of a cone and calculate its volume. The
formula to compute the volume of a cone is as follows.
𝑉 = 1
3 𝜋𝑟 2ℎ
Write a descriptive comment on the use of the function so that
the user by typing help nameofyourfunction has all the necessary
information to use your function.
In each of the following questions (2. & 3.), first, evaluate the
given MATLAB code for each of the test cases without using MATLAB.
Then, using MATLAB, check the answers you obtained first.
2. if n > 1 a. n = -25 m = ?
m = n+1 b. n = 2 m = ?
elseif n==0 c. n = 10 m = ?
m = n d. n = 0 m = ?
else
m = n-10
end
3. switch letter a. letter = ‘p’
case ‘x’ b. letter = ‘y’
disp(‘Hello’) c. letter = ‘q’
case{‘y’, ‘Y’} d. letter = ‘Q’
disp(‘Yes’) e. letter = ‘x’
case ‘Q’
disp(‘Quit’)
otherwise
disp(‘Error’)
end
Note: When you copy and paste codes into MATLAB, some characters
may not correctly translate. As a result, you may have to correct
them for the script to run properly.
4. A switch statement can often be used in place of a nested if- else or an if statement with many elseif clauses. Switch
statements are used when an expression is tested to see whether
it is equal to one of several possible values.
The script pickPizzaCase.m posted on Canvas is written using a
case statement. Analyze it and convert it to if-elseif-else
statement.
5. Write MATLAB script that will prompt the user for a temperature
in degrees Celsius, and then a letter ‘F’ for Fahrenheit or ‘K’
for Kelvin. The script should work weather a capital or small
case letter is entered. The script will then print the
corresponding temperature in the scale specified by the user.
For example, the outputs might look as follows.
>> conversioncode
Please enter a temperature value in Celsius: 45
Do you want K or F? k
The temperature in Kelvin is: 318.15.
Please enter a temperature value in Celsius: 29.3
Do you want K or F? f
The temperature in Fahrenheit is: 84.74.
>> conversioncode
Please enter a temperature value in Celsius: 78
Do you want K or F? K
The temperature in Kelvin is: 351.15.
>> conversioncode
Please enter a temperature value in Celsius: 74
Do you want K or F? P
Please enter F or K!
Note: Lesson-8 has a sample code that could simply be modified.
Also, conversioncode is simply the name that I have given to my
script. The conversions can be performed using the formulas below.
𝐾 = 𝐶 + 273.15
𝐹 = 9
5 𝐶 + 32
6. Use a for loop to sum the elements in the following vector: V=[10, 27, -66, 43, 72, 87, 56,98, 33, 23.5, 8.5, 77.7, 36,
24,11, 85, 55, 63, 4, 39, 100, -96, 4, 41, 189, -9,78.3, -83].
You will need to first determine the size (length) of the
vector. Check your answer using the MATLAB sum function.
7. Repeat the previous problem, this time using a while loop.
8. Create a function called “isInt” to check whether an input number is an integer. The function should display a sentence of
the form: “22.5 is not an integer!” or “99 is an integer!” if
the inputs were respectively 22.5 and 99.
9. In chemistry, the pH of an aqueous solution is a measure of its acidity. The pH scale ranges from 0 to 14, inclusive. A solution
with a pH of 7 is said to be neutral, a solution with a pH
greater than 7 is basic, and a solution with a pH less than 7
is acidic. Write a script that will prompt the user for the pH
of a solution, and will print whether it is neutral, basic, or
acidic. If the user enters an invalid pH, an error message
should be printed.