In: Electrical Engineering
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.
The script pickPizzaCase.m posted on Canvas is written using a case statement. Analyze it and convert it to if-elseif-else statement.
>> 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 ? + 32
5
Question 1
function [V] = Volume_Cone(r,h)
% This function Volume_Cone(r,h) calculates the volume of a
cone
% 'h' is the height of the cone
% 'r' is the radius of the cone
% 'V is the volume of the cone
V = 3*pi*r*h;
end
Question 2
The given segment is
if n>1
m = n+1
elseif n == 0
m = n
else
m = n - 10
end
First Case : n = -25
Since n is not greater than 1 and n is not equal to 0, the command m = n - 10 will be executed and the value of m will be m = -25 - 10 = -35
Checking using MATLAB
clc;
n = -25;
if n>1
m = n+1
elseif n == 0
m = n
else
m = n - 10
end
After execution command window is
m =
-35
>>
Second Case : n = 2
Since n >1 , the command , m = n+1 will be executed and the value of m will be m = n+1 = 3
Checking using MATLAB
clc;
n = 2;
if n>1
m = n+1
elseif n == 0
m = n
else
m = n - 10
end
After execution command window is
m =
3
>>
Third Case : n = 10
Since n >1 , the command , m = n+1 will be executed and the value of m will be m = 10+1 = 11
Checking using MATLAB
clc;
n = 10;
if n>1
m = n+1
elseif n == 0
m = n
else
m = n - 10
end
After execution command window is
m =
1
>>
Fourth Case : n = 0
Since n is not greater than 1, the next else will be checked, ie if n == 0. Since n = 0, the command , m = n will be executed and the value of m will be m = n= 0
Checking using MATLAB
clc;
n = 0;
if n>1
m = n+1
elseif n == 0
m = n
else
m = n - 10
end
After execution command window is
m =
0
>>
Question 3
clc;
letter = 'p';
switch letter
case 'x'
disp('Hello');
case {'y','Y'}
disp('Yes');
case 'Q'
disp('Quit');
otherwise
disp('Error')
end
First : letter = 'p'
Since the letter is not in any of the cases of switch, the command window will have Error displayed
Checking using MATLAB
clc;
letter = 'p';
switch letter
case 'x'
disp('Hello');
case {'y','Y'}
disp('Yes');
case 'Q'
disp('Quit');
otherwise
disp('Error')
end
After execution command window is
Error
>>
Second : letter = 'y'
Since the letter is in one of the case statement, the command window will have Yes displayed
Checking using MATLAB
clc;
letter = 'y';
switch letter
case 'x'
disp('Hello');
case {'y','Y'}
disp('Yes');
case 'Q'
disp('Quit');
otherwise
disp('Error')
end
After execution command window is
Yes
>>
Third : letter = 'q'
Since the letter is not in any of the cases of switch, the command window will have Error displayed
Checking using MATLAB
clc;
letter = 'q';
switch letter
case 'x'
disp('Hello');
case {'y','Y'}
disp('Yes');
case 'Q'
disp('Quit');
otherwise
disp('Error')
end
After execution command window is
Error
>>
Fourth: letter = 'Q'
Since the letter is in one of the case statement, the command window will have Quit displayed
Checking using MATLAB
clc;
letter = 'Q';
switch letter
case 'x'
disp('Hello');
case {'y','Y'}
disp('Yes');
case 'Q'
disp('Quit');
otherwise
disp('Error')
end
After execution command window is
Quit
>>
Fifth: letter = 'x'
Since the letter is in one of the case statement, the command window will have Hello displayed
Checking using MATLAB
clc;
letter = 'x';
switch letter
case 'x'
disp('Hello');
case {'y','Y'}
disp('Yes');
case 'Q'
disp('Quit');
otherwise
disp('Error')
end
After execution command window is
Hello
>>
Question 4
function [Temp] = ConversionCode()
%This function converts the temperature in degree Celcius to
degree
%Farenheit of Kelvin according to the user's choice
C = input('Please enter a temperature value in Celsius:');
choice = input(' Do you want K or F?');
switch choice
case {'k','K'}
K = C + 273.15;
disp('The temperature in Kelvin is:');
disp(K);
case {'f','F'}
F = 9*C + 32;
disp('The temperature in Fahrenheit is:');
disp(F);
otherwise
disp('Please enter F or K!');
end
end
Question 5
clc;
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];
N = length(V);
S = 0;
for i=1:N
S = S + V(i);
end
disp('The Sum f the numbers
is:')
disp(S);
disp('The Sum of the numbers using
the function sum is:')
sum(V)
Question 6
clc;
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];
N = length(V);
S = 0;
i = 1
while i<= N
S = S + V(i);
i = i + 1;
end
disp('The Sum f the numbers is:')
disp(S);
disp('The Sum of the numbers using the function sum is:')
sum(V)
Question 7
function [] = isInt(n)
%This function checks whether the number is an integer or not
if floor(n)==n
fprintf('"%d is an integer"\n', n);
else
fprintf('"%d is not an integer"\n',n);
end
Question 08
clc
pH = input('Enter the pH of the solution');
if pH <7 & pH >=0
disp('The solution is acidic');
elseif pH==7
disp('The solution is neutral');
elseif pH>7 & pH<=14
disp('The solution is basic');
else
disp('Error!!!. pH will be between 0 and 14');
end