Matlab Adil Yücel
Matlab Adil Yücel
LECTURE NOTES
MATLAB
LECTURE NOTES
Student Name
Student ID
MATLAB
LECTURE NOTES
LESSON 1
INTRODUCTION
Dr. ADL YCEL
Istanbul Technical University
Department of Mechanical Engineering
MATLAB is written in C.
MATLAB works as an interpreter.
MATLAB works slower than C and Fortran.
MATLAB has many toolboxes for different
disciplines.
Command Window
is the main window
that you write your
commands
Command History
lists the copy of the
commands you have
run on the command
window
clc
Clears the command window.
Does not delete any variable.
home
Move cursor to the upper left corner of the command
window.
clear
Deletes all the variables.
clear <variable name>
Deletes a certain variable.
who
Lists the names of the variables defined by the user.
whos
Lists detailed information about the variables defined
by the user.
beep
Produce a beep sound.
computer
Identify the computer on which MATLAB is running.
realmin
Smallest positive real number.
realmax
Largest positive real number.
date
Shows the current system date.
disp ( <variable name> )
Displays the value of the variable.
disp ( <string constant> )
Displays the string constant.
ans
Automatically created variable when the result of an
operation is not assigned to a variable.
pi
Mathematical constant.
%
Used for writing a comment.
;
Used for suppressing a variable or the result of an
operation.
*
.*
^
.^
\
/
.\
./
:
: Plus
: Minus
: Matrix multiplication
: Array multiplication (Element wise)
: Matrix power
: Array power (Element wise)
: Backslash or left division
: Slash or right division
: Left array division (Element wise)
: Right array division (Element wise)
: Colon
: Transpose
=
==
~=
>
<
>=
<=
&
|
~
true
false
: Assignment
: Equal to
: Not equal to
: Greater than
: Less than
: Greater than or equal to
: Less than or equal to
: Logical AND
: Logical OR
: Logical NOT
: Logical TRUE
: Logical FALSE
abs (x)
Calculates absolute value of x
ceil (x)
Rounds x to the next integer
floor (x)
Rounds x to the previous integer
round (x)
Rounds x to the nearest integer
exp (x)
Calculates exponential of x (ex)
log (x)
Calculates natural logarithm of x (lnx)
log2 (x)
Calculates base 2 logarithm of x (log2x)
log10 (x)
Calculates base 10 logarithm of x (log10x)
mod (x,a)
Calculates modulus of x (x mod a)
sqrt (x)
Calculates square root of x
nthroot (x,n)
Calculates nth root of x
factorial (x)
Calculates factorial of x (x!)
sin (x)
Calculates sine of x
sinh (x)
Calculates hyperbolic sine of x
asin (x)
Calculates inverse sine of x
asinh (x)
Calculates inverse hyperbolic sine of x
cos (x)
Calculates cosine of x
cosh (x)
Calculates hyperbolic cosine of x
acos (x)
Calculates inverse cosine of x
acosh (x)
Calculates inverse hyperbolic cosine of x
tan (x)
Calculates tangent of x
tanh (x)
Calculates hyperbolic tangent of x
atan (x)
Calculates inverse tangent of x
atanh (x)
Calculates inverse hyperbolic tangent of x
sind (x)
Calculates sine of x (x in degrees)
cosd (x)
Calculates cosine of x (x in degrees)
tand (x)
Calculates tangent of x (x in degrees)
cotd (x)
Calculates cotangent of x (x in degrees)
secd (x)
Calculates secant of x (x in degrees)
asind (x)
Calculates inverse sine of x (result in degrees)
acosd (x)
Calculates inverse cosine of x (result in degrees)
atand (x)
Calculates inverse tangent of x (result in degrees)
acotd (x)
Calculates inverse cotangent of x (result in degrees)
asecd (x)
Calculates inverse secant of x (result in degrees)
format short
4 decimal places (3.1416)
format short e
4 decimal places with exponent (3.1416e+000)
format long
Many decimal places (3.141592653589793)
format long e
Many decimal places with exponent
(3.141592653589793e+000)
format bank
2 decimal places (3.14)
MATLAB
LECTURE NOTES
LESSON 2
MATRIX OPERATIONS
Dr. ADL YCEL
Istanbul Technical University
Department of Mechanical Engineering
a:b:c
Starting Value
Increment
Ending Value
linspace (a,b,n)
This method creates an array with n number of equally
spaced values starting from a and ending at b.
logspace (a,b,n)
This method creates an array with n number of equally
spaced logarithmic (10x) values starting from a and
ending at b.
dot (A,B)
Calculates the dot product of two vectors.
cross (A,B)
Calculates the cross product of two vectors.
sort (A)
Sorts the elements of the given vector.
mean (A)
Calculates the average value of the elements of the
given vector.
sum (A)
Calculates the sum of the elements of the given
vector.
prod (A)
Calculates the product of the elements of the given
vector.
min (A)
Results the minimum value of the given vector.
max (A)
Results the maximum value of the given vector.
zeros (m,n)
Creates an mxn matrix of all zeros.
ones (m,n)
Creates an mxn matrix of all ones.
eye (m)
Creates an mxm identity matrix.
rand (m,n)
Creates an mxn matrix full of uniformly distributed
random numbers between 0 and 1.
diag (A)
Creates a diagonal matrix with the elements of the
given vector as the diagonal.
size (A)
Returns the dimensions of the given matrix.
length (A)
Returns the length of the given vector.
det (A)
Calculates the determinant of the given matrix.
inv (A)
Calculates the inverse of the given matrix.
A
Calculates the transpose of the given matrix.
A(m,n)
Results the element of the given matrix in row m and
column n.
A(:,n)
Results the elements of the given matrix in column n.
A(m,:)
Results the elements of the given matrix in row m.
K.X = S
K-1.K.X = K-1.S
X = K-1.S
X = inv (K)*S
3a + 6b + 8c = 101.5
5a 4b 7c = 56.5
4a + 5b + 9c = 108.5
MATLAB
LECTURE NOTES
LESSON 3
DATA VISUALIZATION
Dr. ADL YCEL
Istanbul Technical University
Department of Mechanical Engineering
plot (x,y)
Plots the given vectors on a normal chart.
plot (x1,y1,x2,y2)
Plots the two given vectors on a normal chart.
plotyy (x,y1,x,y2)
Plots the two given vectors with two different y-axis.
bar (x,y)
Plots the given vectors on a vertical bar chart.
barh (x,y)
Plots the given vectors on a horizontal bar chart.
polar (x,y)
Plots the given vectors on a polar coordinate chart.
area (x,y)
Plots the given vectors on an area chart.
quiver (x,y)
Plots the given vectors on a velocity chart.
ribbon (x,y)
Plots the given vectors on a ribbon chart.
stairs (x,y)
Plots the given vectors on a stairstep chart.
stem (x,y)
Plots the given vectors on a discrete data chart.
pie (x)
Plots the pie chart for the values given in the vector.
semilogx (x,y)
Plots the given vectors on a normal chart with
logarithmic x axis and linear y axis.
semilogy (x,y)
Plots the given vectors on a normal chart with
logarithmic y axis and linear x axis.
loglog (x,y)
Plots the given vectors on a normal chart with
logarithmic x axis and logarithmic y axis .
subplot (m,n,k)
Creates a chart area of mxn and locates the next plot
to the position of k.
MATLAB
LECTURE NOTES
LESSON 4
POLYNOMIALS
Dr. ADL YCEL
Istanbul Technical University
Department of Mechanical Engineering
polyval (p,x)
This method calculates the value of the polynomial p
for the given value of x.
roots (p)
Calculates the roots of the given polynomial vector.
poly (r)
Calculates the polynomial vector for the given roots.
polyder (p)
Calculates the derivative of the given polynomial
vector.
polyint (p)
Calculates the analytic integral of the given
polynomial vector.
conv (p1,p2)
Multiplies the given two polynomial vectors.
deconv (p1,p2)
Divides the given two polynomial vectors.
polyfit (x,y,degree)
Calculates the polynomial vector of nth degree that
interp1 (x,y,xnew,method)
Performs a one-dimensional interpolation over the data
given by x and y for the values of xnew using the
specified method. Methods are ;
nearest
linear
spline
cubic
MATLAB
LECTURE NOTES
LESSON 5
PROGRAMMING
Dr. ADL YCEL
Istanbul Technical University
Department of Mechanical Engineering
m-file
Script File
A script file is
a sequence of
commands or
statements.
Function File
A function file is
a sub-program
to execute a
certain process.
inline (...)
Lets you define simple functions without writing
external m-files.
input (...)
Lets you input data into a variable from command
window.
fprintf (...)
Displays constant data or the values of variables in a
certain format. Use the formatting strings given below
to format the output.
Examples of fprintf :
Program Block
Program Block
Program Block
if grade < 50
disp ('You failed.')
else
disp ('You passed.')
end
if mach < 1
disp ('Flow is subsonic.')
elseif mach > 1
disp ('Flow is supersonic.')
else
disp ('Flow is sonic.')
end
Program Block
Program Block
Program Block
Program Block
Program Block
for k = 1:1:10
s = k^2;
fprintf ('Square of %2d = %d \n',k,s);
end
continue
This command jumps to the next iteration of the loop.
for k = 1:1:10
if k == 5
continue
end
s = k^2;
fprintf ('Square of %2d = %d \n',k,s);
end
break
This command stops the loop.
for k = 1:1:10
if k == 5
break
end
s = k^2;
fprintf ('Square of %2d = %d \n',k,s);
end
Program Block
k = 5;
while k <= 10
c = k^3;
fprintf ('Cube of %2d = %d \n',k,c);
k = k + 1;
end
MATLAB
LECTURE NOTES
LESSON 6
NUMERICAL INTEGRATION
Dr. ADL YCEL
Istanbul Technical University
Department of Mechanical Engineering
Trapezoidal Rule :
Trapezoidal Rule :
Simpson Rule :
Simpson Rule :
trapz (x,y)
Calculates the area under the trapezoids created by
x-y pairs.
quad (...,a,b)
Numerically integrates the given function in the range
of a and b using the adaptive simpson method.
MATLAB
LECTURE NOTES
LESSON 7
ROOT FINDING ALGORITHMS
Dr. ADL YCEL
Istanbul Technical University
Department of Mechanical Engineering
Bisection Method :
Newton's Method :
converges to a root.
fzero (f,x0)
Finds the root of the function f starting from an initial
guess of x0.
fminbnd (f,a,b)
Finds the local minimizer of function f in the interval a
and b.
MATLAB
LECTURE NOTES
LESSON 8
SYMBOLIC ALGEBRA
Dr. ADL YCEL
Istanbul Technical University
Department of Mechanical Engineering
syms ...
Used to create symbolic variables.
sym (...)
Used to create symbolic variables, expressions and
equations.
findsym (f)
Determines the symbolic variables present in an
expression.
subs (S,old,new)
Substitutes a variable with a new variable or a new
value in a symbolic expression.
collect (S)
Collects the coefficients in a symbolic expression.
factor (S)
Gives the prime factors of a symbolic expression.
expand (S)
Expands a symbolic expression.
simplify (S)
Simplifies a symbolic expression.
poly2sym (p)
Converts a polynomial vector to a symbolic expression.
sym2poly (S)
Converts a symbolic expression to a polynomial vector.
diff (S)
Calculates the derivative of the symbolic expression.
diff (S,u)
Calculates the derivative of the symbolic expression
with respect to a variable.
int (S)
Integrates the symbolic expression.
int (S,u)
Integrates the symbolic expression with respect to a
variable.
int (S,u,a,b)
Integrates the symbolic expression with respect to a
variable within the range of a and b.
solve (...,...,...)
Solves the given equation or the system of equations.
dsolve (...,...,...)
Solves the given differential equation or the system of
differential equations.
MATLAB
LECTURE NOTES
LESSON 9
THREE DIMENSIONAL PLOTS
Dr. ADL YCEL
Istanbul Technical University
Department of Mechanical Engineering
mesh (X, Y, Z)
Used to plot X, Y and Z values using a mesh view.
rangeX = -10:1:10;
rangeY = -5:1:5;
[X, Y] = meshgrid (rangeX, rangeY);
Z = X .* sin(Y);
mesh (X, Y, Z);
meshc (X, Y, Z)
Used to plot X, Y and Z values using a mesh view with
projections.
rangeX = -10:1:10;
rangeY = -5:1:5;
[X, Y] = meshgrid (rangeX, rangeY);
Z = X .* sin(Y);
meshc (X, Y, Z);
surf (X, Y, Z)
Used to plot X, Y and Z values using a surface mesh
view.
rangeX = -10:1:10;
rangeY = -5:1:5;
[X, Y] = meshgrid (rangeX, rangeY);
Z = X .* sin(Y);
surf (X, Y, Z);
surfc (X, Y, Z)
Used to plot X, Y and Z values using a surface mesh
view with projections.
rangeX = -10:1:10;
rangeY = -5:1:5;
[X, Y] = meshgrid (rangeX, rangeY);
Z = X .* sin(Y);
surfc (X, Y, Z);
shading interp
Plots the graphic with interpolated colors.
rangeX = -10:1:10;
rangeY = -5:1:5;
[X, Y] = meshgrid (rangeX, rangeY);
Z = X .* sin(Y);
surf (X, Y, Z);
shading interp
shading faceted
Plots the graphic with meshed colors.
rangeX = -10:1:10;
rangeY = -5:1:5;
[X, Y] = meshgrid (rangeX, rangeY);
Z = X .* sin(Y);
surf (X, Y, Z);
shading faceted
plot3 (X, Y, Z)
Used to create a simple plot in three dimensions.
X = -10:0.1:10;
Y = -10:0.1:10;
Z = X .* sin(Y);
plot3 (X, Y, Z);
grid