Introduction To Matlab: Victoria Lapuerta Ana Laverón
Introduction To Matlab: Victoria Lapuerta Ana Laverón
MATLAB
Victoria Lapuerta
Ana Laverón
Index
2D and 3D graphics (I)
Introducción
2D and 3D graphics (II)
Basic elements of Matlab’s
desktop 2D and 3D graphics (III)
Creating movies
MATLAB editor
Matlab files
Numbers and operations Functions for functions
Vectors and matrices Programming
Operations with vectors and Numerical analysis
matrices Exercices
Functions for vectors and
matrices
Data Input and output
Data structures and cell
matrices
Polynomials
Introduction
What is Matlab? MATrix LABoratory.
Current
directory
Command
Windows
Command
History
Basic elements of Matlab’s desktop
Some comments about the command window
Moving around the command line is possible with left / right arrow
keys → ←. Go to the beginning of the line with Inicio (Home) and
to the end with Fin (End). Esc deletes the whole line.
Use “main_” for the name of the main programs, for example:
main_curvature
Write “;” at the end of a line If you don’t want that the intermediate
calculus is written in the window while the program is running
Write “…” at the end of a line if you are writing a very long statement
and you want to continue in the next line
Matlab editor
Debugger
Set/Clear breakingpoint: Sets or clears a break point in the
line the cursor is placed.
Clear all breakingpoints: Deletes all breaking points.
Step in: Executes the current line of the program, if the line
calls to a function, steps into the function.
Variables are defined with the assignment operator “=“. MATLAB is dynamically typed,
meaning that variables can be assigned without declaring their type, and that their
type can change. There is no need to define variables as integers, reals, etc, as in
other languages
Integers: a=2
Reals: x=-35.2
Maximum 19 significant figures
2.23e-3=2.23*10-3
Precision and formats: By defect, it uses a short format defect, but other formats
can be used:
>> format long (14 significant figures)
>> format short (5 significant figures)
>> format short e (exponential notation)
>> format long e (exponential notation)
>> format rat (rational approximation)
Information about the variables used and their dimensions (if they’re
matrices): Workspace. Also typing
>> who
>> whos (gives more information)
Addition: +, Substraction -
Multiplication: *, Division: /
Power: ^
exp(x), log(x) (base e), log2(x) (base 2), log10(x) (base 10),
sqrt(x)
Example: main_number_operations.m
Vectors and matrices
Defining vectors:
Example: main_matrix_operations.m
Vectors and matrices
Defining matrices:
It’s not needed to define their size before hand (a size can be
defined and changed afterwards).
Example: main_matrix_operations.m
Vectors and matrices
Defining matrices:
Generating de matrices:
Example: main_matrix_operations.m
Operations with vectors and matrices
Operating vectors and matrices with scalars:
v: vector, k: scalar:
v+k addition
v-k sustraction
v*k product
v/k divides each element of v by k
k./v divides k by each element of v
v.^k powers each element of v to the k-power
k.^v powers k to each element of v
Example: main_matrix_operations.m
Operations with vectors and matrices
Operating vectors and matrices
+ addition
– subtraction
* matrix product
.* product element by element
^ power
.^ power element by element
\ left-division
/ right-division
./ y .\ right and left division element by element
Transposed matrix: B=A’ (in complex numbers, it returns the
conjugated transposed, to get only the trasposed: B=A.’)
Example: main_matrix_operations.m
Functions for vectors and matrices
sum(v) adds the elements of a vector
Example: main_matrix_operations.m
Data input and output
Example: main_matrix_operations.m
Data structures and cell matrices
Matlab allows store variables with a tree structure:
subdato31 subdato32
Example: main_data_structure.m
Polynomials
Polynomials are written in Matlab as a row vector whose dimension
is n+1, n being the degree of the polynomial.
Example: x3+2x-7 is written:
>> pol1= 1 0 2 -7
Obtaining the roots: roots (returns a column vector, even though
pol1 is a row vector)
>>roots_data=roots(pol1)
Example: main_polynomials.m
Polynomials
Matlab functions for polynomials
2D: plot() creates a graphic from vectors, with linear scales on both
axes,
2D: fill(x,y,’option’) draws a closed curve and fills it with the color
indicated in ‘option’
Example: main_graphics.m
2D and 3D Graphics (I)
Selecting the axes scale
axis off: deactivates the axes labels. Axes, labels and grid disappear, axis
on: activates it again.
Manipulating graphics:
view(azimut, elev), view([xd,yd,zd])
Example: main_movie.m
Matlab Files
Program files: Scripts
They are built with a series of commands. The main file will be named
main_name.m
Function files
To create your own functions. They are called from within the scripts.
The first line is executable and starts with the word function as showed:
>> @reference_name=function_name
Function references are very useful to give a function to other functions. Functions
that execute other functions are called functions for functions.
Function references are variables of MATLAB, so they can be stored in matrices, for
example.
Example: main_functions_for_functions.m
Function for functions
Example: main_functions_for_functions.m
Programming
Loops
for k=n1:incre:n2
end
for k=vector_column
end
while
end
Example: main_loops
Programming
Conditional control structures
Logical operators:
>, <, >=,<=,== (equal)
| (or), &(and)
if
if
if elseif
else
end else
end
end
Example: main_conditional
Programming
Structures of control condicionated: switch
switch_expresion=case_expr3 %example
switch switch_expresion
case case_expr1,
actions1
case {case_expr2, case_expr3,case_expr4,...}
actions2
otherwise, % option by default
actions3
end
Example: main_conditional
Programming
Interpolation
1D:
A polynomial is defined (example, n=2, ax^2+bx+c),to
2D:
matrix_Z=interp2(X,Y,Z,matrix_X,matriz_Y,method). Methods:
[T,Y]=solver(@F,tspan,Y0)
3x+2y-z=1
5x+y+3z=-2
3y-4z=3