Matlab Basics MEE

Download Matlab Basics MEE

Post on 18-Apr-2015

14 views

Category:

Documents

4 download

Embed Size (px)

TRANSCRIPT

<p>MATLAB</p> <p>BASICS20-Jan-2007</p> <p>Sanjay R Joshi sanjayrjoshi@yahoo.com</p> <p>MATLAB for ELECTRICAL ENGINEERINGEE DEPT IIT BOMBAY</p> <p>1</p> <p> MATLAB</p> <p>- developed by Math Works Inc.</p> <p>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</p> <p>Why MATLAB is popular ?1. 2. 3. 4.</p> <p>5.</p> <p>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</p> <p>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</p> <p>Easy to use Platform independence Predefined functions Device independent plotting5</p> <p>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</p> <p>6</p> <p> Provides a variety of graphic output displays: linear log-log semilog polar bar chart, and contour plots 2-D and 3-D views7</p> <p> 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</p> <p> 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</p> <p>10</p> <p>11</p> <p>Simple Math 2+2.5+106 ans = 110.5000 4*25 + 2^3 ans = 108 One can use it as a calculator/scratch pad12</p> <p>MATLAB Variablesa=2 a= 2 b = 3 b= 313</p> <p>MATLAB Variables c = 1000; d = 0.001; e = a*b*c/d e= 6000000 </p> <p>14</p> <p>MATLAB VariablesMATLAB</p> <p>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</p> <p>variable are automatically</p> <p>MATLAB WorkspaceTo recall the variablea a= 2 </p> <p>Use arrow keys for scrolling through previous commands16</p> <p>MATLAB WorkspaceList of variables in the workspace who Your variables are: a b c d e </p> <p>17</p> <p>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</p> <p>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</p> <p>19</p> <p>To Clear a Variable who Your variables are: a b c d e clear a; who Your variables are: b c d e20</p> <p>To clear all variables who Your variables are: b c d e clear who </p> <p>21</p> <p>Complex Numbersi ans = 0 + 1.0000i c1 = 2+3i c1 = 2.0000 + 3.0000i 22</p> <p> x=sqrt(2)/2 x= 0.7071 y=sin(x) y= 0.6496 </p> <p>Mathematical Functions</p> <p>23</p> <p>T ig n m t ic r oo er fnt n u c io s</p> <p>Built-in Functions</p> <p>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</p> <p>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, ,</p> <p>24</p> <p>Saving &amp; Loading Data&gt;&gt; A=[2 0; 0 5]; &gt;&gt; [M l]=eig(A); &gt;&gt; save data1 M l; clear all; &gt;&gt; who &gt;&gt; load data1 &gt;&gt; who Your variables are: M l25</p> <p>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</p> <p>4</p> <p>5</p> <p>6</p> <p>7 8 9 10 Note : operator</p> <p> y = sin(x) y=</p> <p>Array Operations</p> <p>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</p> <p>Array Addressing y(3) ans = 0.1411 y(1:5) ans = 0.8415 -0.9589 0.9093 0.1411 -0.756828</p> <p> z = [1; 2; 3; 4] z= 1 2 3 4 z' ans = 1 2 3</p> <p>Array Orientation</p> <p>4</p> <p>29</p> <p>Array Manipulation A = [1 2; 3 4; 5 6] A= 1 3 5 2 4 6</p> <p> B = [1 2 3; 4 5 6]; A+B ??? Error using ==&gt; + Matrix dimensions must agree. 30</p> <p>Array Manipulation A*B ans = 9 12 15 19 26 33 29 40 51</p> <p>31</p> <p>Significance of clear A = [1 2; 3 4]; B = [1 1; 2 2]; A.*B ans = 1 6 2 8</p> <p>.</p> <p>32</p> <p> 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</p> <p>.</p> <p>Significance of</p> <p>.</p> <p>33</p> <p> [A B] ans = 1 3 ans-1 ans = 0 2 </p> <p>Matrix Operations2 4 1 2 1 2</p> <p>1 3</p> <p>0 1</p> <p>0 134</p> <p>Matrix Operations&gt;&gt; D = A +B D= 2 5 3 6 &gt;&gt; G= A - B G= 0 1 1 2</p> <p>35</p> <p>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</p> <p> C(1,:) ans = 1 </p> <p>Matrix Functions size(C) ans = 2 4 A A= 1 3 2 4</p> <p> determ(A) ans = -2 </p> <p> inv(A) ans = -2.0000 1.0000 1.5000 -0.500037</p> <p>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</p> <p>Show eigshow</p> <p>38</p> <p> eye(2) ans = 1 0</p> <p>Standard ArraysOther such arrays: ones(n), ones(r, c) 0 1 zeros(n), zeros(r, c) rand(n), rand(r,c)</p> <p> eye(2,3) ans = 1 0 0 1 0 0</p> <p>39</p> <p>ones(3) ans = 1 1 1</p> <p>Standard Arrayszeros(2) ans = 1 1 1 1 1 1 0 0 0 0 &gt;&gt; zeros (3,2) ans = 0 0 0 0 0 040</p> <p>&gt;&gt; ones(3,2) ans = 1 1 1 1 1 1</p> <p>Plots x = 1:2:50; y = x.^2; plot(x,y) </p> <p>41</p> <p>Plots plot(x,y,'*-') xlabel('Values of x') ylabel('y') </p> <p>42</p> <p>Relational Operators &lt; &gt; = == ~=43</p> <p>Control Flow Statementsfor n=1:10 end while expression end if expression else ... end44</p> <p>MATLAB FilesM-files Script Files Function Files MAT-files MEX-files45</p> <p>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</p> <p>Useful for automating a series of steps you need MATLAB language for to perform many times your application 46</p> <p>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)</p> <p>47</p> <p>48</p> <p>Note script her</p> <p>49</p> <p>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</p> <p>50</p> <p>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</p> <p>51</p> <p>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</p> <p>52</p> <p>FUNCTION</p> <p>M-Files</p> <p>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</p> <p>Keyword</p> <p>Output argument(s) Input argument(s) Function name</p> <p>General structure of a Function M-file</p> <p>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</p> <p>maxpow.m</p> <p>Function M-files</p> <p>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) &gt; maxpower maxpower = power(i); rl=Rl(i); end end55</p> <p>MAT</p> <p>Files</p> <p>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</p> <p>MEX </p> <p>Files</p> <p>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).</p> <p>57</p> <p>Why MEX-files?</p> <p>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-files58</p> <p>Calling a Function [mp rs rl] = maxpow(200,5) mp = 2000 rs = 5 rl = 5</p> <p>59</p> <p>Calling a FunctionYou 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</p> <p>60</p> <p>PolynomialsRepresenting 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</p> <p>Polynomials from the Roots PP = poly(r) PP = 1.0000 -12.0000 -0.0000 25.0000 116.0000 </p> <p>62</p> <p>Polynomial Multiplicationa = 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</p> <p>Recommended books Mastering MATLAB Duane Hanselman, Bruce Littlefield The MATLAB Curriculum Series, Prentice Hall Inc. MATLAB Programming for Engineers Stephan J. Chapan</p> <p>Thomson Publication Introduction to Matlab 6.0 2nd edition Delores M. Etter David C.Kuncicky Doug Hull Pearson Education http://www.mathworks.com64</p> <p>LETS START</p> <p>MATLABing65</p> <p>THANK VERY</p> <p>YOU MUCH</p> <p>66</p>