0% found this document useful (0 votes)
2 views

Lab 4-4

The document outlines the objectives and exercises for a lab focused on file input/output, symbolic data manipulation, and basic matrix operations in Matlab. It includes detailed instructions on using input and display functions, formatted output with fprintf, and various matrix manipulation tasks. Students are required to complete exercises and submit their commands and outputs as part of their lab report.

Uploaded by

rmiguel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 4-4

The document outlines the objectives and exercises for a lab focused on file input/output, symbolic data manipulation, and basic matrix operations in Matlab. It includes detailed instructions on using input and display functions, formatted output with fprintf, and various matrix manipulation tasks. Students are required to complete exercises and submit their commands and outputs as part of their lab report.

Uploaded by

rmiguel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MEGN201

LAB 4 File I/O, Matrices

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.

1. File input/output (input, disp, fprintf functions)


1.1 Input
The Matlab uses input allowing the user to input values of a matrix from the keyboard while the
program is running. It displays a text string in the command window and then waits for the user
to provide the requested input. For example,
z = input('Enter a value')
displays
Enter a value
in the command window. If the user enters a value such as
5

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.

Formatted Output —The fprintf Function


Consider the following example:
number = 5;
fprintf('There are %f people in the pasture', number)
The string, which is the first argument inside the fprintf function, contains a placeholder (%)
where the value of the variable (in this case, cows) will be inserted. The placeholder also
contains formatting information.
Often when you use the fprintf function, your variable will be a matrix—for example,
x = 1:5;
MATLAB will repeat the string in the fprintf command until it uses all the values in the matrix.
Therefore,
fprintf(‘%8.2f \n',x);
returns
1.00
2.00
3.00
4.00
5.00
If the variable is a two-dimensional matrix, MATLA uses the values one column at a time, going
down the first column, then the second, and so on. For example:
feet = 1:3;
inches = feet.*12;
Combine these two matrices:
table = [feet; inches]
MATLAB then returns
table =
1 2 3
12 24 36
Now we can use the fprintf function to create a table that is easier to interpret. For instance,
fprintf(‘%4.0f %7.2f \n', table)
will get
1 12.00
2 24.00
3 36.00

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).

You might also like