You are on page 1of 3

Lab 2: Vectors & Matrices

Simple Input/ Output


Selection Statments
Vectors and Matrices
1) Using the colon operator and also the linspace function, create the following row vectors:
a) -5 -4 -3 -2 -1
b) 5 7 9
c) 8 6 4
2) Using the colon operator and the transpose operator, create a column vector that has the values -1 to 1
in steps of 0.5.
3) Find an efficient way to generate the following matrix:
7 8 9 10
12 10 8 6
Then, give expressions that will, for the matrix above:
a) refer to the element in the first row, third column.
b) refer to the entire second row.
c) refer to the first two columns.
d) refer to the entire second row.
e) refer to the first two columns.
4) Randomly generate 3 x 6 matrices that satisfy the following
a) Integer entries, in the range (5, 20).
b) floating point entries in the range (25, 30).
5) Generate a matrix of random size consisting of random values and using functions: numel, length and
size to find the size of the generated matrix.
6) Generate a 5 x 6 matrix of random integers ranging from -10 to 10, then reshape this matrix to a 3 x
10 matrix. Then using linear indexing, change the value of the element at the 4th Row and 3rd
Column in the original matrix to whatever value you like.
Simple Input/output functions
7) Write an input statement that will prompt the user for the name of a chemical element as a string.
Then, find the length of the string.
8) Write an input statement that will prompt the user for a real number and store it in a variable. Then,
use the fprintf function to print the value of this variable using two decimal places.
9) In the metric system, fluid flow is measured in cubic meters per second (m3/s). A cubic foot per
second (ft3/s) is equivalent to 0.028 m3/s. Write a script titled flowrate that will prompt the user for
flow in cubic meters per second and will print the equivalent flow rate in cubic feet per second. Here
is an example of running the script. Your script must produce output in exactly the same format as
this:
>> flowrate
Enter the flow in m^3/s: 15.2

A flow rate of 15.200 meters per sec


is equivalent to 542.857 feet per sec
10) Write a script lumin that will calculate and print the luminosity L of a star in Watts. The luminosity L
is given by L = 42 b, where d is the distance from the sun in meters and b is the brightness in
Watts/meters2. Prompt the user for the inputs and print the output in an informative way.
selection statements
11. Determine and print whether or not a random number in the range from 0 to 1 is less than 0.5.
12. Consider implementing the following continuous function y = f(x) where:
1
<1
F(x) ={ 2
12
4
>2
Write a script that receives a value for x and displays y.
13. Write a Program that makes you choose from 1,2,3 ,4 ,5 then tell you what you choose using
Switch-case
14. Create a program that prompts the user to enter a scalar value for the outside air temperature. If
the temperature is equal to or above 80F, send a message to the command window telling the
user to wear shorts. If the temperature is between 60F and 80F send a message that it is a
beautiful day. If the temperature is equal to or below 60F, send a message telling the user to
wear a jacket.
15. At a local university, each engineering major requires a different number of credits for
graduation. For example, recently the requirements were as follows:
Civil Engineering
Chemical Engineering
Computer Engineering
Electrical Engineering
Mechanical Engineering

130
130
122
126.5
129

Select an engineering program from a menu. Use a switch/case structure to send the minimum
number of credits required for graduation back to the command window.
16. Write a Program asks the user for a type of pizza Cheese or Mushroom or Sausage and prints
which type to order using a switch
17. 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 quantities (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 assumed to be inches).
>> areaMenu
Menu
Cylinder
Circle
Rectangle

>> areaMenu
Menu
Cylinder
Circle
Rectangle

Please choose one: 2


Enter the radius: 4.1
The area is 52.81

Recommended problems: 1(a),2, 3, 6, 8, 9,12,15,17

Please choose one: 3


Enter the length: 4
Enter the width: 6
The area is 24.00

You might also like