Lab Assignment 8
Lab Assignment 8
Aim:
Programming experiment based on if ... end" structure, relational and logical operators, ”for ...
end" loop, ”while ... end" loop.
Prerequisite:
MATLAB basics
Learning Outcomes:
Students will learn to create functions with if ... end" structure, relational and logical operators,
”for ... end" loop, ”while ... end" loop.
Theory:
If Statement
The if statement chooses whether another statement, or group of statements, is executed or
not. The general form of the if statement is:
if condition
action
end
If else statement
The if statement chooses whether or not an action is executed. Choosing between two actions,
or choosing from among several actions, is accomplished using if-else, nested if-else, and
switch statements. The if-else statement is used to choose between two statements, or sets of
statements.
The general form is:
if condition
actionl
else
action2
end
First, the condition is evaluated.
Tasks :
1. 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>= 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.
2. In aerodynamics, the Mach number is a critical quantity. It is defined as the ratio of
the speed of an object (e.g., an aircraft] to the speed of sound. If the Mach number is
less than 1, the flow is subsonic; if the Mach number is equal to 1, the flow is
transonic; and if the Mach number is greater than 1, the flow is supersonic. Write a
script that will prompt the user for the speed of an aircraft and the speed of sound at
the aircraft's current altitude and will print whether the condition is subsonic,
transonic, or supersonic.
3. 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 '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].
4. Write a function flipvec that will receive one input argument. If the input argument is
a row vector, the function will reverse the order and return a new row vector. If the
input argument is a column vector, the function will reverse the order and return a
new column vector. If the input argument is a matrix or a scalar, the function will
return the input argument unchanged.
5. Write a function eqfn that will calculate f(x) = x^2 + 1/x for all elements of x. Since x
division by 0 is not possible, if any element in x is zero, the function will instead
return a flag of -99.
PART B
(Students must submit the soft copy as per following segments within two hours of the
practical slot. The soft copy must be uploaded on the Blackboard or emailed to the
concerned lab in charge faculties at the end of the practical in case the there is no Black
board access available)
Date of Grading
if machNumber < 1
fprintf('The flow is subsonic.\n');
elseif machNumber == 1
fprintf('The flow is transonic.\n');
else
fprintf('The flow is supersonic.\n');
end
3 %% Question 3 %% Enter your answer (y/n):
y
letter = input('Enter your answer (y/n): ', 's'); OK, continuing
main file:
disp('Flipped Vector:');
disp(resultVec);
Main file
5
% Prompt the user to enter a vector of values Enter a vector of values for x:
x = input('Enter a vector of values for x: '); [1,2,3,4]
Input vector x:
% Call the eqfn function to calculate f(x)
1 2 3 4
result = eqfn(x);
% Display the original input and the result Resulting vector f(x):
disp('Input vector x:'); 2.0000 4.5000 9.3333 16.2500
disp(x);
% Check if the result indicates an error (-99) or a
valid output
if result == -99
disp('Error: Division by zero occurred in the
input vector.');
else
disp('Resulting vector f(x):');
disp(result);
end
function file:
The switch-case statement in MATLAB (and other programming languages) is used for
multi-way branching based on the value of a variable or expression. It allows you to choose
between multiple possible execution paths more efficiently and clearly than using multiple
if-elseif statements. The switch-case structure is useful when you need to compare a
variable to several discrete values and execute different code for each case. Key benefits
include:
Clarity: It makes the code more readable and easier to understand when there are
multiple conditions.
Efficiency: It may be more efficient than a long chain of if-elseif statements,
especially when there are many possible outcomes to check.
Organization: Keeps the code more organized by grouping cases under a single
switch statement.
if b > 0
else
end
else
end