Professional Documents
Culture Documents
INDEX
2 Creating Arrays
2.1 One dimensional (Vector)
2.2 Two dimensional (Matrix)
2.3 Array addressing
2.4 Built in function
2.5 Strings & Strings as variables
2.6 Assignment-I
4 File
4.1 Script
4.2 Functions
4.3 Assignment-III
5 Plotting
5.1 2D&3D plot
5.2 Assignment-IV
6 MATLAB programming
6.1 Relational operators
6.2 Logical operator
6.3 Built in functions
6.4 Assignment V
i
MATLAB awareness program MAP
ii
MATLAB awareness program MAP
Trigonometric
iii
MATLAB awareness program MAP
Rounding function
Ans : If the user does not assign the value of an expression to a variable
MATLAB Automatically stores the result in ans.
iv
MATLAB awareness program MAP
NaN : stands for not a number (0/0) (Syntax is CAPITAL -small- CAPITAL)
Command outcome
v
MATLAB awareness program MAP
2. Creating Arrays
2.1One dimensional (Vector)
Row vector
Column Vector
A = [ 1; 2; 3; 4; 5; 6 ];
2.2 Two dimensional (Matrix) A(i, j) = ith row & jth column
A = [1 2 3; 2 3 4; 5 6 7];
1 2 3
A= 2 3 4
5 6 7
1 2 3
A= 2 3 4
5 6 7
vi
MATLAB awareness program MAP
A( m : n ,:) = Refers to the element in all the columns (:) between rows
m&n
A( m : n , p: q) = Refers to the element in rows m through n & Columns
p through q
vii
MATLAB awareness program MAP
Assignment- I
Q.1 Create a row vector that has the elements 32, 4, 81, e2.5, 63, cos
(Π/3) & 14.12
Q.2 Create a column vector that has the elements : 55, 14, ln(51),
987, 0 & 5sin(2.5 Π )
Q.4 Using Zeros and Ones commands create a 3x5 matrix in which
the 1st ,2nd & 5th columns are 0‟s and 3rd ,4th columns are 1‟s.
viii
MATLAB awareness program MAP
Addition
a11 b11 a12 b12 a13 b13
C A B a21 b21 a22 b22 a23 b23
a b a33 b33
31 31 a32 b32
Subtraction
3.2 Multiplication
The product of the multiplication of two square matrices is also square
matrix of the same size. The multiplication of matrices is not
commutative.
A*B ≠ B*A
a a b b
A 11 12 B 11 12
a21 a22 b21 b22
a b a b a11b12 a12b22
C A * B 11 11 12 21
a21b11 a22b21 a21b12 a22b22
3.3 Division
Take A & B matrix. Where B is inverse of A (A-1). Then multiply A*B
AI = IA = A & BA = AB =I
Two types of division is possible
Left division
AX = B;
A-1 AX = A-1B but A-1 AX = IX = X
-1
Hence X=A B means X = A\B
Right division
XA = B;
X A A-1 = B A-1
Hence X = B A-1 means X = B/A
ix
MATLAB awareness program MAP
Example:
a .*b = [ a1b1 a2b2 a3b3 a4b4]
x
MATLAB awareness program MAP
4.5000
Std (A) If A is vector, returns the >> A = [ 5 9 2 4];
standard deviation of the >> std(A)
7
element of the vector. ans=
2.9439
det (A) Returns the determination of a >> A = [2 4; 3 5];
square matrix A. >> det(A)
8
ans=
-2
dot (a, b) Calculates the scalar (dot) >> a=[1 2 3];
product of two vectors a and b. >>b=[3 4 5];
9 The vectors can each be row or >> dot (a,b)
column vectors. ans = 26
xi
MATLAB awareness program MAP
Some time there is need to have random numbers that are distributed in
an interval other than (0, 1) or to have numbers that are only integers.
The random numbers that are distributed in a range (a, b) can be
obtained by multiplying rand by (b-a) and adding the product to a
(b - a)*rand + a
Example:-
A vector of 10 elements with random values between (a = -5) and
(b = 10) can be created as above equation
r = 15*rand (1, 10)-5
r = -1.8 0.6973 6.7499 5.2122 1.9164 3.5174 6.9132 -4.1123
4.0430 -4.2460
Assignment- II
Q.1 The depth of well ,d,in meters can be determined from the time it
takes for a stone that is dropped into well (zero intial velocity) to
hit the bottom by d= 0.5 x g x t2, where t is the time in seconds
and g=9.81m/s2. Determine d for t= 1 to 10s.(create a vector t
and determine d using element by element calculations.
Q.2 Use the following matrices A, B, C to find
5 2 4 11 5 3 7 14 1
A 1 7 3 B 0 12 4 C 10 3 2
6 10 0 2 1 8 5 9
6
a) Does A*B = B*A ?
b) Does A*(B*C) = (A*B)*C ?
c) Does (A*B)t = Bt*At ? (t means transpose)
d) Does (A+B)t = At +Bt ? (t means transpose)
xii
MATLAB awareness program MAP
matrices is associative.
c) Calculate 5(A+C) and 5A+5C to show that , when matrices
are multiplied scalar , the multiplication is distributive.
d) Calculate A*(B+C) and A*B+A*C to show that matrix
multiplication is distributive.
4. File
4.1 Script
4.1.1 Creating
% This script file calculates the average points scored in three
games.
% The points from each game is assigned to the variable by input
command.
% The disp command is used to display the output.
g1=input('Enter the points scored in 1st game ');
g2=input('Enter the points scored in 2nd game ');
g3=input('Enter the points scored in 3rd game ');
ave=(g1+g2+g3)/3 ;
disp(' ')
disp('The average of points scored in a game is :')
disp(' ')
disp(ave)
4.1.2 Saving
Save this file with any name (not number) at default folder „work‟ which
located at C:\MATLAB7\work check the same path at MATLAB7 window
current directory location.
4.1.3 Running
After saving go to debug menu and click on run or save and run (if not
saved earlier).
xiii
MATLAB awareness program MAP
4.1.4 Outing
Using disp command we are outing result .The command window will look
like as
Enter the points scored in 1st game 5
Enter the points scored in 2nd game 4
Enter the points scored in 3rd game 5
4.6667
We can use fprintf('The average of point scored in the three games%f
',ave); in place of disp
4.2 Function
Function files are m-files .That are used to create new MATLAB
functions. Variables defined and manipulated inside a function file are
local to function.
The general form of function file is
Function variable (s) = function_name(argument)
% help text in the usage of the function
%
.
.
end
4.2.1 Creating
Ex- Write a function file to solve the equivalent resistance of series
connected resistor R1, R2, R3, R4….Rn
R = R1+R2+R3+…..+Rn
Function req =equiv_sr (r)
% equiv_sr (r) is a function program for obtaining the equivalent
resistance of series connected resistor
% Usage : req = equiv_sr(r)
% r is input vector of length n
% req is an output, the equivalent resistance (scalar)
n=length(r); % number of resistance
req = sum(r); % sum of resistor
end
4.2.2 Saving
Save this file with equiv_sr.m (not number) at default folder „work‟ which
located at C:\MATLAB7\work check the same path at MATLAB7 window
current directory location.
4.2.3 Running
After saving go to command promt >> use the function as shown
>>a=[1.1 100 2.2 14];
>> series=equiv_sr(a)
xiv
MATLAB awareness program MAP
4.4.4 Outing
series =
117.3000
Assignment- III
Q.1 Write a program in a script file that determines the real roots of a
quadratic equation ax2+bx+c=0. Name the file quadroots. When
the file runs it asks the user to enter the values of the constants
a, b, and c.To calculate the roots of the equation the program
calculates the discrimination D given by:
D= b2-4ac
If D >0 the program displays a message “ the equation has two
roots”, and the roots are displayed in the next line.
If D = 0 the program displays a message “ the equation has one
root”, and the roots are displayed in the next line.
If D<0 the program displays a message “ the equation has no
real roots”, and the roots are displayed in the next line.
Run this file in command window to obtain solution for
X2+3x+2=0
15X2+10x+5=0
x2-2x+3=0
Q.2 Write a function file that can be used to calculate the equivalent
resistance of n parallel connected register
1 1 1 1 1
........
Req R1 R2 R3 Rn
xv
MATLAB awareness program MAP
5. Plotting
5.1 2D PLOTS
MATLAB has built in functions that allow one to generate bar charts,
X-Y polar, contour and 3D.
MATLAB also allows one to give titles to graph, label the X-Y axes
and add grid to graphs.
Function Plot
Bar (x,y) Vertical bars
Barh (x,y) Horizontal
bars
Stairs(x,y) Stairs case
nature
Stem(x,y) Sampled
graph
Pie(x) Pie chart
xvi
MATLAB awareness program MAP
2,2,1 2,2,2
2,2,3 2,2,4
5.2 3D PLOTS
Mesh & surface plot
%This program plots 3D for given function
x=-3:0.25:3;
y=-3:0.25:3;
[X,Y]=meshgrid(x,y);
Z=1.8.^(-1.5*sqrt(X.^2+Y.^2)).*cos(0.5*Y).*sin(X);
mesh(X,Y,Z)
xlabel('x');ylebel('y');zlebel('z')
xvii
MATLAB awareness program MAP
xviii
MATLAB awareness program MAP
Assignment- IV
Q.1 Plot sin and cos function on same graph. Use linspace command to
generate steps?
xix
MATLAB awareness program MAP
6. MATLAB programming
6.1 Relational & logical operators
A relational operator compares two numbers by determining
whether a comparison statement is true or false.
Precedence
Precedence operation
1 highest Parentheses
2 Exponentiation
3 Logical not
4 Multiplication/ division
5 Addition/ Subtraction
6 Relational operators
7 Logical AND
8 Logical OR
xx
MATLAB awareness program MAP
Assignment- V
xxi