Vous êtes sur la page 1sur 24

How to use this book?

This book enables you to learn basic MATLAB which is mandatory before learning any toolbox of MATLAB.
This book contains complete notes on following topics:

MATLAB Arrays

Chapter on Arrays covers how to enter one-dimensional and two-dimensional array, mathematical operations
with array and built in functions for handling arrays.

MATLAB Functions

Chapter on functions covers in-built functions of MATLAB and how to solve linear equations in MATLAB. It also
covers how to create and use a function file.

MATLAB Script file

Chapter on script file covers how to create a script file and commands used in script file.

MATLAB Loops

Chapter on loops clearly elucidates for-end and while-end loops. A student completely naïve to programming
can easily understand concept of loops which is helpful in learning any programming language. A complete
explanation on nested loop has been given along with programs which will help in developing algorithm making
skill.

MATLAB Two dimensional and Three-dimensional plots

Professionals use MATLAB across the world. We can draw two dimensional and three-dimensional plots with
just two or three lines of code. Scientists, Engineers and Economist widely use MATLAB software for drawing
two dimensional and three-dimensional graph. Our chapter on two dimensional and three-dimensional plots
teaches how to draw graph using inbuilt functions and how to format a plot using commands. This chapter
covers different commands used for three-dimensional plotting and how to observe extreme points using inbuilt
functions.

MATLAB application in calculus

We can solve complicated questions of integral and differential calculus very easily using inbuilt functions. This
chapter teaches how to use inbuilt functions to solve complicated questions.

MATLAB Solution of differential equation using MATLAB

We can solve complicated differential equations up to nth order in Simulink very easily. Using function file, we
can solve first order differential equations very easily. This chapter covers how to solve linear first order
differential equation using function file.

In Conclusion, content of this book can be easily understood by the user. Programs are also given along with
theory to develop algorithm making skill. We have covered lucid and complete notes on basic MATLAB.
Complete notes on Simulink, Image processing, Simpower system toolbox and fuzzy logic toolbox will be
added very soon. Wherever and whenever you require any information about MATLAB you are just a click
away............
About us

Our mission is to be innovative and meticulous to create products that give students everywhere access to
enjoyable, free and effective study material. We prepare students for MS right from free GRE coaching on our
website www.onlinegrecoaching to improving SOPs. It is mandatory to learn MATLAB before going for MS.
Projects on MATLAB also helps in improving SOPs. Learn basic MATLAB from this app and wishing you a
successful MS program.

All the best............

Content

Chapter 1………………………………………………………………………………………………………………………………4

Chapter 2……………………………………………………………………………………………………………………………. 2

Chapter 3………………………………………………………………………………………………………………………………54

Chapter 4………………………………………………………………………………………………………………………………78

Chapter 5………………………………………………………………………………………………………………………………99

Chapter 6………………………………………………………………………………………………………………………………126

Chapter 7………………………………………………………………………………………………………………………………151

Chapter 8………………………………………………………………………………………………………………………………178

Chapter 9………………………………………………………………………………………………………………………………209

Chapter 10………………………………………………………………………………………………………………………………233
Chapter-1
Interacting with MATLAB
MATLAB can be used as a programming language and as a simulation software developed by
mathworks. Our tutorial gives you aggressively a gentle introduction of MATLAB programming language
and simulink. It is assumed that the MATLAB software is installed on the computer and that the user can
start the program. Once the program starts, the window that opens contains three smaller windows
command window, workspace window and command history window. These are three of different
windows in MATLAB.

MATLAB WINDOWS
1 COMMAND WINDOW
2 EDITOR WINDOW
3 COMMAND HISTORY WINDOW
4 WORKSPACE WINDOW
5 CURRENT DIRECTORY WINDOW

COMMAND WINDOW – command window is MATLAB’s main window and opens when MATLAB is
started. It enters variables and runs programs.

Fig 1.1: Example of command window

EDITOR WINDOW-The Editor window is used for writing MATLAB programs. This window is opened
from the file menu in the command window. More details on editor window are given in chapter-3
where it is used to create a script file and in chapter-4 where it is used to create function file.
Fig 1.2 shows example of Editor window

WORKSPACE WINDOW-It provides information about the variables that are declared by user.

Fig 1.3: Example of workspace window


COMMAND HISTORY WINDOW-It contain list of commands that are entered in command window.

Fig 1.4: example of command history window

Current directory window or current folder- It shows list of files in current folder.

Fig 1.5: example of command history window


WORKING IN COMMAND WINDOW
• command window can be used for executing commands, opening other windows, running
programs written by the user.
• To type a command in command window the cursor must be placed next to the command
prompt (>>).
• Once a command is typed and the Enter key is pressed, the command will get executed.
• Any output that the command generates is displayed in the command window. If a semicolon (;)
is typed at the end of the command the output of the command is not displayed.

AIRTHMETIC OPERATIONS WITH SCALARS


OPERATION SYMBOL EXAMPLE
1 Addition + 5+3=8

2 Subtraction - 5-3=2

3 Multiplication * 5*3 = 15

4 Right division / 5/3 = 1.6667

5 Left division \ 3\5 = 1.6667

6 Exponentiation ^ 5^3 = 125

EXAMPLE OF USING MATLAB AS A CALCULATOR

>> 7 + 8 [TYPE AND ENTER]


ans =
15
>>27^(1/3) + (64)^(1/2)
ans =
11
LIST OF COMMONLY USED MATLAB MATHEMATICAL
BUILT-IN FUNCTIONS
FUNCTION DESCRIPTION EXAMPLE

sqrt(x) Sqrt root >>sqrt(81)


ans =
9
exp(x) Exponential >>exp(5)
ans =
148.41
abs(x) Absolute value of complex or real number >>abs(2 + 3i)
ans =
3.6056
log(x) Natural Algorithm >>log(1000)
ans =
6.907
log10(x) Base 10 Algorithm >>log10(1000)
ans =
3
factorial(x) Factorial function x! >>factorial(5)
ans =
120
rem(x,y) Returns remainder when x is divided by y >>rem(90,10)
ans =
0

TRIGONOMETRIC MATH FUNCTIONS

FUNCTION DESCRIPTION
sin(x) Sine of angle x(x in radians)

cos(x) Cosine of angle x(x in radians)

tan(x) Tangent of angle x(x in radians)

cot(x) Cotangent of angle x(x in radians)


• Similarly pre-defined inverse trigonometric functions are asin(x), acos(x), atan(x), acot(x).
• Hyperbolic trigonometric functions are sinh(x), cosh(x), tanh(x), coth(x).

ROUNDING FUNCTIONS

FUNCTION DESCRIPTION
round(x) Round to the nearest integer

fix(x) Round towards zero

ceil(x) Round towards infinity

floor(x) Round towards minus infinity

sign(x) Returns 1 if x>0,-1 if x<0 and 0 if x=0

PREDEFINED VARIABLE
Pi – The number pi

eps – The smallest difference between two numbers equal to 2^(-52) = 2.2204e-16

inf – used for infinity

NaN – stands for not a number

Other useful commands in MATLAB


clear all – removes all variables from the memory

clc – command clears the command window.

clear x y z – removes variables only x, y and z from memory


who – display a list of variables currently in memory

whos – display a list of variables currently in their memory and their size
Chapter-2
CREATING ARRAYS AND MATHEMATICAL OPERATIONS WITH ARRAY

ONE DIMENSIONAL ARRAY:


Variable_name = [type vector elements or array elements]

ROW VECTOR :- row vectors are created by enclosing the set of elements in square bracket
and putting space or comma between the elements.
For example :
>>r = [7 8 9 10 11];

COLUMN VECTOR :– These are created by enclosing the set of elements in square bracket
and putting semicolon between the elements.
For example :
>>r = [7;8;9;10;11]

REFERENCING THE ELEMENTS OF VECTOR :-


a(i) – refer to the ith element of a vector.
a(:) – refer to the all elements of a vector.
a(i:j)- refer to the elements from i to j of vector a.
For example :
>> a = [1 2 3 4 5 6]; %refers to all elements of vector a
>>a(:)
ans = 1
2
3
4
5
6
>>a(2) % refers to the second element of vector a
ans =
2
>>a(1:4)
ans =
1 2 3 4
>>a = [1;2;3;4;5;6];%column vector
>>a
a= 1
2
3
4
5
6
>>a(2:3)%refers to second and third element of vector a
ans =
2
3

CREATING A VECTOR WITH CONSTANT SPACING BY SPECIFYING THE FIRST


TERM, LAST TERM AND SPACING:
Syntax for creating a vector with constant spacing by specifying the first term, last term and
spacing:
Variable_name = [m:p:n]

[Vector in which first term is m, spacing is p and last term is n]

CREATING A VECTOR WITH CONSTANT SPACING BY SPECIFYING THE FIRST


TERM, LAST TERM AND SPACING:-
Vector in which first element is x, last element is y and number of elements is n is created by
using linspace command. Syntax for using linspace command:-
Variable_name = linspace(x,y,n)
>>X = [1:3:10]%first element is 1,spacing is 3 and last element is 10
X =
1 4 7 10
>>X = [3:7]%first element is 3,last element is 7 and default value of spacing is 1
X =
3 4 5 6 7
>>a = linspace(10,20,5)%first element is 10,last element is 20 and number of elements = 5
a =
10 12.5 15 17.5 20

CREATING A TWO DIMENSIONAL ARRAY(MATRIX):-


Syntax for entering matrix
Variable_name = [first row elements; second row elements;--------------------; last row
elements]

• All the rows must have same number of elements.


>>a = [5 10 15;1 2 3;4 5 6]%semicolon is typed before a new line is started
A = 5 10 15
1 2 3
4 5 6
>>b = [7 2 9 % instead of semicolon, Enter key can also be pressed before a new line is started
1 2 3
4 5 6]
B =
7 2 9
1 2 3
4 5 6

THE ZEROS, ONES AND EYE COMMAND:-


zeros(m,n) – It creates a matrix with m rows and n columns in which all elements are zero.

ones(m,n)-It creates a matrix with m rows and n columns in which all elements are 1.

eye(n) -It creates a square matrix with n rows and n columns in which diagonal elements are 1 and non
diagonal elements are 0.

REFERENCING THE ELEMENTS OF MATRIX:-


A(m,n)-refers to the element in row m and column n of matrix A.
A(:,n)-refers to the elements in all the rows of column n of matrix A.
A(:,m:n)-refers to the elements in all the rows between column m and n of matrix A.
A(m:n,:)-refers to the elements in all the columns between row m and n of matrix A.
A(m:n,p:q)-refers to the elements in rows m through n and column p through q of matrix A.
>>A = [1 2 3;4 5 6;7 8 9];
>>A(1,2)
ans =
2
>>A(:,1)
ans =
1
4
7
>>A(:,1:2)
ans =
1 2
4 5
7 8
>>A(1:2,:)
ans =
1 2 3
4 5 6
>>A(1:2,2:3)
Ans =
2 3
5 6

DELETING ELEMENTS- An element, or a range of elements, of an existing variable can be


deleted by reassigning nothing to these elements. For example :-
For deleting 6th element of a vector, command is
A(6) = []
>> A = [1 2 3 4 5 6 7 8 9 10];
>> A(6) = []%Delete 6th element of vector
A=
1 2 3 4 5 7 8 9 10
>> A(3:5) = []%Delete 3rd,4th and 5th element of vector
A=
1 2 7 8 9 10
>> B = [1 2 3;4 5 6;7 8 9];
>> B(:,2:3) = []%Delete elements of 2nd and 3rd column
B=

1
4
7

BUILT – IN FUNCTIONS FOR HANDLING ARRAYS:-


1 length (A)
• Returns the number of elements in the vector A.
For example:-

>> A = [5 9 2 4];
>> length(A)%built in function for finding length of array

ans =

2 size (A)

• Returns a row vector containing two elements one indicating number of rows and
another indicating number of column in matrix A.

>> A = [1 2 3;4 5 6;7 8 9];


>> size(A)

ans =
3 3%it means that no. of rows = no. of column = 3

3 reshape(A,m,n)

• Rearrange a matrix A that has r rows and s columns to have m rows and n columns. r
times s must be equal to m times n.

>> A = [5 1 6;8 0 2];


>> B = reshape(A,3,2)

B=

5 0
8 6
1 2
4 diag(A)
• when A is a vector or one dimensional array. Above function creates a square matrix
with the elements of A in the diagonal.
• When A is a matrix, above function creates a vector from diagonal elements of A.

>> A = [1 2 3];
>> B = diag(A)

B=

1 0 0
0 2 0
0 0 3

>> A = [1 2 3;4 5 6;7 8 9];


>> B = diag(A)

B=

1
5
9

MATHEMATICAL OPERATIONS WITH ARRAY:-


ADDITION AND SUBTRACTION-
The operations +(addition) and –(subtraction) can be used with arrays of identical size(the same
number of rows and column). For example:

>> A = [1 2 3;4 5 6;7 8 9];


>> B = [1 1 1;2 3 4;6 1 2];
>> A + B

ans =

2 3 4
6 8 10
13 9 11

>> A - B

ans =

0 1 2
2 2 2
1 7 7

MULTIPLICATION-
• Multiplication operation * is executed by MATLAB according to the rules of linear
algebra.
• If A and B are two matrices, the operation A*B can be carried out if the number of
column in matrix A is equal to number of rows in matrix B.
• Two vectors can multiply each other only if both have the same number of elements
and one is a row vector and other is column vector.
>> A = [1 2 3;4 5 6;7 8 9];
>> B = [1 2;3 4;5 6];
>> A*B %for multiplication number of columns in A should be equal to the number of rows in
B
ans =
22 28
49 64
76 100
>> c = [1 2 3];%multiplication of row vector with a column vector gives n*n matrix
>> d = [1;2;3];
>> c*d
ans =
14
>> e = [1;2;3];%multiplication of column vector with a row vector gives n*n matrix
>> f = [1 2 3];
>> e*f
ans =
1 2 3
2 4 6
3 6 9
IN- BUILT FUNCTIONS IN MATLAB
FUNCTION DESCRIPTION EXAMPLE
mean(A) • If A is a vector, returns >> A = [1 2 3;4 5 6;7 8 9];
the mean value of >> mean(A)
elements. ans =
• If A is a matrix, returns 4 5 6
the mean value of >> B = [5 9 2 4];
elements of particular >> mean(B)
column. ans =
5
max(A) • If A is a vector, returns >> A = [1 2 3;4 5 6;7 8 9];
largest element in A. >> max(A)
• If A is a matrix, returns a ans =
row vector containing 7 8 9
largest element of each >> B = [5 9 2 3 12];
column of A. >> max(B)
ans =
12
min(A) • If A is a vector, returns >> A = [1 2 3;4 5 6;7 8 9];
smallest element in A. >> min(A)
• If A is a matrix, returns ans =
row vector containing 1 2 3
smallest element of >> B = [5 9 2 3 12];
each column of A. >> min(B)
ans =
3

[d,n] = max(A) • If A is a vector, d is the >> A = [4 9 14 1 3];
largest element in A and >> [d,n] = max(A)
n is the position of d=
element(first if several 14
has same value) n=
• If A is a matrix, d is the 3
vector containing >> A = [1 2 3;4 5 6;7 8 9];
largest element of each >> [d,n] = max(A)
column of A and n is the
d=
row in which largest
7 8 9
element is present.
n=
3 3 3
sum(A) • If A is a vector, returns >> A = [1 2 3 4];
the sum of elements of >> b = sum(A)
vector. b=
• If A is a matrix, returns 10
the sum of elements of >> A = [1 2 3;4 5 6;7 8 9];
each column of A. >> b = sum(A)
b=
12 15 18

sort(A) • If A is a vector, arranges >> A = [2 1 6;1 0 9;17 1 8];


the elements of vector in >> sort(A)
ascending order. ans =
• If A is a matrix, arranges 1 0 6
the elements of column 2 1 8
of matrix in ascending 17 1 9
order. >> A = [5 9 2 4];
>> sort(A)
ans =
2 4 5 9
det(A) • Returns the determinant >> a = [2 4;3 5];
of square matrix A. >> det(a)
ans =
-2.0000
dot(a,b) • Calculates the scalar dot >> a = [1 2 3];
product of two vectors a >> b = [4 5 6];
and b. >> dot(a,b)
ans =
32
rank(A) • Calculates the rank of >> a = [1 2 3;2 4 6;7 3 9];
matrix or provides the >> rank(a)
number of non-zero rows ans =
in reduced row echelon 2
form.

rref(A) • Computes reduced row >> a = [1 2 3;2 4 6;7 3 9];


echelon form. >> rref(a)
ans =
1.0000 0 0.8182
0 1.0000 1.0909
0 0 0
cross(a,b) • Computes cross product >> a = [1 3 2];
of two vectors. The two >> b = [2 4 1];
vectors must have three >> cross(a,b)
elements ans =
-5 3 -2
inv(A) • returns the inverse of >> a = [2 1 4;4 1 8;2 1 -3];
square matrix A. >> inv(a)
ans =
-0.7857 0.5000 0.2857
2.0000 -1.0000 0
0.1429 0 -0.1429

A’ • returns the transpose of >> a = [2 1 4;4 1 8;2 1 -3];


matrix A. >> a'
ans =
2 4 2
1 1 1
4 8 -3

METHOD FOR SOLVING PAIR OF LINEAR EQUATIONS IN MATLAB:-


Lets take example of following linear equations
x + 2y + 3z = 6
6x - 7y + 8z = 7
x + 3y + 5z = 6
1 2 3 6
A = 6 -7 8 B= 7
1 3 5 6

AX = B (multiply both sides by A^-1)

X = (A^-1)*B

In matlab, last equation can be written as

X = A\B ------------------------------------------(First method or Gauss elimination method)

or X = inv(A)*B

There are two methods for solving system of linear equations. First (left division method is
recommended, because in second method calculation of inverse may be less accurate, when large
matrices are involved)

MATLAB COMMANDS FOR SOLVING SYSTEM OF LINEAR EQUATION


>> a = [1 2 3;6 -7 8;1 3 5];
>> b = [6;7;9];
>> x = a\b
x=
1.0000
1.0000
1.0000
>> x = inv(a)*b

x=

1.0000
1.0000
1.0000

ELEMENT-BY- ELEMENT OPERATIONS


Element-by-element multiplication, division and exponentiation of two vectors or matrices can be done
in MATLAB by using following symbols.
SYMBOL DESCRIPTION
.* MULTIPLICATION

.^ EXPONENTIATION

./ RIGHT DIVISION

.\ LEFT DIVISION

For example:-

>> A = [1 2 3;2 2 2];


>> B = [1 4 2;2 1 3];
>> A.*B
ans =
1 8 6
4 2 6
>> X = [1:4];
>> y = X.^2 + 4*X
y=
5 12 21 32
>> d = [1 4 9];
>> h = sqrt(d)
h=
1 2 3
Chapter-3
SCRIPT FILES
For purchase kindle edition. Ctrl + Click here:

https://www.amazon.in/MATLAB-TUTORIAL-Er-Sumit-Arya-
ebook/dp/B078MLYZND/ref=sr_1_fkmr0_1?ie=UTF8&qid=1514375261&sr=8-1-
fkmr0&keywords=matlab+tutorial+aumit+arya

For purchase paperback edition. Ctrl + Click here:

https://www.amazon.com/dp/1976738474/ref=sr_1_2?ie=UTF8&qid=1514375505&sr=8
-2&keywords=matlab+tutorial+sumit+arya

Copy short URL

Vous aimerez peut-être aussi