Vous êtes sur la page 1sur 11

MATLAB TUTORIAL FOR BEGINNERS

EE1347 Section 501 GTA: eduardo.perez@uta.edu

Introduction
MATLAB: MATrix LABoratoy. Designed for matrix computations With a variety of graphical capabilities It has its own programming language, similar to C MATLAB is not a symbolic computation system (SCS) such as Mathematica: this does not make it better or worse, it is a tool designed for different tasks and is therefore not directly comparable

Why MATLAB in EE!?: Toolboxes


Automatic Control Control Toolbox (& SIMULINK) Digital Signal Processing (DSP) & Telecomm wavelet, filter design, Signal Processing toolboxes, Communications Power Systems Power Toolbox & free MATLAB base software MATPOWER Other useful toolboxes in EE: RF toolbox Optimization toolbox Partial differential equation Linear programming And much more Type demo in command window just to see an example

MATLAB ENVIRONMENT
Command Window Workspace
Save workspace option

Current Directory (Set Path: J:\EE1347) Command History

MATLAB: matrix calculator


In MATLAB everything is a matrix Entering vectors and matrices:
Separate the elements of a row with blanks or commas Use a semicolon (;) to indicate the end of each row Surround the entire list of elements with square brackets [ ] A=[16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

Arithmetic operators
A+B & A-B: A and B must have the same size Matrix multiplication: C=A*B Array multiplication: D=A.*B is the element by element product of the arrays A and B. A & B must have the same size Matrix right division: B/A = B*inv(A) Array right division: A./B = A(i,j)/B(i,j) Matrix left division: A\B = inv(A)*B Array left division: A.\B = B(i,j)/A(i,j) Some miscellaneous commands

Graphics:

GRAPHS

Creating a Plot Figure Windows Adding plots to an existing graph Exercise1.m: plot for 0.01t 0.01, with time intervals of 1e-7 sec.
p (t ) = Z I 2 cos Z + Z I 2 cos( 2t + Z )

Z= I=10, Z = 45, =2f, f = 60 Hz. Find maximum & minimum value of p(t). 9 In the same graph, plot ZI2cos(2 t + Z) (hold on) 9 Plot both in different graphs. (figure)

3D PLOTS: meshgrid & mesh


To display a function of two variables, z=f(x,y), generate X and Y matrices consisting of repated rows and columns, respectively, over the domain of the function. Then use these matrices to evaluate and graph the function. The meshgrid function transforms the domain specified by a single vector of two variables. The rows of X are copies of the vector x and the columns of Y are copies of the vector y.

Programming in MATLAB
Flow control:
If Switch and case For While Break: lets you exit early from a for or while loop.

Programming in MATLAB
Scripts: When you invoke a script, MATLAB simply executes the commands found in the file. Although scripts do not return output arguments, any variables that they create remain in the workspace, to be used in subsequent computations. In addition, scripts can produce graphical output using functions like plot. For example, create a file called magicrank.m that contains these MATLAB commands: % Investigate the rank of magic squares r = zeros(1,32); for n = 3:32 r(n) = rank(magic(n)); end r bar(r)

Programming in MATLAB
Functions: are M-files that can accept input arguments and return output arguments. The name of the M-file and of the function should be the same. Syntax:
function [output1, output2,] = function-name(input1,input2,)

For example, create a function root2.m to calculate the roots of the quadratic polynomial ax2+bx+c=0 using:

b b 2 + 4ac x1 , x2 = 2a

Vous aimerez peut-être aussi