43
MATLAB - Definition The MATLAB System MATLAB Development Environment MATLAB Programming AN INTRODUCTION TO MATLAB !!! Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. Kannur October 13, 2011 Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. An Introduction to Matlab

matlab

Embed Size (px)

DESCRIPTION

matlab

Citation preview

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

AN INTRODUCTION TO MATLAB !!!

Dr. C. SREEKUMARAssociate Professor

Dept. of Electrical & Electronics Engg.Govt. College of Engg. Kannur

October 13, 2011

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Outline

1 MATLAB - Definition

2 The MATLAB System

3 MATLAB Development Environment

4 MATLAB Programming

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Definition

MATLAB R© is a high-performance language for technicalcomputing. It integrates computation, visualization andprogramming in an easy-to-use environment where problemsand solutions are expressed in familiar mathematical notation.Typical uses include Math and computation, Algorithmdevelopment, Data acquisition, Modeling, simulation,prototyping, Data analysis, exploration, visualization, Scientificand engineering graphics, Application development includinggraphical user interface building

-’The MathWorks, Inc.’

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB

MATRIX LABORATORYEverything in MATLAB is a matrixThe basic data element in MATLAB is an array that doesnot require dimensioning.An interactive software package/language designed forscientific and engineering computations - Making itSimple!!!!!An interpreted language - Commands executed line by linePrecise numerical calculations and graphics illustrationsare possible

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB............ ?

MATLAB commands are expressed in a notation close tothat used in Mathematics and Engineering

Eg: sin,log,abs ......

Problem solving in MATLAB is generally much faster thantraditional programming

Eg: inv

Single MATLAB command to find the inverse of a matrixBecause of natural notation, MATLAB code is clear andself documentedMATLAB Software is written in Optimized C and assemblylanguage- to obtain high performance

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB............ ?

MATLAB commands are MATLAB functions generallystored as an M file (File stored with .m as extension)

Eg: sin.m , plot.m etc...

The built in M files are open. Modifications are possible.One can create user defined M files /functions for solvingcertain problems

sin.m is a built-in function to calculate ’sin’ of an angleBut no built in function is available to find sine square of anangleUser can create a function named sin2 (file sin2.m) to dothe same

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB............ ?

There is nothing one can do in Fortran or C that can not bedone in MATLAB (and in a much easier fashion)MATLAB has a rich set of Toolboxes/application-specificsolutions to do specialized workToolboxes are comprehensive collections of MATLABfunctions (M-files) that extend the MATLAB environment tolearn and apply specialized technology

SimulinkElectrical - Control Systems, Signal processing, PowersystemsMathematics - PDE , Fuzzy logic , Optimization, Waveletetc..

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB............ ?

MATLAB has a good demo program for beginnersAll the help, manuals etc. are freely available athttp://www.mathworks.com

MATLAB is costly ???Scilab by INRIA is an Alternative. It is Open Source andfree

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB System

Five main partsDevelopment Environment - Set of tools and facilities thathelps to use MATLAB functions and files - GUI’sThe MATLAB Mathematical Function Library - a vastcollection of computational algorithms⇒ sum, sine,cosine, matrix inverse, matrix eigenvalues, Besselfunctions, and fast Fourier transformsThe MATLAB Language - high-level matrix/array languagewith control flow statements, functions, data structures,input/output, and object-oriented programming features.

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB System

Graphics - functions for displaying vectors and matrices asgraphs, two and three-dimensional data visualization,image processing, animation, presentation graphics andbuilding complete GUIsThe MATLAB Application Program Interface (API) - librarythat allows to write C and Fortran programs that interactwith MATLAB⇒ facilities for calling routines from MATLAB (dynamiclinking), calling MATLAB as a computational engine

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB Environment Windows

MATLAB Desktop⇒ Place one reach on startingMATLAB. Contains GUI tools for managing files, variables,and applications such as Command window, currentDirectory, command history etc...

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB Desktop Tools

Command Window⇒ to enter variables and run functionsand M-files

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB Desktop Tools

Command History⇒ to view previously run commands/statements and to execute them

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB Desktop Tools

Workspace Browser⇒ to view the set of variables builtup during a MATLAB session and stored in memory

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB Desktop Tools

Current Directory Browser⇒ to view the content of thecurrent directory

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB Desktop Tools

Start Button and Launch Pad⇒ to access tools, demos,and documentation. The Launch Pad provides similaraccess in a tree view

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB Desktop Tools

Help Browser⇒ A Web browser integrated into theMATLAB desktop that displays HTML documents. To openthe Help browser, click the help button in the toolbar, ortype helpbrowser in the Command Window

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB Desktop Tools

Array Editor⇒ to view and edit an array. Double-click avariable in the Workspace browser to see it in the ArrayEditor

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB Desktop Tools

Editor/Debugger⇒ GUI to create and debug M-files/programs written to run MATLAB functions

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

MATLAB Desktop Tools

Profiler⇒ GUI to measure the computation time ofMATLAB command to improve the performance ofprograms. To open⇒ Type profile viewer in CW

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Matrix Operations

Entering a Matrix - Several waysEnter an explicit list of elementsLoad matrices from external data filesGenerate matrices using built-in functionsCreate matrices with your own functions in M-files

Basic ConventionsSeparate the elements of a row with blanks or commasUse a semicolon ; to indicate the end of each row.Surround the entire list of elements with square brackets [ ]

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Matrix Operations...Contd

» A= [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

A =

16 3 2 135 10 11 89 6 7 124 15 14 1

To suppress output, end the line with a semicolon» a = 3a=3» a = 3 ;Variable names⇒ Case Sensitive» B= [1,6,3,2;5,1,0,8;9,6,7,2;4,15,1,4];If variable name is not specified, the default name is ’ans’

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Matrix Operations.- Contd..

»A1 = [1 2 3;2 1 3;3 1 2]

A1 =1 2 32 1 33 1 2

»A2 = [A 3*A;A-(2*A) A+A]

A2 =

1 2 3 3 6 92 1 3 6 3 93 1 2 9 3 6−1 −2 −3 2 4 6−2 −1 −3 4 2 6−3 −1 −2 6 2 4

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Matrix Operations.- Contd..

sum(A) - gives the sum of column elementsTo find the sum of row elements transpose the matrix andtake the sum⇒ A’ gives the transpose of A⇒ sum(A’)diag(A) - gives the diagonal elements⇒sum(diag(A))- gives the sum of diagonal elementsThe element in row i and column j of A is denoted by A(i,j)⇒ A(1,1) gives 16

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Matrix Operations.- Contd..

eig(A) gives the eigen values of Ainv(A) gives the inverse of Adet(A) gives the determinant of Apoly(A) gives the coefficients of the characteristicpolynomialA(:,2) gives the elements of the 2nd columnA(2,:)=[3 6 4 3];⇒ replaces second row of A

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Some General Commands

A matrix entered is automatically remembered in theMATLAB Workspacewho - MATLAB replies with the variables in yourWorkspacewhos - MATLAB gives more details on Workspacevariableswhat -MATLAB replies with the current directory andMATLAB files in the directorysave file-name variable list Eg: save my-variables.dat A1,A2, Bclc - Clears the cluttered Command Window

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Some General Commands - Contd...

clear A1 - Removes the variable A1 from Workspaceclear all - Deletes everything from Workspaceload file-name - Retrieves the variables saved in the filenamed file-name Eg: load my-variables.datCan do the same for entire environment. Commands aresave myenv; clear all; load myenv;

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Some General Commands - Contd...

help - The most important function for self learningMATLABhelp matfun - all the basic matrix functionshelp datafun - all the basic data analysis functionshelp graph2d - all the basic 2-dimensional plottingfunctionshelp stats - all the functions in the MATLAB StatisticsToolboxhelp optim - all the functions in the MATLAB OptimizationToolbox

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Scalars and Vectors

A variable is a 1 X 1 arrayA linear array is a vectorMost variables will be arrays or matrices of doubles (64-bitdefault)) or 16-bit charOther types are also supported: complex, symbolic, 16-bitand 8 bit integers, etc.Variable Names

First character must be a LETTER after that, anycombination of letters, numbers and _CASE SENSITIVE!

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Scalars and Vectors

Built-in variablesi and j can be used to indicate complex numberspi has the value 3.1415926...Inf and -Inf are positive and negative infinityNaN represents ’Not a Number’

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Characters and Strings

» a=’s’;⇒ defines a character variable a with value ’s’» ’Welcome 2012’⇒ display the string with the stringstored as ’ans’To remove "ans =", disp() function can be used» disp(’Welcome’ 2012)MATLAB function sprintf() allows to mix strings withvariables » year=2012;» disp(sprintf(’Welcome %g’, year))The format is C-syntax

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Scalar Operations

Arithmetic operations (+,−, ∗, /)» 7/45» (1+i)*(2-i)Exponentiation ( )̂» 42

Complicated expressions, use parentheses» ((2 + 3) ∗ 3)0.1

Multiplication is NOT implicit» 3(1+0.7) gives an error» 3*(1+0.7) is correct

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Built in Functions

MATLAB has an enormous library of built-in functionsCall using parentheses - passing parameter to function

» sqrt(2)» log(2), log10(0.23)» cos(1.2), atan(-.8)» exp(2+4*i)» round(1.4), floor(3.3), ceil(4.23)» angle(i); abs(1+i);

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Help on Functions

To get info on how to use a function» help function-name % Eg. help cos

To get a better help with examples» doc sin

To search for a function by specifying keywords» lookfor One-word-description

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Functions- An Example

Verify that e(i∗x) = cos(x) + i ∗ sin(x) for a few values of x» x = pi/4;» a = exp(i ∗ x)» b = cos(x) + i ∗ sin(x)» a− b

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Arrays - More Operators

Element-wise operatorsOperators * / ˆ have element-wise operationTo do element-wise operations, dot . operator is used.Operand dimensions should match

» a = [112]; b = [2; 3; 1];» a. ∗ b, a./b, a. ˆ (b)⇒ gives errors» a. ∗ b, a./b, a.ˆ (b)⇒ valid

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Arrays - Automatic Generation

Initialize a vector of ones, zeros, or random numbers

» L=ones(1,10)→ row vector with 10 elements, all 1» z=zeros(5,1)→ column vector with 5 elements, all 0» r=rand(1,9)→ row vector with 9 elements (uniform [0,1])» n=nan(1,69)→ row vector of NaNs (useful for representing

uninitialized variables)

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Arrays - Automatic Generation - Contd....

linspace command is used to initialize a linear vector

» a=linspace(0,10,5)→ starts at 0, ends at 10 (inclusive), 5values

A colon operator (:) can also initialize a linear vector

» b=0:2:10→ starts at 0, increments by 2, and ends at orbefore 10

→ increment can be decimal or negative» c=1:5→ if increment is not specified, default is 1

To initialize logarithmically spaced values logspacecommand is used

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Plotting Curves

Generating data for plots

» x=linspace(0,4*pi,10);» y=sin(x);

Plot values against their index

» plot(y);

to plot y versus x» plot(x,y);

to make plot of a function look smoother, evaluate at morepoints

» x=linspace(0,4*pi,1000);» plot(x,sin(x));

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Plotting Curves - Contd....

Can change the line color, marker style, and line style byadding a string argument

» plot(x,y,’k.-’);⇒ k indicates color, . indicates point marker, -indicates line style

Try→ » help plot⇒ practice the various optionsA second plot command normally overwrites the firstTo draw 2 curves in the same graph the hold on commandis used

» hold on;

To plot on a new figure» figure;» plot(x,y);

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

to add axis labels

» ylabel(’Number of vehicles’)

» xlabel(’Time’)

to add a title

» title(’Vehicle’)

to specify the axis limits

» axis([0,20,0,6])

to add a text at co-ordinate (10,1)

» text(10,1,’ Hi welcome’)Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Plotting Curves - Contd....

All the above can be done in the figure GUI itselfGUI can be used to save plots in a variety of file formats:.eps. .pdf, .jpg etc.*.eps is the preferred way to put graphics into TEX andLATEX documentsIt is also editable and savable in usual drawing programs,especially linux basedLATEX commands can be inserted in plots

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab

MATLAB - DefinitionThe MATLAB System

MATLAB Development EnvironmentMATLAB Programming

Thank you

Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. KannurAn Introduction to Matlab