Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Material Type: Notes; Class: Reinfor Learning/Artif Intell; Subject: Electrical And Computer Engr; University: University of Tennessee - Knoxville; Term: Unknown 2000;

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-6ic
koofers-user-6ic 🇺🇸

10 documents

1 / 8

Toggle sidebar

Related documents


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 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 = [ 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 1 2 3 4 1 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 2 3 7 12 8] A = 0 2 3 7 12 8 The (complex conjugate) transpose of a vector or matrix may be obtained by placing an apostrophe after the expression: >> w = z’ w = 0 1 2 3 4 >> B = A’ B = 0 3 12 2 7 8 To give a variable a set of evenly-spaced values, use the colon operator: >> t = 0:6 t = 0 1 2 3 4 5 6 >> u = 0:0.3:1.8 u = 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 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 a spacing of 1. 2 >> 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 be multiplied 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 >> A*B ans = 4 14 16 14 58 92 10 71 184 >> B*A ans = 153 81 117 93 Putting a period in front of the multiplication, division or power operator performs that operation componentwise, i.e. (x.*y)i = xi · yi or (x.∧2)i = x2i . >> [1 2 3 4].ˆ2 ans = 1 4 9 16 >> [1 2 3 4].*[5 0 5 0] ans = 5 0 15 0 5 For loops To run a command more than once as an index varies, you may use a for loop. In the following example, a for loop is used to compute n! for n = 1, . . . ,10: >> j = 1; >> for i = 1:10 j = j*i end You may also do the loop with the values of i in the matrix [0,2,3,6]: >> for i = [ 0 2 3 6] DO SOMETHING end Note that MATLAB will not run the for loop until you have hit return after typing end to indicate the end of the for loop. I find it useful to use the tab key to indent all commands within the loop. This can make the code much easier to read and understand. If statements MATLAB uses a similar structure for if statements: >> if i == 1 DO SOMETHING elseif i == 2 DO SOMETHING ELSE elseif i == 3 DO SOMETHING ELSE else DO SOMETHING (IF i >= 4 or i <= 0) end Plotting To make a line plot of t versus √ t, simply type: >> plot(t,sqrt(t)) To make a plot to t versus √ t and t versus t on the same plot, type >> plot(t,sqrt(t),t,t) putting each x-y pair together. You can add symbols or use symbols instead of lines by adding commands to each pair: >> plot(t,sqrt(t),’*-’,t,t,’o-’) See help plot for more information on plotting and a catalog of the available symbols and line types. Use legend(’sqrt(t)’,’t’) to label the different lines. Title your plot by typing title(’A plot of t versus sqrt(t)’). You can add labels to the axes similarly: xlabel(’t’) or ylabel(’sqrt(t)’). Other useful commands: axis, plot3 and (for a bit of fun) comet. 6 Running .m files You can write scripts of MATLAB functions which may be run as a whole. Save the files with a .m extension so that MATLAB will recognize it as a script. Type >> junk to run the script in the file junk.m. DO NOT USE THE NAME OF AN INTRINSIC MATLAB FUNCTION AS THE TITLE OF YOUR SCRIPT. 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) = x2 +2x), then you may enter it using the command inline: >> f = inline(’x.ˆ2 + 2*x’) f = Inline function: f(x) = x.ˆ2 + 2*x We can then apply this function to a scalar or vector since we used the .∧2 command for computing the square of x: >> f([0:5]) ans = 0 3 8 15 24 35 If the function is more complicated, you may create a file whose name ends in .m which tells MATLAB how to compute the function. Type help function to see the format of the function 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 7
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved