Sunteți pe pagina 1din 25

Introduction to MATLAB

Lecture-1

Fundamentals
MATLAB stands for Matrix Laboratory, a tool for numeric
and Scientific computation.
Various toolboxes-> Control Systems, Signal Processing,
Image Processing & System Identification.

Active Folder
generally WORK

MATLAB Window

Command Window ( Can


be treated as a simple
Calculator)
Command History

Basic Operations
When invoked, command window will display
prompt >>. So now ready for entering data and
executing commands.
To quit exit or quit. Help can be sought using
help or lookfor command.
? Find out the difference between Scalar & Vector
in MATLAB.
? What happens if a MATLAB statement ends with
a Semicolon.

Rules for Variable Declaration


Can be upto 63 characters long in
MATLAB 6.5
Can contain letter, digits and underscore
character.
Must begin with a letter.
MATLAB is case-sensitive so a and A
are not the same.
Avoid using names of built in functions like
pi, sqrt, etc

Predefined Values
Function

Description

pi

(3.14)

i,j

Represents the value I(-1)

Inf

Infinity, Result of Division by Zero

Nan

Stands for Not a Number, result of Undefined Mathematical operation


like Division of Zero by Zero

clock

Represents the date and time in a six element row vector containing
the year, month, day, hour, minute & second

date

Represents the date in Character String format as 15-Dec-2003

eps

The short name for epsilon, it represents the floating point precision
for the Computer being used, it is the smallest difference between 2
nos. that can be represented on the computer

ans

A special variable used to store the results of an expression if that is


not explicitly assigned to some other variable

Basic Commands
Command

Description

Comments

demo

Access on line demo page

length

Length of a matrix

Clear

Clears variables or functions from


workspace

clc

Clears command window during work


session

clg

Clears graphic window

diary

Saves a session in a disk, possibly for


printing at a later date

close

Closes all active windows except the one


which is working

Common MATLAB functions


Function

Description

abs(x)

Absolute value of x

acos(x)

Cos-1x, in radians

asin(x)

Sin-1x, in radians

atan(x)

Tan-1x, in radians

atan2(x,y)

Obtains tan-1(y/x) overall 4 quadrants of a


circle in radian

cos(x)

Cosine value with x in radians

exp(x)

ex

log(x)

Log ex, natural logarithm

log10(x)

Log10x, logarithm base 10.

Some Utility Matrices


Functions

Description

diag(a)

Products a vector constituting of diagonal


of a square matrix a.

eye(n)

Generates an n by n identity matrix

eye(n,m)

Generates an n by m identity matrix

ones(n)

Produces an n by n matrix all elements


unity

ones(n,m)

Produces an n by m matrix with all


elements unity

zeros(n)

Generates n by n matrix of zeros

zeros(n,m)

Generates n by m matrix of zeros

Assignment-1(Command Line)
1.

A trigonometric identity is given by


cos2(x/2)=(tanx+sinx)/2tanx
Verify that the identity is correct by calculating each
side of the equation, substituting x=/5.

2.

Input a 34 matrix. Find out its size & transpose.

3.

Input 2 Two Dimensional Matrices and perform the


following operations:a.
Addition
b.
Subtraction

Division Operator
Left Division Operator
a\b== b/a==inv(a)*b [MATLAB]
Right Division Operator
a/b== a/b==a*inv(b) [MATLAB]
Decision depends on Programmer. Care must
be taken that a and b are matrices.

Period(.)
Preceding the linear algebraic operations
of +,-,*,/ by a period (.) indicates an array
or element by element operation.
So, .*, ./, .\, .^ are element by
element multiplication, division and
exponent.

Complex Number
A no. z=2+j2 may be entered in MATLAB
as z=2+2*i or z=2+2*j
In general, while entering nos. like y=3+4j,
no spaces should be given in between
otherwise it will be considered as 2
separate numbers.
Ex- w=[1+j 3-2*j; 3+2*j 4+3*j]

Assignment-2
1. For a two dimensional matrix, demonstrate the
operations of Matrix addition, Subtraction,
Multiplication, Division. Also find out square,
cube, square root and cube root of each
element of both the matrices.
2. Convert the following expression in MATLAB
equivalent
za=22exp[(/4)j].
3. Calculate the magnitude and Phase (in
degrees) of Input Impedance
Z=((5+6j)(4-j8)/(9-2j))+4/300

Colon Symbol (:)


It has three important functions:1. To create vectors and matrices.
2. To specify sub-matrices and vectors.
3. To perform iterations.
Ex- t1=1:6, t2=3:-0.5:1, etc.. Generates
different sets of vectors.

Generating Vectors
For generating vectors we use 2 commands:1. Linspace- generates linearly evenly spaced
vectors.
2. Logspace- generates a Logarithmically
evenly spaced vectors.
Syntax- linspace(i_value,f_value,np)
logspace(i_value,f_value,np)
Where
i_value= initial value
f_value=final value
np=total no. of elements in vector.

M Files
Command Line Prompt allows execution
of one instruction at a time.
MATLAB files with .m extension are
called M Files. These are text editor to
write programs.
There are two types of M Files:a. Script Files
b. Function Files

Script Files
It is especially useful for analysis and
design problems that require long
sequences of MATLAB commands.
Statements in a script file operate globally
on the workspace data.

Function Files
These are M Files equivalent to the User
Defined Functions used in the C/C++
Language style.
Scope of a variable declared inside a
function is Local. However, arguments
may be into or out of a Function M file.
Syntax-function variable(parameters)=
function_name (arguments)

Assignment-3
Implement using M files and Function files
the following operation: Input- a=10, b=20
Output- Add, Sub., Mul., Division( both types) and
ab.

Solution using M file

clc;
close all;
clear all;
echo on;
a=10;
b=20;
c=a+b;
d=a-b;
e=a*b;
f=a/b;

g=a\b;
h=a^b;
disp(a);
disp(b);
disp(c);
disp(d);
disp(e);
disp(f);
disp(g);
disp(h);

Solution using Function File


function [c]=demo_func(a,b);
e=a+b;
f=a-b;
g=a*b;
h=a/b;
i=a\b;
j=a^b;
disp(e);
disp(f);
disp(g);
disp(h);
disp(i);
disp(j);
To call the function from Command Line type
>> demo_func(10,20)

Assignment-4
Find out the role of functions namely- ceil, exp,
fix, floor, log10, rem, round, sqrt, tan.
Clearly Explain the difference between Vectors
and Scalars in MATLAB.
Suppose you are given a set of Five resistances
namely
R1=1, R2=2 , R3=3 , R4=4 , R5=5 .
Write a Program to calculate the Equivalent
Resistances when they are connected in Series
and in Parallel.

Assign-4(Ctd.)
For the Following figure, find out the
Equivalent Resistance across Point A and
B.
4

2
5
4
2
A

S-ar putea să vă placă și