15
MATLAB (MATrix LABoratory) In numerical computations, especially those deals with vectors and matrices, is used extensively and flawlessly in terms of ease of use, availability of built-in functions, ease of programming, and speed.

MATLAB

Embed Size (px)

DESCRIPTION

matlab tutor

Citation preview

MATLAB (MATrix LABoratory)

In numerical computations, especially those deals with vectors and matrices, is used extensively and

flawlessly in terms of ease of use, availability of built-in functions, ease of programming, and speed.

IMPOTANT MATLAB DESKTOP FEATURES:

1. Command Window

Write all sort of commands here which requires user interaction.

2. Command History

See the history of all commands you have entered earlier.

3. M File Editor Window

If You want to run set of commands sequentially then use it. Go to file-> new -> mfile

4. Figure Window

It show you the output window i.e graph.

Things to be remembered:

1. MATLAB is case sensitivefor variable names and built-in functions

2. Do not start a matlab mfile name with numerical value.

3. MATLAB file must not contain any blank space or special characters

4. MATLAB file name must not be same with any inbuilt function or keywords.

5. Lines terminated by semicolon(;) means next lines begins just like c programming. However,

MATLAB will not print out the result in console i.e. Command window if the line ends in a semi-

colon other than display() command.

Some typical matlab command:

%

is used to comment a line

^

Is used to denote power. A^2 means A*A.

exp()

is used to generate exponential signal. exp(2) means e*e

Clc

Clear the command window

clear all

reset all the variables

close all

closes all the variable instances and graph window.

zeros(m,n) and ones(p,q)

zeros(m,n) creates a matrix of m rows and n columns having all element as 0

ones(p,q) creates a matrix of p rows and q columns having all element as 1

figure()

It is used to create blank graph window.

simplify(x)

if variable x has a value with several operations this command simplifies it to the nearest simple

value.

pretty(x)

if variable x has a value having numerator and denominator then this command is used to show

it in �

� form.

How to define array / Matrix / range of numbers in MATLAB?

>> x=[initial : incr/decr : final]

>> x=[1:3:11]

x = 1 4 7 10

Get length of the array

>> l=length(x)

l=4

Access any position of array/Matrix in MATLAB

>> val_at_3=x(3)

val_at_3=7

Multidimentional Array/matrix

>> y=[9,8,7 ; 1,7,5]

output

y =

9 8 7

1 7 5

>> y(2,2) i.e. Accessing 2nd column of 2nd row

ans = 7

Dimention or size of a matrix

>> size(y)

ans = 2 3 i.e. 2 row having 3 column

>> x=[1:1:3]

>>size(x)

ans = 1 3 i.e. 1 row having 3 column

Another example

>> x=[11:-3:1]

x = 11 8 5 2

x(1) x(2) x(3) x(4)

>> l=length(x)

l=4

>> val_at_1=x(1)

val_at_3=11

The first position of the Array is denoted as Array_name(1). Array_name(0) is worng.

How to Multiply , divide or powering up the array elements (individually multiply/ divide)

To multiply or divide elements of two matrices, the matrix dimensions must be satisfied and then use

dot(.) operator before the multiplication(*) or division(/) sign.

Result_arr=arr1 .* arr2 multiply each element of arr1 with corresponding element of arr2.

Or

Result_arr=arr1 ./ arr2

Or

Result_arr=arr1 .^ 2 to square every element of the array arr1

if we use Result_arr=arr1^2 it will show error

Transposition of a matrix

>> arr2=transpose(arr1); That will transpose the matrix

Elements of plotting graphs

title(‘give your title of the graph’)

xlabel(‘x-axis name’)

ylabel(‘y-axis name’)

Subplot,plot,stem

1. How to plot a graph?

For continuous plot use plot(x,y) . both x and y must be of same length.

For discrete time plot use stem(x,y). both x and y must be of same length.

Otherwise dimension is not agreed etc. error will be happen.

2. How to plot more than one graph in same window?

Use command subplot(R,C,A)

That means it divides the area in a table of R number of rows and C number of columns. The

graph that one wants to print is on the cell number A. use plot or stem function as per requirement

then set title, xlabel,ylabel of the plot area.

Then again use subplot function followed by plot or stem function and then suitable

title,xlabel,ylabel.

An ‘Axis’ command that will be used frequently

>> axis([xmin, xmax ,ymin ,ymax]) - It sets the limits for the x- and y-axis of the current axes.

e.g.

>> a=[1:10]

a = 1 2 3 4 5 6 7 8 9 10

>> figure

>> subplot(1,2,1)

>> plot(a,a)

>> ylabel('yaxis 1 to 10 ')

>> xlabel('xaxis 1 to 10 ')

>> title('with out axis command')

> > % the next sub graph for plotting with axis command in the same figure window

>> subplot(1,2,2)

>> plot(a,a)

>> title('with axis command')

>> ylabel('yaxis 1 to 10 ')

>> xlabel('xaxis 1 to 5 ')

>> axis([1, 5, 1, 10])

See, both the axis range is from 1 to 10 in 1st figure while in 2nd figure the graph is shown only for

those value having x axis range from 1 to 5 and y axis range from 1 to 10.

How to save a plot in bitmap or jpeg format

in the figure window go to

1. file -> save as

2. in the dialog box change save type as *.jpg or all file type( i.e. *.* )

3. then give file name with .jpg or .bmp or .jpeg extension

Hold on , hold off

>> hold on command is used to superimpose more than one plots

>> hold off command is just opposite of hold on

ezplot() [Easy to use function plotter]

It plots the function f(x) in a default domain of –2*pi<x<2*pi where pi=π.

e.g.

>> ezplot('u^2 - v^4')

Output

Check the x axis range from somewhere -6.28 to 6.28

Variations of explot()

ezplot('f(x)’,[xmin,xmax,ymin,ymax])

e.g.

>> ezplot('u^2 - v^4 ',[-3,2,-2,3])

Output

Check the x axis range from -3 to 2 and y axis range in -2 to 3

zplane()

This function is used to pole zero plot.

Syntax

zplane(z,p) where z=[array of point of zeros in transfer function] and p=[array point

of pole in transfer function]

pzmap()

This function is used for Pole-zero plot of dynamic system.

Syntax

pzmap(h) where h is a transfer function

tf()

This function is used for create transfer function from two arrays (one representing coefficients

of numerator and other the coefficients of denominator)

Comparative use of zplane(),pamap() with tf()

>> num=[2,-5,1];

>> den=[1 2 -3];

>> h=tf(num,den)

So, output at this stage becomes the transfer function

h =

2 s^2 - 5 s + 1

---------------

s^2 + 2 s – 3

now use following codes to get plots using pzmap(fx) and zplane(n,d)

>> pzmap(h)

>> figure

>> zplane(num,den)

The plots become

abs()

It finds out the magnitude of an element.

angle()

It finds out the phase of a component given in complex number (represented by j or i

Like A+iB or A+jB) form.

if statement and its variations in MATLAB

if expression statements elseif expression statements else statements end e.g. >> x=rem(4,2) % get remainder if statement >> if x==1 display('odd') else display('even') end; output even

For loop in MATLAB

>> for m = 1:3

for n = 1:2

display(n);

end;

end ;

output

n = 1

n = 2

n = 1

n = 2

n = 1

n = 2

How to create a User define function in MATLAB?

1. Write a Function 2. Save the File (The file must be of same name that of function to eliminate ambiguity.) 3. Call the Function

Write a Function

Open M file in a text editor. Within the file, declare the function and add program statements:

function f = add(a,b) f = a+b;

save as add.m . The file must be in same directory which is under use.

call the function by the name part of M file. So, use same name for the function and M- file.

>>x = 1;

>> y=2;

>>op= add (x,y)

op = 3

a,b are function’s local input argument and their instances are x,y outside the function

body.

output is stored in variable f inside the function while op is used to hold instant value .

And “function” is the keyword to identify a function.

For multiple output use array/matrix

e.g.

calc.m function [sum,diff,mul,div]= calc(a,b) sum=a+b; diff=abs(a-b); mul=a*b; div=a/b;

Call the function from command window >> X=4; >> Y=2; >> [sum,diff,mul,div]= calc(x,y) The output will be sum = 6 diff = 2 mul = 8 div = 2