Lab01_MATLAB Intro_ENGR2100U-1
Lab01_MATLAB Intro_ENGR2100U-1
January 2020
Laboratory
Introduction to MATLAB
1
This experiment will introduce you to the MATLAB basic functions, using the command line you
will be able to understand how MATLAB works with variables, basic functions and workspace.
PLEASE, SOLVE THIS LAB ONLY DURING THE LABORATORY TIME, AT HOME
YOU SHOULD ONLY DO THE PRELAB TASKS.
Objectives
Components/Software
MATLAB R2019b
MATLAB Documentation
MATLAB Examples
MATLAB Question/Answer
Prelab Section
1. Introduction:
Ensure you have installed MATLAB R2019b on your computer.
Get familiar with the Mexican Hat function.
Current Folder: where you project is located, all folders and files used by it should be
in the project folder. Usually one folder for each project is created and used, if working
in another project, create a new folder and navigate through these folders.
Workspace: where all variables are created and/or imported from files. Also, where all
the variables are stored in the project. If you navigate to another project (change current
folder) the workspace will also change accordingly. You can define multiple parameters
to be viewed in the workspace. You need to right-click on the Name and define. For
example, you can define Size and then look for the matrices or vectors sizes
dynamically, as shown in Figure 10.
Command Window: user interface command line. It is possible to type your commands
here and create the variables in the workspace. Also, it is possible to access the
variables from the workspace and manipulate them.
Command line: the variables can be created using command line. For example, if you
type A = 2, the variable A will be created in the workspace with the value of 2.
You can use ; in the end of the command in order for it not be repeated in the command
window, making it clear and better looking. Figure 2 shows some examples of basic
functions.
Besides the basic mathematical function, MATLAB can also provide sophisticated
functions like exponential, trigonometry, logarithm. Also, it is possible to operate with
imaginary numbers. Figure 3 shows the examples on how to input these functions in the
command line.
Figure 5 show the command line example of vector and matrices manipulation and
operations.
The sum of the elements of vector A is also calculated and can be seen as 8.
The multiplication of vector and matrices are given element by element.
o Using .* or ./ you multiply or divide vectors.
o Using * or / you multiply or divide matrices.
For more information, please read: Array-matrix operations.
The determinant of the matrices can be found using the function det().
The inverse of the matrices can be found using the function inv().
In order to access specific location in the vector or matrix, it is possible to:
o A(1,2) : element in the first row and second column.
o A(r, c) : element in the row r and in the column c.
o H(:, 2): all elements on the second column.
o H(1, :) : all the elements of first row.
4.1. Trigonometry
In order to work with trigonometry, it is evident the need of transformation from degrees
to radians and radians to degrees.
Most of the sin, cos, tg functions used in MATLAB are in radians, so it should be taken
care of using the following functions.
5. Plotting
In order to plot a graph, it is needed the values of x and the values of y(x).
The values of x can be defined as x = [-1, 0, 1], which vector has 3 elements.
However, when it is the case of having x with multiple values with equal step, it is
easier to rewrite as x = [-1:1:1] ([min, step, max]). For example, x = [-1:0.1:1] will
result in a vector of 21 elements from -1 to 1 with equal steps of 0.1.
In case there is a 3D plot, the values of 2 variables will be needed, thus the same
method can be used.
An important function of MATLAB is working with strings, for example to create file
names in a sequence or to create multiple variable names when needed.
By creating the word Hello, and the word World, you can join them with space or without
space, if you need space between the words, a space character needs to be inserted,
as show in Figure 10, otherwise, it will put together the two words without space.
Lab Tasks
1) Find the proper function to calculate the base 10 logarithm of 100. Use HELP function.
4) Define a sinusoidal signal with maximum amplitude of 5V and frequency of 1kHz. Find the
amplitude at t = 2 seconds and 5 seconds.
5) Define a time vector from [-5 5] with interval of 1 and prove that sin2 ( t )+ cos2 ( t ) =1.
9) Find the formula for Mexican Hat function and plot the function. Use the formula directly and
not by using the MATLAB inbuilt function for Mexican Hat.
11) Find the minimum of z ( x , y )=x 2−4 x+ 4 by plotting the function. Use the grid.
[ ]
9 2 1
12) Define A= 3 5 0 and find the following:
7 8 4
13) What is the function linespace? How it can be useful in these exercises?
Lab Report
1. Similar to the lab manual, you should present the print screen of the entire MATLAB
window, showing the workspace, plots, command window and current folder for each
and every question asked.
2. Make sure to make the plot legible, use help plot to have ideas on how to increase title
and legend font sizes.