matlab basics mee

Download Matlab Basics MEE

Post on 18-Apr-2015

14 views

Category:

Documents

4 download

Embed Size (px)

TRANSCRIPT

MATLAB

BASICS20-Jan-2007

Sanjay R Joshi sanjayrjoshi@yahoo.com

MATLAB for ELECTRICAL ENGINEERINGEE DEPT IIT BOMBAY

1

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 tools2

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 plotting5

MATLAB WINDOWSCOMMAND 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

6

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

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, and8

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 C9

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 pad12

MATLAB Variablesa=2 a= 2 b = 3 b= 313

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

14

MATLAB VariablesMATLAB

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 WorkspaceTo recall the variablea a= 2

Use arrow keys for scrolling through previous commands16

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

17

Variable Naming RulesVariable 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_power2by318

Special Variablesans pi inf NaN i, jDefault 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 e20

To clear all variables who Your variables are: b c d e clear who

21

Complex Numbersi 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 l25

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 926

4

5

6

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

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

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

Array Orientation

4

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 Operations2 4 1 2 1 2

1 3

0 1

0 134

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 236

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

Matrix FunctionsSome 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 ArraysOther 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 Arrayszeros(2) ans = 1 1 1 1 1 1 0 0 0 0 >> zeros (3,2) ans = 0 0 0 0 0 040

>> 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 Statementsfor n=1:10 end while expression end if expression else ... end44

MATLAB FilesM-files Script Files Function Files MAT-files MEX-files45

M FilesM-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

SCRIPTSVariables 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

FUNCTIONSThey 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 vectorized53

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 end55

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 i