SlideShare a Scribd company logo
Introduction to Matlab
Introduction to Computing
by
Engr. Mohammad Haroon Yousaf
Topics
• Introduction
• MATLAB Environment
• Getting Help
• Variables
• Vectors, Matrices, and Linear Algebra
• Plotting
• Built in Functions
• Selection Programming
• M-Files
• User Defined Functions
• Specific Topics
Introduction
 What is MATLAB ?
• MATLAB is a computer program that combines computation and
visualization power that makes it particularly useful tool for engineers.
• MATLAB is an executive program, and a script can be made with a list
of MATLAB commands like other programming language.
 MATLAB Stands for MATrix LABoratory.
• The system was designed to make matrix computation particularly easy.
 The MATLAB environment allows the user to:
• manage variables
• import and export data
• perform calculations
• generate plots
• develop and manage files for use with MATLAB.
To start MATLAB:
START  PROGRAMS 
MATLAB 7.0  MATLAB
7.0
MATLAB
Environment
Display Windows
Display Windows (con’t…)
• Graphic (Figure) Window
– Displays plots and graphs
– Created in response to graphics commands.
• M-file editor/debugger window
– Create and edit scripts of commands called M-files.
Getting Help
• type one of following commands in the command window:
– help – lists all the help topic
– help topic – provides help for the specified topic
– help command – provides help for the specified command
• help help – provides information on use of the help command
– helpwin – opens a separate help window for navigation
– lookfor keyword – Search all M-files for keyword
Getting Help (con’t…)
• Google “MATLAB helpdesk”
• Go to the online HelpDesk provided by
www.mathworks.com
You can find EVERYTHING you
need to know about MATLAB
from the online HelpDesk.
Variables
• Variable names:
– Must start with a letter
– May contain only letters, digits, and the underscore “_”
– Matlab is case sensitive, i.e. one & OnE are different variables.
– Matlab only recognizes the first 31 characters in a variable name.
• Assignment statement:
– Variable = number;
– Variable = expression;
• Example:
>> tutorial = 1234;
>> tutorial = 1234
tutorial =
1234
NOTE: when a semi-colon
”;” is placed at the end of
each command, the result
is not displayed.
Variables (con’t…)
• Special variables:
– ans : default variable name for the result
– pi:  = 3.1415926…………
– eps:  = 2.2204e-016, smallest amount by which 2 numbers can differ.
– Inf or inf : , infinity
– NaN or nan: not-a-number
• Commands involving variables:
– who: lists the names of defined variables
– whos: lists the names and sizes of defined variables
– clear: clears all varialbes, reset the default values of special
variables.
– clear name: clears the variable name
– clc: clears the command window
– clf: clears the current figure and the graph window.
Vectors, Matrices and Linear Algebra
• Vectors
• Array Operations
• Matrices
• Solutions to Systems of Linear Equations.
MATLAB BASICS
Variables and Arrays
• Array: A collection of data values organized into rows
and columns, and known by a single name.
Row 1
Row 2
Row 3
Row 4
Col 1 Col 2 Col 3 Col 4 Col 5
arr(3,2)
MATLAB BASICS
Arrays
• The fundamental unit of data in MATLAB
• Scalars are also treated as arrays by MATLAB (1
row and 1 column).
• Row and column indices of an array start from 1.
• Arrays can be classified as vectors and
matrices.
MATLAB BASICS
• Vector: Array with one dimension
• Matrix: Array with more than one dimension
• Size of an array is specified by the number of rows
and the number of columns, with the number of
rows mentioned first (For example: n x m array).
Total number of elements in an array is the
product of the number of rows and the number of
columns.
MATLAB BASICS
1 2
3 4
5 6
a= 3x2 matrix  6 elements
b=[1 2 3 4] 1x4 array  4 elements, row vector
c=
1
3
5
3x1 array  3 elements, column vector
a(2,1)=3 b(3)=3 c(2)=3
Row # Column #
Vectors
• A row vector in MATLAB can be created by an explicit list, starting with a left bracket, entering
the values separated by spaces (or commas) and closing the vector with a right bracket.
• A column vector can be created the same way, and the rows are separated by semicolons.
• Example:
>> x = [ 0 0.25*pi 0.5*pi 0.75*pi pi ]
x =
0 0.7854 1.5708 2.3562 3.1416
>> y = [ 0; 0.25*pi; 0.5*pi; 0.75*pi; pi ]
y =
0
0.7854
1.5708
2.3562
3.1416
x is a row vector.
y is a column vector.
Vectors (con’t…)
• Vector Addressing – A vector element is addressed in MATLAB with an integer index
enclosed in parentheses.
• Example:
>> x(3)
ans =
1.5708
 1st to 3rd elements of vector x
• The colon notation may be used to address a block of elements.
(start : increment : end)
start is the starting index, increment is the amount to add to each successive index, and end is the
ending index. A shortened format (start : end) may be used if increment is 1.
• Example:
>> x(1:3)
ans =
0 0.7854 1.5708
NOTE: MATLAB index starts at 1.
 3rd element of vector x
Vectors (con’t…)
Some useful commands:
x = start:end create row vector x starting with start, counting by one,
ending at end
x = start:increment:end create row vector x starting with start, counting by
increment, ending at or before end
length(x) returns the length of vector x
y = x’ transpose of vector x
dot (x, y) returns the scalar dot product of the vector x and y.
Array Operations
• Scalar-Array Mathematics
For addition, subtraction, multiplication, and division of an array by a
scalar simply apply the operations to all elements of the array.
• Example:
>> f = [ 1 2; 3 4]
f =
1 2
3 4
>> g = 2*f – 1
g =
1 3
5 7
Each element in the array f is
multiplied by 2, then subtracted
by 1.
Array Operations (con’t…)
• Element-by-Element Array-Array Mathematics.
Operation Algebraic Form MATLAB
Addition a + b a + b
Subtraction a – b a – b
Multiplication a x b a .* b
Division a  b a ./ b
Exponentiation ab a .^ b
• Example:
>> x = [ 1 2 3 ];
>> y = [ 4 5 6 ];
>> z = x .* y
z =
4 10 18
Each element in x is multiplied by
the corresponding element in y.
Matrices
A is an m x n matrix.
 A Matrix array is two-dimensional, having both multiple rows and multiple columns,
similar to vector arrays:
 it begins with [, and end with ]
 spaces or commas are used to separate elements in a row
 semicolon or enter is used to separate rows.
•Example:
>> f = [ 1 2 3; 4 5 6]
f =
1 2 3
4 5 6
>> h = [ 2 4 6
1 3 5]
h =
2 4 6
1 3 5
the main diagonal
Matrices (con’t…)
• Matrix Addressing:
-- matrixname(row, column)
-- colon may be used in place of a row or column reference to select the
entire row or column.
recall:
f =
1 2 3
4 5 6
h =
2 4 6
1 3 5
 Example:
>> f(2,3)
ans =
6
>> h(:,1)
ans =
2
1
Matrices (con’t…)
Some useful commands:
zeros(n)
zeros(m,n)
ones(n)
ones(m,n)
size (A)
length(A)
returns a n x n matrix of zeros
returns a m x n matrix of zeros
returns a n x n matrix of ones
returns a m x n matrix of ones
for a m x n matrix A, returns the row vector [m,n]
containing the number of rows and columns in
matrix.
returns the larger of the number of rows or
columns in A.
Matrices (con’t…)
Transpose B = A’
Identity Matrix eye(n)  returns an n x n identity matrix
eye(m,n)  returns an m x n matrix with ones on the main
diagonal and zeros elsewhere.
Addition and subtraction C = A + B
C = A – B
Scalar Multiplication B = A, where  is a scalar.
Matrix Multiplication C = A*B
Matrix Inverse B = inv(A), A must be a square matrix in this case.
rank (A)  returns the rank of the matrix A.
Matrix Powers B = A.^2  squares each element in the matrix
C = A * A  computes A*A, and A must be a square matrix.
Determinant det (A), and A must be a square matrix.
more commands
A, B, C are matrices, and m, n,  are scalars.
Solutions to Systems of Linear Equations
• Example: a system of 3 linear equations with 3 unknowns (x1, x2, x3):
3x1 + 2x2 – x3 = 10
-x1 + 3x2 + 2x3 = 5
x1 – x2 – x3 = -1
Then, the system can be described as:
Ax = b














1
1
1
2
3
1
1
2
3
A











3
2
1
x
x
x
x












1
5
10
b
Let :
Solutions to Systems of Linear Equations (con’t…)
• Solution by Matrix Inverse:
Ax = b
A-1Ax = A-1b
x = A-1b
• MATLAB:
>> A = [ 3 2 -1; -1 3 2; 1 -1 -1];
>> b = [ 10; 5; -1];
>> x = inv(A)*b
x =
-2.0000
5.0000
-6.0000
Answer:
x1 = -2, x2 = 5, x3 = -6
• Solution by Matrix Division:
The solution to the equation
Ax = b
can be computed using left division.
Answer:
x1 = -2, x2 = 5, x3 = -6
NOTE:
left division: Ab  b  A right division: x/y  x  y
 MATLAB:
>> A = [ 3 2 -1; -1 3 2; 1 -1 -1];
>> b = [ 10; 5; -1];
>> x = Ab
x =
-2.0000
5.0000
-6.0000
• The input function displays a prompt string in the
Command Window and then waits for the user to
respond.
my_val = input( ‘Enter an input value: ’ );
in1 = input( ‘Enter data: ’ );
in2 = input( ‘Enter data: ’ ,`s`);
Initializing with Keyboard Input
How to display data
The disp( ) function
>> disp( 'Hello' )
Hello
>> disp(5)
5
>> disp( [ 'Bilkent ' 'University' ] )
Bilkent University
>> name = 'Alper';
>> disp( [ 'Hello ' name ] )
Hello Alper
Plotting
• For more information on 2-D plotting, type help graph2d
• Plotting a point:
>> plot ( variablename, ‘symbol’)
the function plot () creates a
graphics window, called a Figure
window, and named by default
“Figure No. 1”
 Example : Complex number
>> z = 1 + 0.5j;
>> plot (z, ‘.’)
Plotting (con’t…)
Built-in MATLAB Functions
result = function_name( input );
– abs, sign
– log, log10, log2
– exp
– sqrt
– sin, cos, tan
– asin, acos, atan
– max, min
– round, floor, ceil, fix
– mod, rem
• help elfun  help for elementary math functions
Selection Programming
• Flow Control
• Loops
Flow Control
• Simple if statement:
if logical expression
commands
end
• Example: (Nested)
if d <50
count = count + 1;
disp(d);
if b>d
b=0;
end
end
• Example: (else and elseif clauses)
if temperature > 100
disp (‘Too hot – equipment malfunctioning.’)
elseif temperature > 90
disp (‘Normal operating range.’);
elseif (‘Below desired operating range.’)
else
disp (‘Too cold – turn off equipment.’)
end
Flow Control (con’t…)
• The switch statement:
switch expression
case test expression 1
commands
case test expression 2
commands
otherwise
commands
end
• Example:
switch interval < 1
case 1
xinc = interval /10;
case 0
xinc = 0.1;
end
Loops
• for loop
for variable = expression
commands
end
• while loop
while expression
commands
end
•Example (for loop):
for t = 1:5000
y(t) = sin (2*pi*t/10);
end
•Example (while loop):
EPS = 1;
while ( 1+EPS) >1
EPS = EPS/2;
end
EPS = 2*EPS
 the break statement
break – is used to terminate the execution of the loop.
M-Files
• The M-file is a text file that consists a group of
MATLAB commands.
• MATLAB can open and execute the commands
exactly as if they were entered at the MATLAB
command window.
• To run the M-files, just type the file name in the
command window. (make sure the current working
directory is set correctly)
All MATLAB commands are M-files.
So far, we have executed the commands in the command window.
But a more practical way is to create a M-file.
User-Defined Function
• Add the following command in the beginning of your m-file:
function [output variables] = function_name (input variables);
NOTE: the function_name should
be the same as your file name to
avoid confusion.
 calling your function:
-- a user-defined function is called by the name of the m-file, not
the name given in the function definition.
-- type in the m-file name like other pre-defined commands.
 Comments:
-- The first few lines should be comments, as they will be
displayed if help is requested for the function name. the first
comment line is reference by the lookfor command.
Specific Topics
• This tutorial gives you a general background on the
usage of MATLAB.
• There are thousands of MATLAB commands for
many different applications, therefore it is
impossible to cover all topics here.
• For a specific topic relating to a class, you should
consult the TA or the Instructor.
Questions?
Topics
• Introduction
• MATLAB Environment
• Getting Help
• Variables
• Vectors, Matrices, and Linear Algebra
• Mathematical Functions and Applications
• Plotting
• Selection Programming
• M-Files
• User Defined Functions
• Specific Topics
Ad

More Related Content

What's hot (20)

My M.S. Thesis Proposal
My M.S. Thesis ProposalMy M.S. Thesis Proposal
My M.S. Thesis Proposal
Yaser Sulaiman
 
Introduction to Wireless Communication
Introduction to Wireless CommunicationIntroduction to Wireless Communication
Introduction to Wireless Communication
Dilum Bandara
 
Wimax security
Wimax securityWimax security
Wimax security
Behroz Zarrinfar
 
Energy efficient routing protocol
Energy efficient routing protocolEnergy efficient routing protocol
Energy efficient routing protocol
NIIS Institute of Business Management, Bhubaneswar
 
Mimo [new]
Mimo [new]Mimo [new]
Mimo [new]
Mokpo National University
 
Materials Characterization Technique Lecture Notes
Materials Characterization Technique Lecture NotesMaterials Characterization Technique Lecture Notes
Materials Characterization Technique Lecture Notes
FellowBuddy.com
 
EC8702 adhoc and wireless sensor networks iv ece
EC8702 adhoc and wireless sensor networks iv eceEC8702 adhoc and wireless sensor networks iv ece
EC8702 adhoc and wireless sensor networks iv ece
GOWTHAMMS6
 
Mm wave
Mm waveMm wave
Mm wave
Mustafa Khaleel
 
Lecture 3 Properties of Nanomaterial- Surface to Volume Ratio.ppt
Lecture 3 Properties of Nanomaterial- Surface to Volume Ratio.pptLecture 3 Properties of Nanomaterial- Surface to Volume Ratio.ppt
Lecture 3 Properties of Nanomaterial- Surface to Volume Ratio.ppt
DivitGoyal2
 
Advanced Wireless Communication-EC8092
Advanced Wireless Communication-EC8092Advanced Wireless Communication-EC8092
Advanced Wireless Communication-EC8092
Prakash Velayudham V
 
5G and 6G.pptx
5G and 6G.pptx5G and 6G.pptx
5G and 6G.pptx
nassmah
 
Nanotribology
NanotribologyNanotribology
Nanotribology
Digitech Rathod
 
NBA Presentation-EEE
NBA Presentation-EEENBA Presentation-EEE
NBA Presentation-EEE
Dr.NAGARAJAN. S
 
Introduction to Scanning Tunneling Microscopy
Introduction to Scanning Tunneling MicroscopyIntroduction to Scanning Tunneling Microscopy
Introduction to Scanning Tunneling Microscopy
nirupam12
 
Micro electro mechanical systems
Micro electro mechanical systemsMicro electro mechanical systems
Micro electro mechanical systems
sree navya
 
Photolithography and its procedure
Photolithography and its procedurePhotolithography and its procedure
Photolithography and its procedure
karoline Enoch
 
Backhaul Ppt
Backhaul PptBackhaul Ppt
Backhaul Ppt
willyaoll
 
Text analytics in social media
Text analytics in social mediaText analytics in social media
Text analytics in social media
Jeremiah Fadugba
 
Nanowire Based FET Biosensors and Their Biomedical Applications. Fawad Majeed...
Nanowire Based FET Biosensors and Their Biomedical Applications. Fawad Majeed...Nanowire Based FET Biosensors and Their Biomedical Applications. Fawad Majeed...
Nanowire Based FET Biosensors and Their Biomedical Applications. Fawad Majeed...
Fawad Majeed
 
802.11ac Overview
802.11ac Overview802.11ac Overview
802.11ac Overview
Michal Jarski
 
My M.S. Thesis Proposal
My M.S. Thesis ProposalMy M.S. Thesis Proposal
My M.S. Thesis Proposal
Yaser Sulaiman
 
Introduction to Wireless Communication
Introduction to Wireless CommunicationIntroduction to Wireless Communication
Introduction to Wireless Communication
Dilum Bandara
 
Materials Characterization Technique Lecture Notes
Materials Characterization Technique Lecture NotesMaterials Characterization Technique Lecture Notes
Materials Characterization Technique Lecture Notes
FellowBuddy.com
 
EC8702 adhoc and wireless sensor networks iv ece
EC8702 adhoc and wireless sensor networks iv eceEC8702 adhoc and wireless sensor networks iv ece
EC8702 adhoc and wireless sensor networks iv ece
GOWTHAMMS6
 
Lecture 3 Properties of Nanomaterial- Surface to Volume Ratio.ppt
Lecture 3 Properties of Nanomaterial- Surface to Volume Ratio.pptLecture 3 Properties of Nanomaterial- Surface to Volume Ratio.ppt
Lecture 3 Properties of Nanomaterial- Surface to Volume Ratio.ppt
DivitGoyal2
 
Advanced Wireless Communication-EC8092
Advanced Wireless Communication-EC8092Advanced Wireless Communication-EC8092
Advanced Wireless Communication-EC8092
Prakash Velayudham V
 
5G and 6G.pptx
5G and 6G.pptx5G and 6G.pptx
5G and 6G.pptx
nassmah
 
Introduction to Scanning Tunneling Microscopy
Introduction to Scanning Tunneling MicroscopyIntroduction to Scanning Tunneling Microscopy
Introduction to Scanning Tunneling Microscopy
nirupam12
 
Micro electro mechanical systems
Micro electro mechanical systemsMicro electro mechanical systems
Micro electro mechanical systems
sree navya
 
Photolithography and its procedure
Photolithography and its procedurePhotolithography and its procedure
Photolithography and its procedure
karoline Enoch
 
Backhaul Ppt
Backhaul PptBackhaul Ppt
Backhaul Ppt
willyaoll
 
Text analytics in social media
Text analytics in social mediaText analytics in social media
Text analytics in social media
Jeremiah Fadugba
 
Nanowire Based FET Biosensors and Their Biomedical Applications. Fawad Majeed...
Nanowire Based FET Biosensors and Their Biomedical Applications. Fawad Majeed...Nanowire Based FET Biosensors and Their Biomedical Applications. Fawad Majeed...
Nanowire Based FET Biosensors and Their Biomedical Applications. Fawad Majeed...
Fawad Majeed
 

Similar to Introduction to Matlab - Basic Functions (20)

1. Introduction to Computing - MATLAB.pptx
1. Introduction to Computing -  MATLAB.pptx1. Introduction to Computing -  MATLAB.pptx
1. Introduction to Computing - MATLAB.pptx
tgkfkj9n2k
 
matlab_tutorial for student in the first
matlab_tutorial for student in the firstmatlab_tutorial for student in the first
matlab_tutorial for student in the first
naghamsalimmohammed
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
naveen_setty
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
aboma2hawi
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
Satish Gummadi
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
Murshida ck
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
chestialtaff
 
Basics of matlab
Basics of matlabBasics of matlab
Basics of matlab
Anil Maurya
 
Matlab Tutorial.ppt
Matlab Tutorial.pptMatlab Tutorial.ppt
Matlab Tutorial.ppt
RaviMuthamala1
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlab
TUOS-Sam
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
Mukesh Kumar
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
Mohd Esa
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
kebeAman
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
Khulna University
 
COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptx
imman gwu
 
2. Chap 1.pptx
2. Chap 1.pptx2. Chap 1.pptx
2. Chap 1.pptx
HassanShah396906
 
presentation.pptx
presentation.pptxpresentation.pptx
presentation.pptx
raghav415187
 
bobok
bobokbobok
bobok
Adi Pandarangga
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
BilawalBaloch1
 
Matlab1
Matlab1Matlab1
Matlab1
guest8ba004
 
1. Introduction to Computing - MATLAB.pptx
1. Introduction to Computing -  MATLAB.pptx1. Introduction to Computing -  MATLAB.pptx
1. Introduction to Computing - MATLAB.pptx
tgkfkj9n2k
 
matlab_tutorial for student in the first
matlab_tutorial for student in the firstmatlab_tutorial for student in the first
matlab_tutorial for student in the first
naghamsalimmohammed
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
naveen_setty
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
aboma2hawi
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
Murshida ck
 
Basics of matlab
Basics of matlabBasics of matlab
Basics of matlab
Anil Maurya
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlab
TUOS-Sam
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
Mukesh Kumar
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
Mohd Esa
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
kebeAman
 
COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptx
imman gwu
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
BilawalBaloch1
 
Ad

Recently uploaded (20)

Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Journal of Soft Computing in Civil Engineering
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdfDavid Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdfDavid Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Ad

Introduction to Matlab - Basic Functions

  • 1. Introduction to Matlab Introduction to Computing by Engr. Mohammad Haroon Yousaf
  • 2. Topics • Introduction • MATLAB Environment • Getting Help • Variables • Vectors, Matrices, and Linear Algebra • Plotting • Built in Functions • Selection Programming • M-Files • User Defined Functions • Specific Topics
  • 3. Introduction  What is MATLAB ? • MATLAB is a computer program that combines computation and visualization power that makes it particularly useful tool for engineers. • MATLAB is an executive program, and a script can be made with a list of MATLAB commands like other programming language.  MATLAB Stands for MATrix LABoratory. • The system was designed to make matrix computation particularly easy.  The MATLAB environment allows the user to: • manage variables • import and export data • perform calculations • generate plots • develop and manage files for use with MATLAB.
  • 4. To start MATLAB: START  PROGRAMS  MATLAB 7.0  MATLAB 7.0 MATLAB Environment
  • 6. Display Windows (con’t…) • Graphic (Figure) Window – Displays plots and graphs – Created in response to graphics commands. • M-file editor/debugger window – Create and edit scripts of commands called M-files.
  • 7. Getting Help • type one of following commands in the command window: – help – lists all the help topic – help topic – provides help for the specified topic – help command – provides help for the specified command • help help – provides information on use of the help command – helpwin – opens a separate help window for navigation – lookfor keyword – Search all M-files for keyword
  • 8. Getting Help (con’t…) • Google “MATLAB helpdesk” • Go to the online HelpDesk provided by www.mathworks.com You can find EVERYTHING you need to know about MATLAB from the online HelpDesk.
  • 9. Variables • Variable names: – Must start with a letter – May contain only letters, digits, and the underscore “_” – Matlab is case sensitive, i.e. one & OnE are different variables. – Matlab only recognizes the first 31 characters in a variable name. • Assignment statement: – Variable = number; – Variable = expression; • Example: >> tutorial = 1234; >> tutorial = 1234 tutorial = 1234 NOTE: when a semi-colon ”;” is placed at the end of each command, the result is not displayed.
  • 10. Variables (con’t…) • Special variables: – ans : default variable name for the result – pi:  = 3.1415926………… – eps:  = 2.2204e-016, smallest amount by which 2 numbers can differ. – Inf or inf : , infinity – NaN or nan: not-a-number • Commands involving variables: – who: lists the names of defined variables – whos: lists the names and sizes of defined variables – clear: clears all varialbes, reset the default values of special variables. – clear name: clears the variable name – clc: clears the command window – clf: clears the current figure and the graph window.
  • 11. Vectors, Matrices and Linear Algebra • Vectors • Array Operations • Matrices • Solutions to Systems of Linear Equations.
  • 12. MATLAB BASICS Variables and Arrays • Array: A collection of data values organized into rows and columns, and known by a single name. Row 1 Row 2 Row 3 Row 4 Col 1 Col 2 Col 3 Col 4 Col 5 arr(3,2)
  • 13. MATLAB BASICS Arrays • The fundamental unit of data in MATLAB • Scalars are also treated as arrays by MATLAB (1 row and 1 column). • Row and column indices of an array start from 1. • Arrays can be classified as vectors and matrices.
  • 14. MATLAB BASICS • Vector: Array with one dimension • Matrix: Array with more than one dimension • Size of an array is specified by the number of rows and the number of columns, with the number of rows mentioned first (For example: n x m array). Total number of elements in an array is the product of the number of rows and the number of columns.
  • 15. MATLAB BASICS 1 2 3 4 5 6 a= 3x2 matrix  6 elements b=[1 2 3 4] 1x4 array  4 elements, row vector c= 1 3 5 3x1 array  3 elements, column vector a(2,1)=3 b(3)=3 c(2)=3 Row # Column #
  • 16. Vectors • A row vector in MATLAB can be created by an explicit list, starting with a left bracket, entering the values separated by spaces (or commas) and closing the vector with a right bracket. • A column vector can be created the same way, and the rows are separated by semicolons. • Example: >> x = [ 0 0.25*pi 0.5*pi 0.75*pi pi ] x = 0 0.7854 1.5708 2.3562 3.1416 >> y = [ 0; 0.25*pi; 0.5*pi; 0.75*pi; pi ] y = 0 0.7854 1.5708 2.3562 3.1416 x is a row vector. y is a column vector.
  • 17. Vectors (con’t…) • Vector Addressing – A vector element is addressed in MATLAB with an integer index enclosed in parentheses. • Example: >> x(3) ans = 1.5708  1st to 3rd elements of vector x • The colon notation may be used to address a block of elements. (start : increment : end) start is the starting index, increment is the amount to add to each successive index, and end is the ending index. A shortened format (start : end) may be used if increment is 1. • Example: >> x(1:3) ans = 0 0.7854 1.5708 NOTE: MATLAB index starts at 1.  3rd element of vector x
  • 18. Vectors (con’t…) Some useful commands: x = start:end create row vector x starting with start, counting by one, ending at end x = start:increment:end create row vector x starting with start, counting by increment, ending at or before end length(x) returns the length of vector x y = x’ transpose of vector x dot (x, y) returns the scalar dot product of the vector x and y.
  • 19. Array Operations • Scalar-Array Mathematics For addition, subtraction, multiplication, and division of an array by a scalar simply apply the operations to all elements of the array. • Example: >> f = [ 1 2; 3 4] f = 1 2 3 4 >> g = 2*f – 1 g = 1 3 5 7 Each element in the array f is multiplied by 2, then subtracted by 1.
  • 20. Array Operations (con’t…) • Element-by-Element Array-Array Mathematics. Operation Algebraic Form MATLAB Addition a + b a + b Subtraction a – b a – b Multiplication a x b a .* b Division a  b a ./ b Exponentiation ab a .^ b • Example: >> x = [ 1 2 3 ]; >> y = [ 4 5 6 ]; >> z = x .* y z = 4 10 18 Each element in x is multiplied by the corresponding element in y.
  • 21. Matrices A is an m x n matrix.  A Matrix array is two-dimensional, having both multiple rows and multiple columns, similar to vector arrays:  it begins with [, and end with ]  spaces or commas are used to separate elements in a row  semicolon or enter is used to separate rows. •Example: >> f = [ 1 2 3; 4 5 6] f = 1 2 3 4 5 6 >> h = [ 2 4 6 1 3 5] h = 2 4 6 1 3 5 the main diagonal
  • 22. Matrices (con’t…) • Matrix Addressing: -- matrixname(row, column) -- colon may be used in place of a row or column reference to select the entire row or column. recall: f = 1 2 3 4 5 6 h = 2 4 6 1 3 5  Example: >> f(2,3) ans = 6 >> h(:,1) ans = 2 1
  • 23. Matrices (con’t…) Some useful commands: zeros(n) zeros(m,n) ones(n) ones(m,n) size (A) length(A) returns a n x n matrix of zeros returns a m x n matrix of zeros returns a n x n matrix of ones returns a m x n matrix of ones for a m x n matrix A, returns the row vector [m,n] containing the number of rows and columns in matrix. returns the larger of the number of rows or columns in A.
  • 24. Matrices (con’t…) Transpose B = A’ Identity Matrix eye(n)  returns an n x n identity matrix eye(m,n)  returns an m x n matrix with ones on the main diagonal and zeros elsewhere. Addition and subtraction C = A + B C = A – B Scalar Multiplication B = A, where  is a scalar. Matrix Multiplication C = A*B Matrix Inverse B = inv(A), A must be a square matrix in this case. rank (A)  returns the rank of the matrix A. Matrix Powers B = A.^2  squares each element in the matrix C = A * A  computes A*A, and A must be a square matrix. Determinant det (A), and A must be a square matrix. more commands A, B, C are matrices, and m, n,  are scalars.
  • 25. Solutions to Systems of Linear Equations • Example: a system of 3 linear equations with 3 unknowns (x1, x2, x3): 3x1 + 2x2 – x3 = 10 -x1 + 3x2 + 2x3 = 5 x1 – x2 – x3 = -1 Then, the system can be described as: Ax = b               1 1 1 2 3 1 1 2 3 A            3 2 1 x x x x             1 5 10 b Let :
  • 26. Solutions to Systems of Linear Equations (con’t…) • Solution by Matrix Inverse: Ax = b A-1Ax = A-1b x = A-1b • MATLAB: >> A = [ 3 2 -1; -1 3 2; 1 -1 -1]; >> b = [ 10; 5; -1]; >> x = inv(A)*b x = -2.0000 5.0000 -6.0000 Answer: x1 = -2, x2 = 5, x3 = -6 • Solution by Matrix Division: The solution to the equation Ax = b can be computed using left division. Answer: x1 = -2, x2 = 5, x3 = -6 NOTE: left division: Ab  b  A right division: x/y  x  y  MATLAB: >> A = [ 3 2 -1; -1 3 2; 1 -1 -1]; >> b = [ 10; 5; -1]; >> x = Ab x = -2.0000 5.0000 -6.0000
  • 27. • The input function displays a prompt string in the Command Window and then waits for the user to respond. my_val = input( ‘Enter an input value: ’ ); in1 = input( ‘Enter data: ’ ); in2 = input( ‘Enter data: ’ ,`s`); Initializing with Keyboard Input
  • 28. How to display data The disp( ) function >> disp( 'Hello' ) Hello >> disp(5) 5 >> disp( [ 'Bilkent ' 'University' ] ) Bilkent University >> name = 'Alper'; >> disp( [ 'Hello ' name ] ) Hello Alper
  • 29. Plotting • For more information on 2-D plotting, type help graph2d • Plotting a point: >> plot ( variablename, ‘symbol’) the function plot () creates a graphics window, called a Figure window, and named by default “Figure No. 1”  Example : Complex number >> z = 1 + 0.5j; >> plot (z, ‘.’)
  • 31. Built-in MATLAB Functions result = function_name( input ); – abs, sign – log, log10, log2 – exp – sqrt – sin, cos, tan – asin, acos, atan – max, min – round, floor, ceil, fix – mod, rem • help elfun  help for elementary math functions
  • 32. Selection Programming • Flow Control • Loops
  • 33. Flow Control • Simple if statement: if logical expression commands end • Example: (Nested) if d <50 count = count + 1; disp(d); if b>d b=0; end end • Example: (else and elseif clauses) if temperature > 100 disp (‘Too hot – equipment malfunctioning.’) elseif temperature > 90 disp (‘Normal operating range.’); elseif (‘Below desired operating range.’) else disp (‘Too cold – turn off equipment.’) end
  • 34. Flow Control (con’t…) • The switch statement: switch expression case test expression 1 commands case test expression 2 commands otherwise commands end • Example: switch interval < 1 case 1 xinc = interval /10; case 0 xinc = 0.1; end
  • 35. Loops • for loop for variable = expression commands end • while loop while expression commands end •Example (for loop): for t = 1:5000 y(t) = sin (2*pi*t/10); end •Example (while loop): EPS = 1; while ( 1+EPS) >1 EPS = EPS/2; end EPS = 2*EPS  the break statement break – is used to terminate the execution of the loop.
  • 36. M-Files • The M-file is a text file that consists a group of MATLAB commands. • MATLAB can open and execute the commands exactly as if they were entered at the MATLAB command window. • To run the M-files, just type the file name in the command window. (make sure the current working directory is set correctly) All MATLAB commands are M-files. So far, we have executed the commands in the command window. But a more practical way is to create a M-file.
  • 37. User-Defined Function • Add the following command in the beginning of your m-file: function [output variables] = function_name (input variables); NOTE: the function_name should be the same as your file name to avoid confusion.  calling your function: -- a user-defined function is called by the name of the m-file, not the name given in the function definition. -- type in the m-file name like other pre-defined commands.  Comments: -- The first few lines should be comments, as they will be displayed if help is requested for the function name. the first comment line is reference by the lookfor command.
  • 38. Specific Topics • This tutorial gives you a general background on the usage of MATLAB. • There are thousands of MATLAB commands for many different applications, therefore it is impossible to cover all topics here. • For a specific topic relating to a class, you should consult the TA or the Instructor.
  • 40. Topics • Introduction • MATLAB Environment • Getting Help • Variables • Vectors, Matrices, and Linear Algebra • Mathematical Functions and Applications • Plotting • Selection Programming • M-Files • User Defined Functions • Specific Topics
  翻译: