660 likes | 840 Views
M ATLAB B ASICS. Sanjay R Joshi sanjayrjoshi@yahoo.com 18-July-2005 MATLAB for ELECTRICAL ENGINEERING ELECTRICAL ENGINEERING DEPARTMENT L.D.COLLEGE OF ENGINEERING AHMEDABAD. MATLAB - developed by Math Works Inc. http://www.mathworks.com
E N D
MATLABBASICS Sanjay R Joshi sanjayrjoshi@yahoo.com 18-July-2005 MATLAB for ELECTRICAL ENGINEERING ELECTRICAL ENGINEERING DEPARTMENT L.D.COLLEGE OF ENGINEERING AHMEDABAD
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
Why MATLAB is popular ? • 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 some problems.
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.
Easy to use Platform independence Predefined functions Device –independent plotting
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 To visualize graphs and plots
Provides a variety of graphic output displays: • linear • log-log • semilog • polar • bar chart, and • contour plots • 2-D and 3-D views
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 • many special functions
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
Simple Math » 2+2.5+106 ans = 110.5000 » 4*25 + 2^3 ans = 108 » One can use it as a calculator/scratch pad
MATLAB Variables » a = 2 a = 2 »b = 3 b= 3
MATLAB Variables » c = 1000; » d = 0.001; » e = a*b*c/d e = 6000000 »
MATLAB Variables • MATLAB variable are automatically 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.
MATLAB Workspace To recall the variable » a a = 2 » Use arrow keys for scrolling through previous commands
MATLAB Workspace List of variables in the workspace » who Your variables are: a b c d e »
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
To Clear a Variable » who Your variables are: a b c d e » clear a; » who Your variables are: b c d e
To clear all variables » who Your variables are: b c d e » clear » who »
Complex Numbers » i ans = 0 + 1.0000i » c1 = 2+3i c1 = 2.0000 + 3.0000i »
Mathematical Functions » x=sqrt(2)/2 x = 0.7071 » y=sin(x) y = 0.6496 »
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
Array Operations » x = [1 2 3 4 5 6 7 8 9 10] x = 1 2 3 4 5 6 7 8 9 10 » a = 1:2:10 Note : operator a = 1 3 5 7 9
Array Operations » y = sin(x) y = 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
Array Addressing » y(3) ans = 0.1411 » y(1:5) ans = 0.8415 0.9093 0.1411 -0.7568 -0.9589
Array Orientation » z = [1; 2; 3; 4] z = 1 2 3 4 » z' ans = 1 2 3 4
Array Manipulation » A = [1 2; 3 4; 5 6] A = 1 2 3 4 5 6 » B = [1 2 3; 4 5 6]; » A+B ??? Error using ==> + Matrix dimensions must agree. »
Array Manipulation » A*B ans = 9 12 15 19 26 33 29 40 51
Significance of . » clear » A = [1 2; 3 4]; » B = [1 1; 2 2]; » A.*B ans = 1 2 6 8 »
Significance of . » 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»
Matrix Operations » [A B] ans = 1 2 1 1 3 4 2 2 » ans-1 ans = 0 1 0 0 2 3 1 1 »
Matrix Operations >> G= A - B G = 0 1 1 2 >> D = A +B D = 2 3 5 6
Matrix Operations » C = [A B] C = 1 2 1 1 3 4 2 2 » C(1,:) ans = 1 2 1 1 » » C(:,2) ans = 2 4 » C(:,2:end) ans = 2 1 1 4 2 2 »
Matrix Functions » size(C) ans = 2 4 » determ(A) ans = -2 » » A A = 1 2 3 4 » inv(A) ans = -2.0000 1.0000 1.5000 -0.5000
Matrix Functions Some functions: determ, inv, diag, triu, tril, rank, eig, size » eig(A) ans = -0.3723 5.3723 Show eigshow » [a b] = eig(A) a = -0.8246 -0.4160 0.5658 -0.9094 b = -0.3723 0 0 5.3723
Standard Arrays » eye(2) ans = 1 0 0 1 » eye(2,3) ans = 1 0 0 0 1 0 » Other such arrays: ones(n), ones(r, c) zeros(n), zeros(r, c) rand(n), rand(r,c)
ones(3) ans = 1 1 1 1 1 1 1 1 1 >> ones(3,2) ans = 1 1 1 1 1 1 zeros(2) ans = 0 0 0 0 >> zeros (3,2) ans = 0 0 0 0 0 0 Standard Arrays
Plots » x = 1:2:50; » y = x.^2; » plot(x,y) »
Plots » plot(x,y,'*-') » xlabel('Values of x') » ylabel('y') »
Relational Operators < > <= >= == ~=
Control Flow Statements for n=1:10 … end while expression … end if expression … else ... end
MATLAB Files M-files Script Files Function Files MAT-files MEX-files
M Files M-files are ordinary ASCII (text) files having extension .m containing MATLAB commands
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)
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