You are on page 1of 43

MATLAB - Denition 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. Govt. College of Engg. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Outline

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Denition MATLAB R is a high-performance language for technical computing. It integrates computation, visualization and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include Math and computation, Algorithm development, Data acquisition, Modeling, simulation, prototyping, Data analysis, exploration, visualization, Scientic and engineering graphics, Application development including graphical user interface building -The MathWorks, Inc.

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB
MATRIX LABORATORY Everything in MATLAB is a matrix The basic data element in MATLAB is an array that does not require dimensioning. An interactive software package/language designed for scientic and engineering computations - Making it Simple!!!!! An interpreted language - Commands executed line by line Precise numerical calculations and graphics illustrations are possible

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB............ ?
MATLAB commands are expressed in a notation close to that used in Mathematics and Engineering
Eg: sin,log,abs ......

Problem solving in MATLAB is generally much faster than traditional programming


Eg: inv

Single MATLAB command to nd the inverse of a matrix Because of natural notation, MATLAB code is clear and self documented MATLAB Software is written in Optimized C and assembly language- to obtain high performance
Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB............ ?
MATLAB commands are MATLAB functions generally stored as an M le (File stored with .m as extension)
Eg: sin.m , plot.m etc...

The built in M les are open. Modications are possible. One can create user dened M les /functions for solving certain problems
sin.m is a built-in function to calculate sin of an angle But no built in function is available to nd sine square of an angle User can create a function named sin2 (le sin2.m) to do the same

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB............ ?
There is nothing one can do in Fortran or C that can not be done in MATLAB (and in a much easier fashion) MATLAB has a rich set of Toolboxes/application-specic solutions to do specialized work Toolboxes are comprehensive collections of MATLAB functions (M-les) that extend the MATLAB environment to learn and apply specialized technology
Simulink Electrical - Control Systems, Signal processing, Power systems Mathematics - PDE , Fuzzy logic , Optimization, Wavelet etc..

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

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

MATLAB has a good demo program for beginners All the help, manuals etc. are freely available at http://www.mathworks.com MATLAB is costly ??? Scilab by INRIA is an Alternative. It is Open Source and free

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB System
Five main parts Development Environment - Set of tools and facilities that helps to use MATLAB functions and les - GUIs The MATLAB Mathematical Function Library - a vast collection of computational algorithms sum, sine, cosine, matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms The MATLAB Language - high-level matrix/array language with control ow 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. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB System

Graphics - functions for displaying vectors and matrices as graphs, two and three-dimensional data visualization, image processing, animation, presentation graphics and building complete GUIs The MATLAB Application Program Interface (API) - library that allows to write C and Fortran programs that interact with MATLAB facilities for calling routines from MATLAB (dynamic linking), calling MATLAB as a computational engine

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB Environment Windows


MATLAB Desktop Place one reach on starting MATLAB. Contains GUI tools for managing les, variables, and applications such as Command window, current Directory, command history etc...

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB Desktop Tools


Command Window to enter variables and run functions and M-les

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB 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. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB Desktop Tools


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

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB Desktop Tools


Current Directory Browser to view the content of the current directory

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB Desktop Tools


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

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB Desktop Tools


Help Browser A Web browser integrated into the MATLAB desktop that displays HTML documents. To open the Help browser, click the help button in the toolbar, or type helpbrowser in the Command Window

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB Desktop Tools


Array Editor to view and edit an array. Double-click a variable in the Workspace browser to see it in the Array Editor

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB Desktop Tools


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

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

MATLAB Desktop Tools


Proler GUI to measure the computation time of MATLAB command to improve the performance of programs. To open Type prole viewer in CW

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Matrix Operations

Entering a Matrix - Several ways


Enter an explicit list of elements Load matrices from external data les Generate matrices using built-in functions Create matrices with your own functions in M-les

Basic Conventions
Separate the elements of a row with blanks or commas Use 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. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Matrix Operations...Contd
A= [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] 16 3 2 13 5 10 11 8 A= 9 6 7 12 4 15 14 1 To suppress output, end the line with a semicolon a=3 a=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 specied, the default name is ans
Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Matrix Operations.- Contd..


A1 = [1 2 3;2 1 3;3 1 2] 1 2 3 A1 = 2 1 3 3 1 2 A2 = [A 3*A;A-(2*A) A+A] 1 2 3 3 2 1 3 6 3 1 2 9 A2 = 1 2 3 2 2 1 3 4 3 1 2 6 6 3 3 4 2 2 9 9 6 6 6 4

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Matrix Operations.- Contd..

sum(A) - gives the sum of column elements To nd the sum of row elements transpose the matrix and take the sum A gives the transpose of A sum(A) diag(A) - gives the diagonal elements sum(diag(A))- gives the sum of diagonal elements The 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. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Matrix Operations.- Contd..

eig(A) gives the eigen values of A inv(A) gives the inverse of A det(A) gives the determinant of A poly(A) gives the coefcients of the characteristic polynomial A(:,2) gives the elements of the 2nd column A(2,:)=[3 6 4 3]; replaces second row of A

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Some General Commands


A matrix entered is automatically remembered in the MATLAB Workspace who - MATLAB replies with the variables in your Workspace whos - MATLAB gives more details on Workspace variables what -MATLAB replies with the current directory and MATLAB les in the directory save le-name variable list Eg: save my-variables.dat A1, A2, B clc - Clears the cluttered Command Window
Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Some General Commands - Contd...

clear A1 - Removes the variable A1 from Workspace clear all - Deletes everything from Workspace load le-name - Retrieves the variables saved in the le named le-name Eg: load my-variables.dat Can do the same for entire environment. Commands are save myenv; clear all; load myenv;

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Some General Commands - Contd...


help - The most important function for self learning MATLAB help matfun - all the basic matrix functions help datafun - all the basic data analysis functions help graph2d - all the basic 2-dimensional plotting functions help stats - all the functions in the MATLAB Statistics Toolbox help optim - all the functions in the MATLAB Optimization Toolbox

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Scalars and Vectors

A variable is a 1 X 1 array A linear array is a vector Most variables will be arrays or matrices of doubles (64-bit default)) or 16-bit char Other types are also supported: complex, symbolic, 16-bit and 8 bit integers, etc. Variable Names
First character must be a LETTER after that, any combination of letters, numbers and _ CASE SENSITIVE!

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Scalars and Vectors

Built-in variables
i and j can be used to indicate complex numbers pi has the value 3.1415926... Inf and -Inf are positive and negative innity NaN represents Not a Number

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Characters and Strings

a=s; denes a character variable a with value s Welcome 2012 display the string with the string stored as ans To remove "ans =", disp() function can be used disp(Welcome 2012) MATLAB function sprintf() allows to mix strings with variables 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. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB 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. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Built in Functions

MATLAB has an enormous library of built-in functions Call 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), oor(3.3), ceil(4.23) angle(i); abs(1+i);

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB 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. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Functions- An Example

Verify that e(ix) = cos(x) + i sin(x) for a few values of x


x = pi/4; a = exp(i x) b = cos(x) + i sin(x) ab

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Arrays - More Operators

Element-wise operators Operators * / have element-wise operation To 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. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB 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. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB 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), 5 values

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


b=0:2:10 starts at 0, increments by 2, and ends at or before 10 increment can be decimal or negative c=1:5 if increment is not specied, default is 1

To initialize logarithmically spaced values logspace command is used

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB 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 more points


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

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Plotting Curves - Contd....


Can change the line color, marker style, and line style by adding a string argument
plot(x,y,k.-); k indicates color, . indicates point marker, indicates line style

Try help plot practice the various options A second plot command normally overwrites the rst To draw 2 curves in the same graph the hold on command is used
hold on;

To plot on a new gure


gure; plot(x,y);
Dr. C. SREEKUMAR Associate Professor Dept. of Electrical & Electronics Engg. Govt. College of Engg. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB 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. Kannur An Introduction to Matlab

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Plotting Curves - Contd....

All the above can be done in the gure GUI itself GUI can be used to save plots in a variety of le formats: .eps. .pdf, .jpg etc. *.eps is the preferred way to put graphics into TEX and LATEX documents It is also editable and savable in usual drawing programs, especially linux based LATEX commands can be inserted in plots

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

MATLAB - Denition The MATLAB System MATLAB Development Environment MATLAB Programming

Thank you

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

You might also like