0% found this document useful (0 votes)
5 views14 pages

Lab01_MATLAB Intro_ENGR2100U-1

Uploaded by

faiq00goraya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
5 views14 pages

Lab01_MATLAB Intro_ENGR2100U-1

Uploaded by

faiq00goraya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 14

ENGR2100U

Computational Engineering Applications

Lab 1 : Introduction to MATLAB

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

 Gain familiarity with MATLAB interface, workspace, command line.


 Use strings, vectors and matrices, and understand how to manipulate them.
 Use embedded Matlab functions.
 Learn how to 2D and 3D plot.

Components/Software

 MATLAB R2019b

Other Useful Information

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

2. Understanding the MATLAB environment:


Figure 1 MATLAB environment

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.

3. Basic embedded functions


MATLAB can be used to compute basic functions, such as sum, minus, multiplication,
division and power. Figure 2 show the example of these function in the Command
Window. The result of the mathematical expression can be seen beside the variable
name in the workspace, when the command is ended with ;

Figure 2 Using basic functions and understanding the workspace.

3.1. Basic commands

clc Clears command window.


clear Removes variables from memory
(workspace).

exist Checks for existence of file or variable.

global Declares variables to be global.

help Searches for a help topic.

lookfor Searches help entries for a keyword.

quit Stops MATLAB.

who Lists current variables.

whos Lists current variables (long display).

load Loads workspace variables from a file.

figure Opens a new figure window.

hold Freezes current plot

grid Displays gridlines.

plot Generates xy plot.

title Puts text at top of plot.

xlabel Adds text label to x-axis.

ylabel Adds text label to y-axis.

fscanf Read formatted data from a file

fprintf Performs formatted writes to screen or


file.

inv Computes inverse of a matrix.

det Computes determinant of an array.

size Computes array size.

max Returns largest element


rank Computes rank of a matrix.

min Returns smallest element.

syms x Define x as a symbol.

3.2. Sophisticated embedded 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 3 Embedded functions, imaginary number and help

4. Vector and matrix


The vector and matrix manipulation on the MATLAB are done in the following manner:
 Vectors:
o A = [1 3 4];
o B = [2 5 6];
 Matrix:
o D = [9; 2; 4];
o E = [1 2 ; 3 4];
So, as it can be seen the semi-column symbol is used only in matrices, where the row is
separated by the ; . Figure 4 shows the creation of vector and matrices, as well as one
example of using the function size() to find the number of rows and columns of desired
variables. As mentioned before, the matrix D has only one column and 3 rows and the
vector A has only one row and 3 columns.

Figure 4 Working with vector and matrices.

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.

Figure 5 Manipulation of vectors and matrices

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.

degtorad(angleInDegrees) Returns the radians value of the input.

radtodeg(angleInRadians) Returns the degrees value of the input.


Figure 6 shows the command line sequence of the degrees to radians conversion, plus
the sin, cos and tan calculations for each variable. Note the typo was done in the
command line and the error message as “Unrecognized function or variable tg”,
sometimes MATLAB suggest the correct form.

Figure 6 Figure 6 Conversion from degrees to radians and radians to degrees.

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.

Figure 7 Simple plot example


Figure 8 Sine wave plot.

Figure 9 sphere plot. https://stackoverflow.com/questions/38060732/matlab-sphere-plot-without-using-the-sphere-


function
6. Strings

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.

Figure 10 Strings creation and working with workspace.


Lab Section

Lab Tasks

1) Find the proper function to calculate the base 10 logarithm of 100. Use HELP function.

2) Find the proper function to calculate the natural logarithm of 100.

3) Define A= [ 31 24 ] and prove that A.A-1 = I.

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.

6) Define A= [ 21 −15 ] and find the following:


a) Find the dimension of A

b) Find the determinant of A

c) Verify if AA’ = A’A or not.

7) Plot y ( t ) =sin ⁡( 2 πft ) for f =1 kHz over a full period.

8) Plot z ( x , y )=√ x 2+ y 2 for x=[ −1 1 ] y= [−1 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.

10) Plot a circle centered at (1, 1) with radius of 2.

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

a) Find the location of 3.

b) Output the first column.


c) Output the second row.

d) Change the first row to [1 2 3].

e) Multiply first row by first column

f) Find the dimension of the multiplication (e) using the workspace.

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.

You might also like