Top Banner
01/12/2006 Department of Electrical & Electronics Engineering Introduction to MATLAB
34
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Introduction to MATLAB

Page 2: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Outline

• MATLAB Introduction• MATLAB elements

Types variables Matrices

• Loading ,Saving and Plotting • MATLAB Programming • Simulink

Page 3: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Welcome to MATLAB

MATLAB stands for “Matrix Laboratory”APPLICATIONS OF MATLAB:• Mathematical Calculations• Data Analysis & Visualization• Software Development • Simulation

Page 4: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Getting Started

Command-Window

Workspace & Directory

Command- History

Page 5: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

• Command Windowo type commands

• Current Directoryo View folders and m-files

• Workspaceo View program variableso Double click on a variable to see it in the Array

Editor• Command History

o view past commandso save a whole session using diary

Page 6: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Basic MATLAB Commands

clear all: clears workspace of all variablesclose all : closes all the figure windowsplot (x,y) : plots vector “y” versus “x”% : used for Commentshelp : when used with command gives its syntax

Page 7: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Basic Arithmetic Operators

+ Arithmetic addition - Arithmetic subtraction * Arithmetic multiplication / Arithmetic division ^ Exponent or power

.* (element by element for arrays)

Page 8: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Variables

• No need for types. i.e.,

• All variables are created with double precision unless specified and they are matrices.

• After these statements, the variables are 1x1 matrices with double precision

int a;double b;float c;

Example:>>x=5;>>x1=2;

Page 9: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

• a vector x = [1 2 5 1]x =1 2 5 1

• a matrix x = [1 2 3; 5 1 4; 3 2 -1]

x =1 2 35 1 43 2 -1

• transpose y = x’ y =1251

Page 10: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

• a vector x = [1 2 5 1]x =1 2 5 1

• a matrix x = [1 2 3; 5 1 4; 3 2 -1]

x =1 2 35 1 43 2 -1

• transpose y = x’ y =1251

Array, Matrix

Page 11: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

• t =1:10 t =1 2 3 4 5 6 7 8 9 10• k =2:-0.5:-1

k =2 1.5 1 0.5 0 -0.5 -1

• B = [1:4; 5:8]

x =1 2 3 45 6 7 8

Long Array, Matrix

Page 12: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Generating Vectors from Functions• zeros(M,N) MxN matrix of zeros

• ones(M,N) MxN matrix of ones

• rand(M,N) MxN matrix of uniformly distributed random

numbers on (0,1)

x = zeros(1,3)x =0 0 0

x = ones(1,3)x =1 1 1

x = rand(1,3)x =0.9501 0.2311 0.6068

Page 13: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Matrix Index

• The matrix indices begin from 1 (not 0 (as in C)) • The matrix indices must be positive integer

Given:

A(-2), A(0)Error: ??? Subscript indices must either be real positive integers or logical. A(4,2)Error: ??? Index exceeds matrix dimensions.

Page 14: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Concatenation of Matrices

• x = [1 2], y = [4 5], z=[ 0 0]

A = [ x y]

1 2 4 5

B = [x ; y]

1 24 5

C = [x y ;z] Error:??? Error using ==> vertcat CAT arguments dimensions are not consistent.

Page 15: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

First, let's create a simple vector with 9 elements called a

a = [1 2 3 4 6 4 3 4 5]

a =1 2 3 4 6 4 3 4 5

b = a + 2b =3 4 5 6 8 6 5 6 7

Notice how MATLAB requires no special handling of vector or matrix math.

Basic matrix operation

Page 16: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Creating a matrix

Given A and B:

Addition Subtraction Product Transpose

Page 17: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Operators (Element by Element)

.* element-by-element multiplication

./ element-by-element division

.^ element-by-element power

Page 18: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

The Use of “ .” and “Element Operation”A = [1 2 3; 5 1 4; 3 2 1]A = 1 2 35 1 43 2 -1

x = A(1,:)x = 1 2 3

y = A(3 ,:)y= 3 4 -1

b = x .* yB=3 8 -3

c = x . / yc= 0.33 0.5 -3

d = x .^2d= 1 4 9

K= x^2Erorr: ??? Error using ==> mpower Matrix must be square.B=x*yErorr:??? Error using ==> mtimes Inner matrix dimensions must agree.

Page 19: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

eye identity matrixzeros matrix of zerosones matrix of onesdiag create or extract diagonalstriu upper triangular part of a matrixtril lower triangular part of a matrixRand randomly generated matrixhilb Hilbert matrixmagic magic square

Creating Matrices using in-built functions

Page 20: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Scalar in-built functions

Basic Task: Plot the function sin(x) between 0≤x≤4π

>>x=linspace(0,4*pi,100);

>>y=sin(x);

>>plot(y)

Page 21: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Scalar in-built functions

Plot the function e-x/3sin(x) between 0≤x≤4π

Calculate e-x/3 of the x-array

>>y1=exp(-x/3);

Multiply the arrays y and y1

>>y2=y*y1;

Multiply the arrays y and y1 correctly>>y2=y.*y1;

>>plot(y2)

Page 22: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Matrix in-built functions

eigdetsizelengthrankFind

Eigen values and eigenvectorsdeterminantSize of an arrayLength of a vectorrank

find indices of nonzero entries

x = 2 * rand(1,5)y = x(find(x > 1))

Suppose we want vector that consists all values of x greater than 1

Page 23: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

plot (x ,y) plots vector y versus vector x

Plotting in Matlab

plot(y) plots the columns of y versus their index.

plot(x ,y,s ) Where s is a character string made from one element from any or all the following 3 columns:

b blue . point - solid

g green o circle : dotted

r red x x-mark -. dashdot c cyan + plus -- dashed

Example : plot(x, y, ‘r+:') plots a red dotted line with a plus at each data point

Page 24: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Plotting in Matlab - Examples

x = -2.9:0.2:2.9;bar(x, exp(-x.*x));

x = -2.9:0.2:2.9;plot(x, exp(-x.*x));

Page 25: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Plotting in Matlab

x = -pi:pi/10:pi;y = tan(sin(x)) - sin(tan(x));plot(x,y,'--rs','LineWidth',2,...'MarkerEdgeColor','k',...'MarkerFaceColor','g',...'MarkerSize',10)

Page 26: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

x=0:0.25:10;stairs(x, sin(x));

x = 0:0.1:4;y = sin(x.^2).*exp(-x);stem (x,y)

Plotting in Matlab - Examples

Page 27: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

More details of plot

Plot (X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by the (X,Y,S) triples,

x = -pi:pi/10:pi;y = tan(sin(x)) - sin(tan(x));y1=- sin(tan(x))plot(x,y,'--rs',x,y1,'ob:')

Page 28: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Programming in Matlab-Creating M-file

Click to create a new M-File

• Extension “.m” • A text file containing program to run

Page 29: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Flow control in Programming

• if • for • while • break • ….

Page 30: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Control structure in Programming

• If Statement Syntax

if (Condition_1)Matlab Commandselseif (Condition_2)Matlab Commandselseif (Condition_3)Matlab CommandselseMatlab Commandsend

Some Dummy Examplesif ((a>3) & (b==5))Some Matlab Commands; end-----------------------------------if (a<3)Some Matlab Commands;elseif (b~=5) Some Matlab Commands;End

Page 31: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

While Loop Syntax while (condition)Matlab Commandsend

Dummy Examplewhile ((a>3) & (b==5))Some Matlab Commands; end

Control structure

Page 32: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

• For loop syntax

for i=Index_ArrayMatlab Commandsend

Some Dummy Examplesfor i=1:100Some Matlab Commands;end--------------------------------------------for j=1:3:200Some Matlab Commands;end------------------------------------------for m=13:-0.2:-21Some Matlab Commands;end

Control structure

Page 33: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Page 34: gmrit-cse

01/12/2006 Department of Electrical & Electronics Engineering

Writing User defined functions

• Functions are m-files which can be executed by specifying some inputs and supply some desired outputs.

• The code telling the Matlab that an m-file is actually a function is

function out1=functionname(in1)function out1=functionname(in1,in2,in3)function [out1,out2]=functionname(in1,in2)

Note : You should write this command at the beginning of the m-file and you should save the m-file with a file name same as the function name