You are on page 1of 66

MATLAB

BASICS
20-Jan-2007

Sanjay R Joshi sanjayrjoshi@yahoo.com

MATLAB for ELECTRICAL ENGINEERING


EE DEPT IIT BOMBAY

MATLAB

- developed by Math Works Inc.

http://www.mathworks.com MATLAB - acronym for MATrix LABoratory Matrices and arrays - the heart of MATLAB Offers programming features - similar to other languages Offers GUI tools
2

Why MATLAB is popular ?


1. 2. 3. 4.

5.

No need to declare dimensions of a matrix as in FORTRAN or C. Very powerful and easy-to-learn graphics capabilities. MATLAB can be integrated with FORTRAN and C/C++ programs. MATLAB has many toolboxes (collections of MATLAB functions) applied to specific topics like control system, symbolic manipulation, FFT etc. MATLAB has proved to be extremely efficient and hence very fast for solving 3

MATLAB is an interactive system for doing numerical computations Powerful operations can be performed using just one or two commands. There are more than 1000 functions in the basic MATLAB product alone. Modeling, simulation, Data analysis, exploration, and visualization and many more. Application development, including Graphical User Interface building.
4

Easy to use Platform independence Predefined functions Device independent plotting


5

MATLAB WINDOWS
COMMAND WINDOW To give MATLAB commands at the prompt EDIT/DEBUG WINDOW To create , edit and debug the program. Type edit at the command prompt. FIGURE WINDOW

Provides a variety of graphic output displays: linear log-log semilog polar bar chart, and contour plots 2-D and 3-D views
7

Provides extensive numerical resources Over 200 reliable, accurate mathematical subprograms The subprograms provide solutions to broad range of mathematical problems including: matrix algebra ( Linear Algebra ) complex arithmetic differential equations nonlinear systems, and
8

Available in all operating systems: DOS Windows9.x/NT Unix Macintosh Same syntax for all platforms Open system environment - access to source code Allows - to mix MATLAB with FORTRAN or C
9

10

11

Simple Math
2+2.5+106 ans = 110.5000 4*25 + 2^3 ans = 108 One can use it as a calculator/scratch pad
12

MATLAB Variables
a=2 a= 2 b = 3 b= 3
13

MATLAB Variables
c = 1000; d = 0.001; e = a*b*c/d e= 6000000

14

MATLAB Variables
MATLAB

created when they are initialized. There are three way to initializing variable in MATLAB.
Assign data to the variable in an assignment statement. Input data into the variable from the keyboard. Read data from file. 15

variable are automatically

MATLAB Workspace
To recall the variable
a a= 2

Use arrow keys for scrolling through previous commands


16

MATLAB Workspace
List of variables in the workspace
who Your variables are: a b c d e

17

Variable Naming Rules


Variable names are case sensitive. For example NRe and nRe are different MATLAB variables Variable name can contain upto 31 characters. Start with a letter, followed by letters, numbers or underscores. For example: SRe_2_electrical_power2by3
18

Special Variables
ans pi inf NaN i, j
Default variable name used for resuts Value of Stands for infinity (e.g., 1/0) Stands for Not-a-Number (e.g., 0/0) i = j = -1

19

To Clear a Variable
who Your variables are: a b c d e clear a; who Your variables are: b c d e
20

To clear all variables


who Your variables are: b c d e clear who

21

Complex Numbers
i ans = 0 + 1.0000i c1 = 2+3i c1 = 2.0000 + 3.0000i
22

x=sqrt(2)/2 x= 0.7071 y=sin(x) y= 0.6496

Mathematical Functions

23

T ig n m t ic r oo er fnt n u c io s

Built-in Functions

E p n n ia xoet l fnt n u c io s C m le o p x fnt n u c io s R u d ga d o n in n R m in e e a dr fnt n u c io s

sin co tan sin aco atan , s, , , s, , sin , co , tan , asin , h sh h h aco , atan , csc, sec, co sh h t, acsc, ex , lo , lo 1 , sq p g g 0 rt ab an le, im , real, co j s, g ag n flo r, o sig n ceil, ro n , m d rem ud o, ,

24

Saving & Loading Data


>> A=[2 0; 0 5]; >> [M l]=eig(A); >> save data1 M l; clear all; >> who >> load data1 >> who Your variables are: M l
25

Array Operations
x = [1 2 3 4 5 6 7 8 9 10] x= 1 2 3 a = 1:2:10 a= 1 3 5 7 9
26

7 8 9 10 Note : operator

y = sin(x) y=

Array Operations

Columns 1 through 7 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 Columns 8 through 10 0.9894 0.4121 -0.5440
27

Array Addressing
y(3) ans = 0.1411 y(1:5) ans = 0.8415 -0.9589 0.9093 0.1411 -0.7568
28

z = [1; 2; 3; 4] z= 1 2 3 4 z' ans = 1 2 3

Array Orientation

29

Array Manipulation
A = [1 2; 3 4; 5 6] A= 1 3 5 2 4 6

B = [1 2 3; 4 5 6]; A+B ??? Error using ==> + Matrix dimensions must agree.
30

Array Manipulation
A*B ans = 9 12 15 19 26 33 29 40 51

31

Significance of
clear A = [1 2; 3 4]; B = [1 1; 2 2]; A.*B ans = 1 6 2 8

32

A /B ans = 1.0000 2.0000 1.5000 2.0000 A/B Warning: Matrix is singular to working precision. ans = Inf Inf Inf Inf

Significance of

33

[A B] ans = 1 3 ans-1 ans = 0 2

Matrix Operations
2 4 1 2 1 2

1 3

0 1

0 1
34

Matrix Operations
>> D = A +B D= 2 5 3 6 >> G= A - B G= 0 1 1 2

35

Matrix Operations
C = [A B] C= 1 3 2 4 1 2 1 2 C(:,2) ans = 2 4 C(:,2:end) ans = 2 1 1 2 4 1 2 1 2
36

C(1,:) ans = 1

Matrix Functions
size(C) ans = 2 4 A A= 1 3 2 4

determ(A) ans = -2

inv(A) ans = -2.0000 1.0000 1.5000 -0.5000


37

Matrix Functions
Some functions: determ, inv, diag, triu, tril, rank, eig, size eig(A) ans = -0.3723 5.3723 [a b] = eig(A) a= -0.8246 -0.4160 0.5658 -0.9094 b= -0.3723 0 0 5.3723

Show eigshow

38

eye(2) ans = 1 0

Standard Arrays
Other such arrays: ones(n), ones(r, c) 0 1 zeros(n), zeros(r, c) rand(n), rand(r,c)

eye(2,3) ans = 1 0 0 1 0 0

39

ones(3) ans = 1 1 1

Standard Arrays
zeros(2) ans = 1 1 1 1 1 1 0 0 0 0 >> zeros (3,2) ans = 0 0 0 0 0 0
40

>> ones(3,2) ans = 1 1 1 1 1 1

Plots
x = 1:2:50; y = x.^2; plot(x,y)

41

Plots
plot(x,y,'*-') xlabel('Values of x') ylabel('y')

42

Relational Operators < > <= >= == ~=


43

Control Flow Statements


for n=1:10 end while expression end if expression else ... end
44

MATLAB Files
M-files Script Files Function Files MAT-files MEX-files
45

M Files
M-files are ordinary ASCII (text) files having extension .m containing Script MATLAB commandsFunction M-Files M-Files Do not accept input arguments or return output arguments Operate on data in the workspace Can accept input arguments and return output arguments Internal variables are local to the function by defaultfor extending the Useful

Useful for automating a series of steps you need MATLAB language for to perform many times your application 46

SCRIPTS
Variables created by scripts remains in workspace even after the execution of it. Create a script file and edit it in MATLAB Editor called M-File Editor e.g % This is a sample Script M-file A = [ 1 2 3; 4 7 1; 9 5 0]; [x,v] = eig(A)

47

48

Note script her

49

Using Script M-files


what M-files in the current directory C:\WINDOWS.000\Desktop\MEE\ASSIGN-1 solu_AC_Series_Ckt solu_AC_Series_Ckt

50

Using Script M-files


Supply frequency in Hertz = 50 Supply Volatage in Volts =230 Resistance in Ohms = 10 Inductance in Henry = 0.5 Capacitance in Micro Farad = 1000 save data1 P Q V I pf This is one of the saving option of MATLAB

51

FUNCTIONS
They operate on variables of their own work space. This is not the same , we access at the MATLAB command prompt. Create a M-File , Save it and then call it. You may call it to execute at command prompt or within any

52

FUNCTION

M-Files

Function file and Script file can call each other. Multiple functions can appear in a single function M file. [Sub function] All the sub functions follow the rules of M-files. Try to use vectorized
53

Keyword

Output argument(s) Input argument(s) Function name

General structure of a Function M-file

Function function [o1,o2,...] = fun_name(i1,i2...) definition line % This is the H1 line or first help text line % The rest of the help text... % ... % ... function body here (All MATLAB code doing the actual calculations come here) % Comments, if any.
54

maxpow.m

Function M-files

function [maxpower,rs,rl] = maxpow(V,rs) % [Max Power , Rs , RL ] = maxpow(Voltage,Rs) clc; maxpower=0.0; range = rs/10; Rl= (rs- range/2):range/6:(rs + range/2); for i = 1:length(Rl) I(i)=V/(rs + Rl(i)); power(i)= I(i)^2*Rl(i); if power(i) > maxpower maxpower = power(i); rl=Rl(i); end end
55

MAT

Files

MAT-files are the data file format files that MATLAB uses for saving data and workspace to disk. They are machine dependent binary files. However, they can be ported to different architectures as MATLAB can sniff out the architecture information from the MAT-files. Thus a sort of machine independence is achieved with MAT-files. Whenever we want to save MATLAB Workspace or only some variables in the Workspace, we can do so by issuing the command `save filename' at the MATLAB prompt. 56

MEX

Files

Possible to call FORTRAN and C subroutines from inside MATLAB as if they are built-in functions. The way to do it is through what are called MATLAB EXecutable files (MEX-files).

57

Why MEX-files?

There is no need to rewrite long preexisting C or FORTRAN code into M-files Serious constraints like for-loops, which tend to slow down MATLAB can be made to run faster by recoding into C or FORTRAN and running as MEX-files
58

Calling a Function
[mp rs rl] = maxpow(200,5) mp = 2000 rs = 5 rl = 5

59

Calling a Function
You may call your function by less number of output arguments [mp r ] = maxpow(200,5) mp = 2000 r= 5 Here r is our second argument variable

60

Polynomials
Representing Polynomials: x4 - 12x3 + 25x + 116 P = [1 -12 0 25 116]; roots(P) ans = 11.7473 2.7028 -1.2251 + 1.4672i -1.2251 - 1.4672i r = ans;
61

Polynomials from the Roots


PP = poly(r) PP = 1.0000 -12.0000 -0.0000 25.0000 116.0000

62

Polynomial Multiplication
a = x3 + 2x2 + 3x + 4 b = 4x2 + 9x + 16 a = [1 2 3 4]; b = [4 9 16]; c = conv(a,b) c= 4 17 46 75 84 64
63

Recommended books
Mastering MATLAB Duane Hanselman, Bruce Littlefield The MATLAB Curriculum Series, Prentice Hall Inc. MATLAB Programming for Engineers Stephan J. Chapan

Thomson Publication
Introduction to Matlab 6.0 2nd edition Delores M. Etter David C.Kuncicky Doug Hull Pearson Education http://www.mathworks.com
64

LETS START

MATLABing
65

THANK VERY

YOU MUCH

66

You might also like