Docsity

MATLAB Tutorial - Lecture Notes | ECE 517, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
AMATH 352 Lecture 3
MATLAB Tutorial
MATLAB (short for MATrix LABoratory) is a very useful piece of software for numerical
analysis. It provides an environment for computation and the visualization. Learning MATLAB is
not the goal of this course, but a working knowledge of MATLAB will allow you to implement and
test the algorithms that form the heart of this course. Seeing these algorithms at work will hopefully
enable a deeper understanding of the mechanics, strengths and pitfalls of each algorithm.
Starting MATLAB
To start MATLAB, click on the MATLAB icon or type
matlab
at the command line prompt.
To exit, type
quit
.
Entering Variables
There are three types of variables that you can use in matlab: scalars, vectors and matrices. To
assign a value to a scalar variable, type
>> x = 0.2
After hitting return, MATLAB will echo the value of the variable back to you:
x =
0.2000
If you don’t want to see the value of the variable, add a semicolon at the end of the line:
>> x = 0.2;
To enter a vector or matrix, use square brackets to indicate the start and end of the vector/matrix.
For example a row vector may be entered:
>>y=[01234]
y =
01234
To enter a variable with more than one row, the rows of the vector or matrix are separated by
semicolons or by carriage returns. For example a column vector may be entered:
>> z = [ 0; 1; 2; 3; 4]
z =
0
1
2
3
4
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download MATLAB Tutorial - Lecture Notes | ECE 517 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

AMATH 352 Lecture 3 MATLAB Tutorial MATLAB (short for MATrix LABoratory) is a very useful piece of software for numerical analysis. It provides an environment for computation and the visualization. Learning MATLAB isnot the goal of this course, but a working knowledge of MATLAB will allow you to implement and test the algorithms that form the heart of this course. Seeing these algorithms at work will hopefullyenable a deeper understanding of the mechanics, strengths and pitfalls of each algorithm.

Starting MATLAB To exit, type^ To start MATLAB, click on the MATLAB icon or type quit.^ matlab^ at the command line prompt. Entering Variables There are three types of variables that you can use in matlab: scalars, vectors and matrices. To assign a value to a scalar variable, type

x = 0. After hitting return, MATLAB will echo the value of the variable back to you: x =

If you don’t want to see the value of the variable, add a semicolon at the end of the line:

x = 0.2; To enter a vector or matrix, use square brackets to indicate the start and end of the vector/matrix. For example a row vector may be entered: y = [ 0 1 2 3 4] y = 0 1 2 3 4

To enter a variable with more than one row, the rows of the vector or matrix are separated by semicolons or by carriage returns. For example a column vector may be entered:

z = [ 0; 1; 2; 3; 4] z = 0 (^12) (^34)

A matrix may be entered similarly, with the rows of the matrix separated in this case by carriage returns and the whole expression enclosed in square brackets:

A = [ (^0) 3 7 2 12 8] A = 0 2 123 78 The (complex conjugate) transpose of a vector or matrix may be obtained by placing an apostropheafter the expression:

w = z’ w = 0 1 2 3 4 B = A’ B = (^02 37 )

To give a variable a set of evenly-spaced values, use the colon operator:

t = 0: t = 0 1 2 3 4 5 6 u = 0:0.3:1. u = 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.

The first and last numbers are the starting and ending points for the series. The middle number is the spacing between the members of the series. If no spacing is given, MATLAB assumes aspacing of 1.

Getting information about variables see all of the variables that are currently in the MATLAB workspace, type^ To see the size of a variable or its length (number of rows), type^ size(A) whos^ or:^ length(A). To

whosName Size Bytes Class

AB 3x22x3 4848 doubledouble arrayarray anst 1x31x7 2456 doubledouble arrayarray uw 1x71x5 5640 doubledouble arrayarray xy 1x11x5 408 doubledouble arrayarray z 5x1 40 double array Grand total is 45 elements using 360 bytes

Elementary computations Variables may be added, subtracted, multiplied and divided as long as the rules of arithmetic and linear algebra are obeyed, i.e.matrix. Multiplying a vector or matrix by a scalar will scale each element of the vector or matrix you can’t divide by zero or multiply a 1x2 matrix by a 3x by the value of the scalar.

C = 2*[1 2; 3 4] C = (^26 )

v = 2*[1 2 3 4] v = 2 4 6 8 Adding a scalar to a vector or matrix will add the value of the scalar to each element of the matrix: D = 2+[1 2; 3 4] D = (^35 )

s = 2 + [0:5] s = 2 3 4 5 6 7 Matrices and vectors may be added or subtracted as long as they are the same size. They may bemultiplied as long as there are the same number of columns in the first as there are rows in the second. s + 2[0:5] ans = 2 5 8 11 14 17 AB ans =

144 1458 1692 10 71 184

BA ans = 153 81 117 93 Putting a period in front of the multiplication, division or power operator performs that operationcomponentwise, i.e. (x.y) i =^ xi ·^ yi or (x.∧^2 ) i =^ x^2 i. [1 2 3 4].ˆ ans = 1 4 9 16 [1 2 3 4].*[5 0 5 0] ans = 5 0 15 0

Running .m files a .m^ You can write scripts of MATLAB functions which may be run as a whole. Save the files with extension so that MATLAB will recognize it as a script. Type

junk to run the script in the fileFUNCTION AS THE TITLE OF YOUR SCRIPT. junk.m. DO NOT USE THE NAME OF AN INTRINSIC MATLAB

Creating your own functions You can create your own functions to use in MATLAB. There are two options for this. If your function is simple (i.e. f ( x ) = x^2 + 2 x ), then you may enter it using the command inline:

f = inline(’x.ˆ2 + 2x’) f = Inline function: f(x) = x.ˆ2 + 2x We can then apply this function to a scalar or vector since we used thethe square of x: .∧ 2 command for computing

f([0:5]) ans = 0 3 8 15 24 35 If the function is more complicated, you may create a file whose name ends inMATLAB how to compute the function. Type help function to see the format of the function .m which tells file. Our function here could be computed using the following file (called f.m): function [output] = f(x), output = x.ˆ2 + 2*x This function is called from MATLAB in the same way as above, i.e. f(x), where x can be a scalar or vector. Importing data Use the load command to bring data into matlab from an external file: load filename

Clearing variables You may clear a particular variable by typing

clear x or all variables with clear all

Formatting MATLAB output numbers is very large or all of them are very small, MATLAB uses scientific notation. However,^ By default, MATLAB outputs numbers with four digits after the decimal point. If one of the the exponent is written only once at the beginning of the output, so be careful. For example:

[1 2 6 24 120 factorial(20)] ans = 1.0e+18 * 0.0000 0.0000 0.0000 0.0000 0.0000 2. To control the format of the output, type format long e for scientific notation with 15 digits. Other options include format short e, format short, format long, format short g. The default is format short. Getting help Type help followed by the name of the function. For example: help plot To get more help, try helpwin, helpdesk, demo or tour. Also, there is help available on the Math Works website at http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.shtml.