SlideShare a Scribd company logo
1 of 28
Introduction to MATLAB
January 18, 2008
Steve Gu
Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Part I: Basics
• MATLAB Environment
• Getting Help
• Variables
• Vectors, Matrices, and Linear Algebra
• Flow Control / Loops
Display Windows
Display Windows (con’t…)
• Graphic (Figure) Window
– Displays plots and graphs
• E.g: surf(magic(30))
– 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 command – provides help for the
specified command
• help help – provides information on use of the
help command
– Google… of course
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.
• 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
Vectors, Matrices and Linear Algebra
• Vectors
• Matrices
• Solutions to Systems of Linear Equations.
Vectors
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
linspace(start,end,number) create row vector x starting with start, ending at end,
having number elements
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.
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
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…)
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
Flow Control: If…Else
Example: (if…else and elseif clauses)
if temperature > 100
disp (‘Too hot – equipment malfunctioning.’)
elseif temperature > 90
disp (‘Normal operating range.’);
else
disp (‘Too cold – turn off equipment.’)
end
Flow Control: 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.
Part II: Visualization
Visualization: Plotting
• Example:
>> s = linspace (-5, 5, 100);
>> coeff = [ 1 3 3 1];
>> A = polyval (coeff, s);
>> plot (s, A),
>> xlabel ('s')
>> ylabel ('A(s)')
A(s) = s3 + 3s2 + 3s + 1
Plotting (con’t)
 Plot a Helix
t = linspace (-5, 5, 101);
x = cos(t);
y = sin(t);
z = t
plot3(x,y,z);
box on;
Advanced Visualization
Part III: Modelling Vibrations
Second Order Difference Equations
Modelling Vibrations
The equation for the motion:
Remark: Second Order Difference Equation
Modelling Vibrations
• How to use MATLAB to compute y?
• Let’s Do It !
Modelling Vibrations
Results

More Related Content

Similar to matlab_tutorial.ppt

Similar to matlab_tutorial.ppt (20)

Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
presentation.pptx
presentation.pptxpresentation.pptx
presentation.pptx
 
Matlab1
Matlab1Matlab1
Matlab1
 
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docxMATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
 
Matlab Tutorial
Matlab TutorialMatlab Tutorial
Matlab Tutorial
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
EPE821_Lecture3.pptx
EPE821_Lecture3.pptxEPE821_Lecture3.pptx
EPE821_Lecture3.pptx
 
Intro to MATLAB and K-mean algorithm
Intro to MATLAB and K-mean algorithmIntro to MATLAB and K-mean algorithm
Intro to MATLAB and K-mean algorithm
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 
Basic concepts in_matlab
Basic concepts in_matlabBasic concepts in_matlab
Basic concepts in_matlab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Matlab
MatlabMatlab
Matlab
 
Es272 ch1
Es272 ch1Es272 ch1
Es272 ch1
 

More from aboma2hawi

MATLAB_CIS601-03.ppt
MATLAB_CIS601-03.pptMATLAB_CIS601-03.ppt
MATLAB_CIS601-03.pptaboma2hawi
 
Introduction_to_Matlab_lecture.pptx
Introduction_to_Matlab_lecture.pptxIntroduction_to_Matlab_lecture.pptx
Introduction_to_Matlab_lecture.pptxaboma2hawi
 
programming_tutorial_course_ lesson_1.pptx
programming_tutorial_course_ lesson_1.pptxprogramming_tutorial_course_ lesson_1.pptx
programming_tutorial_course_ lesson_1.pptxaboma2hawi
 
HDP Module One (1).pptx
HDP Module One (1).pptxHDP Module One (1).pptx
HDP Module One (1).pptxaboma2hawi
 
5_2019_01_12!09_25_57_AM.ppt
5_2019_01_12!09_25_57_AM.ppt5_2019_01_12!09_25_57_AM.ppt
5_2019_01_12!09_25_57_AM.pptaboma2hawi
 
10.1.1.1039.4745
10.1.1.1039.474510.1.1.1039.4745
10.1.1.1039.4745aboma2hawi
 
10.1.1.193.2962
10.1.1.193.296210.1.1.193.2962
10.1.1.193.2962aboma2hawi
 
Step response plot of dynamic system; step response data matlab step
Step response plot of dynamic system; step response data   matlab stepStep response plot of dynamic system; step response data   matlab step
Step response plot of dynamic system; step response data matlab stepaboma2hawi
 
Lab 4 matlab for controls state space analysis
Lab 4   matlab for controls   state space analysisLab 4   matlab for controls   state space analysis
Lab 4 matlab for controls state space analysisaboma2hawi
 

More from aboma2hawi (14)

MATLAB_CIS601-03.ppt
MATLAB_CIS601-03.pptMATLAB_CIS601-03.ppt
MATLAB_CIS601-03.ppt
 
Introduction_to_Matlab_lecture.pptx
Introduction_to_Matlab_lecture.pptxIntroduction_to_Matlab_lecture.pptx
Introduction_to_Matlab_lecture.pptx
 
programming_tutorial_course_ lesson_1.pptx
programming_tutorial_course_ lesson_1.pptxprogramming_tutorial_course_ lesson_1.pptx
programming_tutorial_course_ lesson_1.pptx
 
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
HDP Module One (1).pptx
HDP Module One (1).pptxHDP Module One (1).pptx
HDP Module One (1).pptx
 
5_2019_01_12!09_25_57_AM.ppt
5_2019_01_12!09_25_57_AM.ppt5_2019_01_12!09_25_57_AM.ppt
5_2019_01_12!09_25_57_AM.ppt
 
08822428
0882242808822428
08822428
 
08764396
0876439608764396
08764396
 
109 me0422
109 me0422109 me0422
109 me0422
 
10.1.1.1039.4745
10.1.1.1039.474510.1.1.1039.4745
10.1.1.1039.4745
 
10.1.1.193.2962
10.1.1.193.296210.1.1.193.2962
10.1.1.193.2962
 
Step response plot of dynamic system; step response data matlab step
Step response plot of dynamic system; step response data   matlab stepStep response plot of dynamic system; step response data   matlab step
Step response plot of dynamic system; step response data matlab step
 
Lab 4 matlab for controls state space analysis
Lab 4   matlab for controls   state space analysisLab 4   matlab for controls   state space analysis
Lab 4 matlab for controls state space analysis
 

Recently uploaded

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 

Recently uploaded (20)

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 

matlab_tutorial.ppt

  • 1. Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
  • 2. Part I: Basics • MATLAB Environment • Getting Help • Variables • Vectors, Matrices, and Linear Algebra • Flow Control / Loops
  • 4. Display Windows (con’t…) • Graphic (Figure) Window – Displays plots and graphs • E.g: surf(magic(30)) – Created in response to graphics commands. • M-file editor/debugger window – Create and edit scripts of commands called M-files.
  • 5. Getting Help • type one of following commands in the command window: – help – lists all the help topic – help command – provides help for the specified command • help help – provides information on use of the help command – Google… of course
  • 6. 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. • 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.
  • 7. 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
  • 8. Vectors, Matrices and Linear Algebra • Vectors • Matrices • Solutions to Systems of Linear Equations.
  • 9. Vectors 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.
  • 10. 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
  • 11. 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 linspace(start,end,number) create row vector x starting with start, ending at end, having number elements 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.
  • 12. 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 the main diagonal
  • 13. 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
  • 14. 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.
  • 15. 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 :
  • 16. 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
  • 17. Flow Control: If…Else Example: (if…else and elseif clauses) if temperature > 100 disp (‘Too hot – equipment malfunctioning.’) elseif temperature > 90 disp (‘Normal operating range.’); else disp (‘Too cold – turn off equipment.’) end
  • 18. Flow Control: 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.
  • 20. Visualization: Plotting • Example: >> s = linspace (-5, 5, 100); >> coeff = [ 1 3 3 1]; >> A = polyval (coeff, s); >> plot (s, A), >> xlabel ('s') >> ylabel ('A(s)') A(s) = s3 + 3s2 + 3s + 1
  • 21. Plotting (con’t)  Plot a Helix t = linspace (-5, 5, 101); x = cos(t); y = sin(t); z = t plot3(x,y,z); box on;
  • 23. Part III: Modelling Vibrations Second Order Difference Equations
  • 24. Modelling Vibrations The equation for the motion: Remark: Second Order Difference Equation
  • 25. Modelling Vibrations • How to use MATLAB to compute y? • Let’s Do It !
  • 27.