Anda di halaman 1dari 10

ASSIGNMENT 4

NOTE: Questions are in blue and answers are in black with the %statements in green.

2. (5 pts) Write a function nexthour that receives one integer argument, which is an hour
of the day, and returns the next hour. This assumes a 12-hour clock; so, for example, the
next hour after 12 would be 1. Here are two examples of calling the function.
>>fprintf('The next hour will be %d./n',nexthour(3))
>>fprintf('The next hour will be %d./n',nexthour(12))
The next hour will be 1.
%------------------------------------------------------------------%
function [ next_hour ] = nexthour( hour )
%This function takes an inputed hour and adds an hour to it.

%If statement that says any input between 0 and 12 will add an hour to the
%inputted hour
if (0<hour)&&(hour<12);
next_hour=(hour+1);
else
next_hour=(1);

end
%------------------------------------------------------------------%

4. (5 pts) The systolic and diastolic blood pressure readings are found when the heart is
pumping and the heart is at rest, respectively. A biomedical experiment is being
conducted on on participants whose blood pressure is optimal. This is defined as a
systolic blood pressure less than 120 and a diastolic blood pressure less than 80. Write a
script that will prompt for the systolic and diastolic blood pressures of a person, and will
print whether or not that person is a candidate for this experiment.
%------------------------------------------------------------------%
systolic = input('Enter the systolic blood pressure: ');
diastolic = input('Enter the diastolic blood pressure: ');

if systolic < 120 && diastolic < 80
disp('You are a candidate for this experiment.');
else
disp('You are not a candidate. Take your meds or hit the gym =D');
end
%------------------------------------------------------------------%
5. (5 pts) The Pythagorean theorm states that for a right triangle, the relationship between
the length of the hypotenuse c adn thengths of the other sides a and b is given by: c^2 =
a^2 +b^2. Write a script that will prompt the user for the lengths a and c, call a function
findb to calculate and return the length of b, and print the result. Note that any values of a
or c that are less than or equal to zero would not make sense, so the script should print an
error message if the user eneters any invalid value. Here is the function findb:
function b = findb(a,c)
% Calculates b from a and c
b = sqrt(c^2-a^2);
end
%------------------------------------------------------------------%
%This script will prompt the user for the lengths a and c, call
%function findb to calculate and return the length of b, and print
%the result.
%------------------------------------------------------------------%
%Clear all variables and command window
clear
clc
%------------------------------------------------------------------%
%Prompt user to input values for side a and hypotenuse c
a = input('Please input side a:');
c = input('Please input hypotenuse c:');
%------------------------------------------------------------------%
%Use an if statement to notify that a or c cannot be negative
if a<0 || c<0;
disp('Invalid - The length of a side cannot be negative.');
%If both values are positive, call function to calcuate side b from
%the given a and c values and print the length of all sides
else
b = findb(a,c);
fprintf('The length of side b with a side a value of %d and hypotenuse of %d is
%d.\n',a,c,b);
end
%------------------------------------------------------------------%

function b = findb(a,c)
% Calculates b from given a and c values
b = sqrt(c^2-a^2);

end
%------------------------------------------------------------------%
10. (10 pts) Write a function createvecMToN that will create and return a vector of
integers from m to n (where m is the first input argument and n is the second), regardless
of whether m is less than n or greater than n. If m is equal to n, the "vector" will just be a
1x1 or a scalar.
%------------------------------------------------------------------%
function [ vectorMtoN ] = creatvecMToN(m,n)
%This function will ask the user to input two values, m to n, and create a
%vector from those values, regardless of the size of m or n. If m=n, a
%scalar quantity will be outputted.
%----------------------------------------------------------------------%
clear
clc
%----------------------------------------------------------------------%
%User asked to input values for m and for n
m = input('Please input a value for m: ');
n = input('Please input a value for n: ');
%if statement to display a vector from m to n
if m>n;
%This will reverse the values of m and n and display a vector
temp=m;
m=n;
n=temp;
vectorMtoN = [m:n];
elseif m==n
%This will display a scalar values
fprintf('%d\n',m);
else
%This will display a vector m:n
vectorMtoN = [m:n];
end
end

11. (10 pts) The continuity equation in fluid dynamics for steady fluid flow through a
stream tube equates the product of the density, velocity, and area at two points that have
varying cross-sectional areas. For incompressible flow, the densities are constant so the
equation is A1V1=A2V2. If the areas and V1 are known, V2 can be found as A1/A2*V1.
Therefore, whether the velocity at the second point increases or decreases depends on the
areas at the two points. Write a script that will prompt the user for the two areas in square
feet, and will print whether the velocity at the second point will increase, decrease, or
remain the same as at the first point.
%----------------------------------------------------------------------%
%This script will prompt the user for 2 areas, and determine whethere the
%velocity at the second point will icrease or decrease.
%----------------------------------------------------------------------%
%Clear command window and all stored variables
clear
clc
%----------------------------------------------------------------------%
%Prompt the user for area 1 and area 2.
Area1=input('Please input the area at point 1: ');
Area2=input('Please input the area at point 2: ');

%----------------------------------------------------------------------%
%Utilize an if statement to determine whether or not the Volume is
%increasing or decreasing
if Area1<0 | Area2<0;
disp('INVALID entry.');
elseif Area1<Area2;
disp('The volume has increased.');
elseif Area1==Area2;
disp('The volumes are equal.');
elseif Area1>Area2;
disp('The volume has decreased.');
end
%----------------------------------------------------------------------%

14. (10 pts) In a script, the user is supposed to enter either a 'y' or 'n' in response to a
prompt. The user's input is read into a character variable called "letter'. The script will
print "OK, continuing" if the user enters either a 'y' or 'Y', or it will print, "OK, halting" if
the user enters a 'n' or an 'N' or "Error" if the user enters anything else. Put this statement
in the script first:
letter = input('Enter your answer: ', 's');
Write the script using a single nested if-else statement (elseif clause is permitted).
%----------------------------------------------------------------------%
%This script will print a response based on whether y, n, or any other entry
%was inputted.
%----------------------------------------------------------------------%
%Clear the command window and any assigned variables
clear
clc
%----------------------------------------------------------------------%
%Prompt user for an entry
letter = input('Enter your answer: ', 's');
%----------------------------------------------------------------------%
%Use an if statement to display response depending on entry
if letter=='y'||letter=='Y';
%If y or Y is entered, the displayed text will be ok, continuing
disp('OK, continuing');
else
%use nested if statement to display response if n or N or something
%other than that is displayed
if letter=='n'||letter=='N';
disp('OK, halting');
else
disp('Error');
%end the nested if
end
%end the initial if statement
end
%----------------------------------------------------------------------%

21. (10 pts) Whether a storm is a tropical depression, tropical storm, or hurricane is
determined by the average sustained wind speed. In miles per hour, a storm is a tropical
depression if the winds are less than 38 mph. It is a tropical storm if the winds are
between 39 and 73 mph, and it is a hurricane if the wind speeds are greater than or equal
to 74 mph. Write a script that will prompt the user for the wind speed of the storm and
will print which type of storm it is.
%----------------------------------------------------------------------%
%This script will determine the type of storm based off of the wind
%speed entered by the user.
%----------------------------------------------------------------------%
%Clear the command window and any assigned variables
clear
clc
%----------------------------------------------------------------------%
%Prompt the user to input the speed
windspeed=input('Please enter the wind speed in mph:');
%----------------------------------------------------------------------%
%Use switch statement to determine from the inputted wind speed what type of
%storm it is.
%----------------------------------------------------------------------%
%Error check
if windspeed < 0;
disp('Ivalid windspeed.');
%----------------------------------------------------------------------%
%Tropical depression if 0 to 38 mph
elseif windspeed<=38;
disp('Tropical Depression');
%----------------------------------------------------------------------%
%Tropical Storm if 39 to 73 mph
elseif windspeed>=39 && windspeed<=73;
disp('Tropical Storm');
%----------------------------------------------------------------------%
%Tropical Hurricane if more than 73mph
else
disp('Tropical Hurricane');
end
%----------------------------------------------------------------------%

27. Write a script areaMenu that will print a list consisting of "cylinder", "circle", and
"rectangle". It prompts the user to choose one, and then prompts the user for the
appropriate quantitites (e.g., the radius of the circle) and then prints its area. If the user
enters an invalid choice, the script simply prints an error message. The script should use a
nested if-else statement to accomplish this. Here are two examples of running it (units are
asumed to be inches)
>> areaMenu
Menu
1. Cylinder
2. Circle
3. Rectangle
Please choose one: 2
Enter the radius of the circle: 4.1
The area is 52.81
>> areaMenu
Menu
1. Cylinder
2. Circle
3. Rectangle
Please choose one: 3
Enter the length: 4
Enter the width: 6
The area is 24.00
%----------------------------------------------------------------------%
%This script will determine the area of the chosen shape.
%----------------------------------------------------------------------%
%Clear the command window and any assigned variables
clear
clc
%----------------------------------------------------------------------%
%Prompt the user to choose cylinder, circle, or rectangle
disp('Menu')
disp(' ')
disp('1. Cylinder')
disp(' ')
disp('2. Circle')
disp(' ')
disp('3. Rectangle')
disp(' ')
shape=input('Please choose a shape(1,2, or 3): ');
%----------------------------------------------------------------------%
%Use switch statement for different calculations depending on user input
if shape
%Input and calculation for cylinder
elseif 1
r=input('Enter the radius of the cylinder: ');
h=input('Enter the height of the cylinder: ');
Area=(2*pi*r*h)+ (2*pi*r);
fprintf('The area of your cylinder is %.2f.\n',Area)
%Input and calculation for circle
elseif 2
r=input('Enter the radius of the circle: ');
Area=pi*(r^2);
fprintf('The area of your circle is %.2f.\n',Area)
%Input and calculation for rectange
elseif 3
l=input('Enter the length of the rectangle: ');
w=input('Enter the width of the rectangle: ');
Area=l*w;
fprintf('The area of your rectangle is %.2f.\n',Area)
%Output if wrong entry was made
else
disp('INVALID ENTRY');
end
%----------------------------------------------------------------------%


28. (10 pts) Modify the areaMenu script to use a switch statement to decide which area to
calculate.
%----------------------------------------------------------------------%
%This script will determine the area of the chosen shape.
%----------------------------------------------------------------------%
%Clear the command window and any assigned variables
clear
clc
%----------------------------------------------------------------------%
%Prompt the user to choose cylinder, circle, or rectangle
disp('Menu')
disp(' ')
disp('1. Cylinder')
disp(' ')
disp('2. Circle')
disp(' ')
disp('3. Rectangle')
disp(' ')
shape=input('Please choose a shape(1,2, or 3): ');
%----------------------------------------------------------------------%
%Use switch statement for different calculations depending on user input
switch shape
%Input and calculation for cylinder
case 1
r=input('Enter the radius of the cylinder: ');
h=input('Enter the height of the cylinder: ');
Area=(2*pi*r*h)+ (2*pi*r);
fprintf('The area of your cylinder is %.2f.\n',Area)
%Input and calculation for circle
case 2
r=input('Enter the radius of the circle: ');
Area=pi*(r^2);
fprintf('The area of your circle is %.2f.\n',Area)
%Input and calculation for rectange
case 3
l=input('Enter the length of the rectangle: ');
w=input('Enter the width of the rectangle: ');
Area=l*w;
fprintf('The area of your rectangle is %.2f.\n',Area)
%Output if wrong entry was made
otherwise
disp('INVALID ENTRY');
end
%----------------------------------------------------------------------%

29. (10 pts) Modify the areaMenu script (either version) to use the built-in menu function
instead of printing the menu choices.
%----------------------------------------------------------------------%
%This script will determine the area of the chosen shape.
%----------------------------------------------------------------------%
%Clear the command window and any assigned variables
clear
clc
%----------------------------------------------------------------------%
%Prompt the user using the menu function to choose cylinder, circle, or
%rectangle

shape=menu('Please choose a shape','cylinder','circle', 'rectangle');
%----------------------------------------------------------------------%
%Use switch statement for different calculations depending on user input
switch shape
%Input and calculation for cylinder
case 1
r=input('Enter the radius of the cylinder: ');
h=input('Enter the height of the cylinder: ');
Area=(2*pi*r*h)+ (2*pi*r);
fprintf('The area of your cylinder is %.2f.\n',Area)
%Input and calculation for circle
case 2
r=input('Enter the radius of the circle: ');
Area=pi*(r^2);
fprintf('The area of your circle is %.2f.\n',Area)
%Input and calculation for rectange
case 3
l=input('Enter the length of the rectangle: ');
w=input('Enter the width of the rectangle: ');
Area=l*w;
fprintf('The area of your rectangle is %.2f.\n',Area)
%Output if wrong entry was made
otherwise
disp('INVALID ENTRY');
end
%----------------------------------------------------------------------%

30. (15 pts) Write a script that prompts the user for a value of a variable x. Then, it uses
the menu function to present choices between 'sin(x)', 'cos(x)', adn 'tan(x)'. The script will
print whichever function of x the user chooses. Use an if-else statement to accomplish
this.
%----------------------------------------------------------------------%
%This script will determine the area of the chosen shape.
%----------------------------------------------------------------------%
%Clear the command window and any assigned variables
clear
clc
%----------------------------------------------------------------------%
%Prompt user to input variable x
x=input('Please input a value for x: ');
%----------------------------------------------------------------------%
%Use menu to allow user to select a function of x
choice = menu('Select a function','sin(x)', 'cos(x)', 'tan(x)');
%----------------------------------------------------------------------%
%Use if statements to print function and result
if choice == 1;
fprintf('You have chosen the sin function. Sin(%d) = %.2f\n',x,sin(x));
elseif choice == 2;
fprintf('You have chosen the cos function. Cos(%d) = %.2f\n',x,cos(x));
elseif choice == 3;
fprintf('You have chosen the tan function. Tan(%d) = %.2f\n',x,tan(x));
else
display('INVALID ENTRY')
end
%----------------------------------------------------------------------%

Anda mungkin juga menyukai