Lab 4-4
Lab 4-4
The objectives of this lab are 1) to get familiar with file input/output, 2) to learn to create,
manipulate and use symbolic data in Matlab, and 3) to perform basic matrix manipulation in Matlab.
Practice each new command by completing the examples and exercise. Turn-in the answers for
all the exercise problems as your lab report. When answering the problems, indicate the
commands you entered and the output displayed. You may copy and paste the commands and
outputs to the word document. Similarly, plots can also be pasted to your word document.
Example
Create an M-file prompts the user to enter a value of x and then calculates the value of sin(x).
In the M-file “mySine.m” (or any filename), we have
x = input('Enter value of x: ');
display('The answer is: '); disp(sin(x));
In the command window, type
mySine
You will see in the command window:
Enter value of x: pi/3
The answer is:
0.8660
1.2 Output
The display (disp) function can be used to display the contents of a matrix without printing the
matrix name. It accepts a single array as input. Thus,
disp(x)
returns
12345
The display command can also be used to display a string (text enclosed in single quotation
marks). For example,
disp('The values in the x matrix are:');
returns
The values in the x matrix are:
When you enter a string as input into the disp function, you are really entering an array of
character information.
Exercise 1
The volume of a right circular cone is given by
V = πR2h/3
where R is radius of the base, and h is the height of the cone.
Write a script file vol.m that prompts the user to enter the R and h use input, and then
calculate and display the volume V use disp.
Exercise 2
The distance that a freely falling body has traveled (neglecting air friction) is given by
y = ½ g t2
Assume that g = 9.81 m/s2. Generate a table of time versus distance traveled for values
of time from 0 to 2.5 seconds, with a increment of 0.1 second.
Use fprintf to display the results as below. The numbers should be right-justified (red
numbers in the following example). Show all your commands and results.
At time 0.0 the distance is 0.000
At time 0.1 the distance is 0.049
…
…
At time 2.4 the distance is 28.253
At time 2.5 the distance is 30.656
2. Matrix manipulation
We have learned different kinds/ways of manipulating matrix, such as how to access matrix
elements, concatenate matrix, as well as sub-matrices, adding/removing row/column. For details
please refer to the lecture handout, then do the following exercises.
Exercise 3
Create the following matrices,
10 11 12 50
𝑎 = [13 14 15] 𝑏 = [20 21 22 23] 𝑐 = [51]
16 17 18 52
Do the following exercise. Show your commands together with the results in Matlab. No
points without Matlab commands.
(a) Create a matrix D by adding c to the left of a. You should get a 3x4 matrix.
(b) Add matrix b to the top of matrix D to create matrix E (4x4 matrix).
(c) Create a matrix F from the 1st and 2nd columns of matrix a.
(d) Create a matrix G by inserting c between the 2nd and 3rd column of a. You will get a 3x4
matrix.
(e) Create a column vector H with the first element equal to G(2,4), the second element equal to
F(3,2) and the third element equal to E(3,1).