Documente Academic
Documente Profesional
Documente Cultură
Computational Mechanics
A introduction to MATLAB 1 / 61
Outline I
1 Introduction
Key Features
Desktop Basics
2 Language Fundamentals
Matrices and Arrays
Expressions
Entering Commands
Indexing
Types of Arrays
3 Mathematics
Linear Algebra
Data Analysis
4 Graphics
Basic Plotting Functions
Mesh and Surface plots
A introduction to MATLAB 2 / 61
Outline II
5 Programming
Control Flow
Scripts and Functions
A introduction to MATLAB 3 / 61
Introduction Key Features
A introduction to MATLAB 4 / 61
Introduction Key Features
Key Features
A introduction to MATLAB 5 / 61
Introduction Desktop Basics
Default Layout
Current Folder:
Access your files.
Command Window:
Enter commands (»).
Workspace: Explore
data (variables, arrays,
matrices, strings, ecc. )
Command History:
View or rerun
commands entered in
the command line.
A introduction to MATLAB 6 / 61
Introduction Desktop Basics
a=1
b=2
c =a+b
d = sin(a)
e = c ∗ b;
A introduction to MATLAB 8 / 61
Language Fundamentals
In this section...
Main Topics
Matrices and Arrays
Expressions
Entering Commands
Indexing
Types of Arrays
A introduction to MATLAB 9 / 61
Language Fundamentals Matrices and Arrays
Entering Matrices
A introduction to MATLAB 10 / 61
Language Fundamentals Matrices and Arrays
A introduction to MATLAB 11 / 61
Language Fundamentals Matrices and Arrays
A introduction to MATLAB 12 / 61
Language Fundamentals Matrices and Arrays
Generate Matrices
A introduction to MATLAB 13 / 61
Language Fundamentals Expressions
Variables
A introduction to MATLAB 14 / 61
Language Fundamentals Expressions
Numbers
MATLAB stores all numbers internally using the long format (15 sig-
nificant digits) and it is able to represent number from about 10−308 to
10+308 .
Numbers precision
A introduction to MATLAB 15 / 61
Language Fundamentals Expressions
Matrices Arrays
A introduction to MATLAB 16 / 61
Language Fundamentals Expressions
Building tables
A introduction to MATLAB 17 / 61
Language Fundamentals Expressions
Built-in Functions
Functions Constants
Special Values
A introduction to MATLAB 18 / 61
Language Fundamentals Entering Commands
The format function controls how the number are displayed, not how
they are computed.
A introduction to MATLAB 19 / 61
Language Fundamentals Entering Commands
Long Statements
Suppressing Output
A introduction to MATLAB 20 / 61
Language Fundamentals Indexing
Indices
A introduction to MATLAB 21 / 61
Language Fundamentals Indexing
A introduction to MATLAB 22 / 61
Language Fundamentals Indexing
Concatenation
The concatenation is the process of joining small matrices to make bigger ones.
A introduction to MATLAB 23 / 61
Language Fundamentals Indexing
A introduction to MATLAB 24 / 61
Language Fundamentals Indexing
Scalar Expansion
Matrices and scalars can be combined in several ways. A scalar can be added to or
subtracted from a matrix, or assigned to a portion of it.
A introduction to MATLAB 25 / 61
Language Fundamentals Indexing
A introduction to MATLAB 26 / 61
Language Fundamentals Types of Arrays
Multidimensional Arrays
In the case of a 3D array, the generic element of the matrix A is denoted by A(i, j, k ).
A introduction to MATLAB 27 / 61
Language Fundamentals Types of Arrays
s is a matrix 1x5 of
char .
’s’ is handled as
arrays of numbers.
It is possible to
concatenate strings
both horizontally or
vertically.
It is possible to
manipulate
multi-lines text
A introduction to MATLAB 28 / 61
Language Fundamentals Types of Arrays
Structures
A introduction to MATLAB 29 / 61
Mathematics
In this section...
Main Topics
Linear Algebra
Data Analysis
A introduction to MATLAB 30 / 61
Mathematics Linear Algebra
A introduction to MATLAB 31 / 61
Mathematics Linear Algebra
A introduction to MATLAB 32 / 61
Mathematics Linear Algebra
Cholesky Factorization
Matrix A has to be
symmetric and
positive definite.
AX = B Matrix R is upper
triangular.
The backslash
A = R0R
operator recognizes
triangular matrices,
therefore the
X = R(R 0 B)
system solution can
be easily obtained.
A introduction to MATLAB 33 / 61
Mathematics Linear Algebra
LU Factorization
AX = B A is a generic square
matrix.
P is a permutation
PA = LU matrix.
L is a lower triangular
matrix with ones on its
A = P −1 LU diagonal.
U is an upper triangular
matrix.
X = U(P −1 LB)
A introduction to MATLAB 34 / 61
Mathematics Linear Algebra
Computational Cosiderations
Although it is not standard mathematical notation, MATLAB uses the
division terminology familiar in the scalar case to describe the solution
of a general system of equations.
A introduction to MATLAB 35 / 61
Mathematics Linear Algebra
Square Matrices
If A is symmetric and has real, positive diagonal element,
MATLAB performs a Cholesky factorization.
If A is upper Hessemberg, MATLAB uses Gaussian elimination.
In general, MATLAB uses the LU factorization algorithm.
A introduction to MATLAB 36 / 61
Mathematics Linear Algebra
A is a square
matrix.
Av = λv V is the matrix
whose columns are
the eigenvectors.
A = V ΛV −1 Λ is the matrix
containing
eigenvalues on its
diagonal.
A introduction to MATLAB 37 / 61
Graphics
In this section...
Main Topics
Basic Plotting Functions
Mesh and Surface Plots
A introduction to MATLAB 38 / 61
Graphics Basic Plotting Functions
A simple plot
It is possible to save
figures and to plot them.
Commands below the
menu bar allow the user to
zoom and pan the view.
A introduction to MATLAB 39 / 61
Graphics Basic Plotting Functions
Multiple plots
A introduction to MATLAB 40 / 61
Graphics Basic Plotting Functions
MATLAB allows the user to specify the line style and the marker shape.
A introduction to MATLAB 41 / 61
Graphics Basic Plotting Functions
The hold command enables the user to add plots on the active figure.
A introduction to MATLAB 42 / 61
Graphics Basic Plotting Functions
Figures
Graphic functions (like plot) automatically create a new figure. If different figures are
open, MATLAB plots on the last one (i.e. the active figure).
figure(n)
clf reset
A introduction to MATLAB 43 / 61
Graphics Basic Plotting Functions
Subplotting
A introduction to MATLAB 44 / 61
Graphics Basic Plotting Functions
Limits
The axis command allow the user to specify custom plotting limits.
axis auto
Aspect Ratio
The command:
axis square
axis equal
Axes Visibility
The command
axis off
Grid
The command:
grid on
A introduction to MATLAB 46 / 61
Graphics Mesh and Surface plots
Mesh Command
The mesh command produces a wireframe that color only the lines
connecting the points.
Surf Command
The surf command displays both connecting lines and faces of the
surface in color.
A introduction to MATLAB 47 / 61
Graphics Mesh and Surface plots
A introduction to MATLAB 48 / 61
Graphics Mesh and Surface plots
Meshgrid
A introduction to MATLAB 49 / 61
Graphics Mesh and Surface plots
Transparent Surfaces
A introduction to MATLAB 50 / 61
Graphics Mesh and Surface plots
A introduction to MATLAB 51 / 61
Programming Control Flow
The editor
The text editor of MATLAB is the tool used for coding. To open it, digit edit in the com-
mand window. Differently from the command window, the editor environment is not
interactive. Therefore the user can write some lines of code and then execute them by
clicking the run button.
A introduction to MATLAB 52 / 61
Programming Control Flow
A introduction to MATLAB 54 / 61
Programming Control Flow
A introduction to MATLAB 55 / 61
Programming Control Flow
The simple script below find a root of the equation x 3 − 2x − 5 with the bisection
method.
A introduction to MATLAB 56 / 61
Programming Control Flow
Continue
The continue statement passes control to the next iteration inside a
for or a while loop, skipping everything appears in the lines below it.
Break
The break statement exit from the loop. In nested loops, it exit form
the inner loop only.
Return
The return statement terminates the sequence of commands.
Everything after is not executed.
A introduction to MATLAB 57 / 61
Programming Scripts and Functions
Scripts
Scripts program do not accept input arguments and do not return
anything. They operate in data present in the workspace.
Functions
Functions accept input arguments and return output arguments.
Internal variables are local, therefore they are not stored in the
workspace.
A introduction to MATLAB 58 / 61
Programming Scripts and Functions
Functions
The simple function below write a ’.txt’ file containing values from a matrix.
A introduction to MATLAB 59 / 61
Programming Scripts and Functions
Functions
Once the function is saved in the working directory with its name in a ’.m’ file, it is
callable from another functions/scripts or from the command window.
A introduction to MATLAB 60 / 61
Programming Scripts and Functions
Functions
A introduction to MATLAB 61 / 61