Scuola Universitaria Professionale Dipartimento
della Svizzera Italiana di informatica
ed elettronica Matlab
Introduzione a
Ing. Roberto Bucher
7 aprile 2003
Introduzione a Matlab
2 Copyright 2003 Roberto Bucher 7 aprile 2003
Indice
1 Introduzione 13
1.1 Origini . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.2 Piattaforme HW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.3 Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2 Basi 15
2.1 Introduzione . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2 Tipi di dati . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3 Indirizzamento di matrici . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4 Manipolazione di matrici . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5 Espressioni . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.6 Informazioni sul Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.7 Accesso al sistema operativo . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.8 Salvataggio e richiamo di dati . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.9 Formati dei numeri . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.10 Altre caratteristiche di base . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.11 Stringhe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3 L’Help in linea 25
3.1 Introduzione . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.2 Il comando help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.3 Il comando lookfor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4 Calcolo scientifico 29
4.1 Introduzione . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.2 Funzioni matematiche . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.3 Numeri complessi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5 Operazioni con matrici 33
5.1 Matematica tra scalari e matrici . . . . . . . . . . . . . . . . . . . . . . . . 33
5.2 Matematica tra matrici . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.3 Orientazione delle matrici . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.4 Operatori relazionali e logiche . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.4.1 Operatori relazionali . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.4.2 Operatori logici . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.5 Algebra lineare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7 aprile 2003 Copyright 2003 Roberto Bucher 3
Introduzione a INDICE
Matlab
6 Matrici sparse 41
7 Semplici script 43
8 Programmi e controllo di flusso 45
8.1 Programmi e funzioni . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
8.2 Controllo di flusso . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
8.2.1 Ciclo for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
8.2.2 Ciclo while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
8.2.3 Ciclo if-elseif-else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
8.2.4 Ciclo switch-case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
9 Analisi di dati 49
9.1 Introduzione . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
10 Polinomi 51
11 Interpolazione 55
12 Analisi numerica 57
12.1 Premessa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
12.2 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
12.3 Ricerca di minimi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
12.4 Ricerca di zeri . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
12.5 Integrazione e derivazione . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
13 Grafica 59
13.1 Introduzione . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
13.2 Grafica 2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
13.3 Il comando subplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
13.4 Trasformazione di coordinate . . . . . . . . . . . . . . . . . . . . . . . . . . 62
13.5 Grafica 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
13.6 Stampa di immagini . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
13.7 Animazioni . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
14 Equazioni differenziali 69
15 Input/Output 73
16 Debugger 77
17 Grafica avanzata (GUI) 81
18 Strutture particolari 89
18.1 Matrici multidimensionali . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
18.2 Celle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
18.3 Strutture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
4 Copyright 2003 Roberto Bucher 7 aprile 2003
INDICE Introduzione a Matlab
19 Programmazione orientata agli oggetti 95
19.1 Introduzione . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
19.2 Costruttori . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
19.3 Funzioni di conversione . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
19.4 Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
19.5 Ereditarietà e aggregazione . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
A Comandi di Matlab 101
A.1 Preferences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
A.1.1 Saved preferences files. . . . . . . . . . . . . . . . . . . . . . . . . . 101
A.1.2 Preference commands. . . . . . . . . . . . . . . . . . . . . . . . . . . 101
A.1.3 Configuration information. . . . . . . . . . . . . . . . . . . . . . . . 101
A.2 General purpose commands. . . . . . . . . . . . . . . . . . . . . . . . . . . 101
A.2.1 General information . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
A.2.2 Managing the workspace. . . . . . . . . . . . . . . . . . . . . . . . . 102
A.2.3 Managing commands and functions. . . . . . . . . . . . . . . . . . . 102
A.2.4 Managing the search path . . . . . . . . . . . . . . . . . . . . . . . 102
A.2.5 Controlling the command window. . . . . . . . . . . . . . . . . . . . 102
A.2.6 Operating system commands . . . . . . . . . . . . . . . . . . . . . . 102
A.2.7 Debugging M-files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
A.2.8 Profiling M-files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
A.3 Operators and special characters. . . . . . . . . . . . . . . . . . . . . . . . 103
A.3.1 Arithmetic operators. . . . . . . . . . . . . . . . . . . . . . . . . . . 103
A.3.2 Relational operators. . . . . . . . . . . . . . . . . . . . . . . . . . . 103
A.3.3 Logical operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
A.3.4 Special characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
A.3.5 Bitwise operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
A.3.6 Set operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
A.4 Programming language constructs. . . . . . . . . . . . . . . . . . . . . . . . 105
A.4.1 Control flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
A.4.2 Evaluation and execution. . . . . . . . . . . . . . . . . . . . . . . . 105
A.4.3 Scripts, functions, and variables. . . . . . . . . . . . . . . . . . . . . 105
A.4.4 Argument handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
A.4.5 Message display. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
A.4.6 Interactive input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
A.5 Elementary matrices and matrix manipulation. . . . . . . . . . . . . . . . . 106
A.5.1 Elementary matrices. . . . . . . . . . . . . . . . . . . . . . . . . . . 106
A.5.2 Basic array information. . . . . . . . . . . . . . . . . . . . . . . . . 106
A.5.3 Matrix manipulation. . . . . . . . . . . . . . . . . . . . . . . . . . . 106
A.5.4 Special variables and constants. . . . . . . . . . . . . . . . . . . . . 107
A.5.5 Specialized matrices. . . . . . . . . . . . . . . . . . . . . . . . . . . 107
A.6 Elementary math functions. . . . . . . . . . . . . . . . . . . . . . . . . . . 107
A.6.1 Trigonometric. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
A.6.2 Exponential. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
A.6.3 Complex. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
A.6.4 Rounding and remainder. . . . . . . . . . . . . . . . . . . . . . . . . 109
A.7 Specialized math functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
7 aprile 2003 Copyright 2003 Roberto Bucher 5
Introduzione a INDICE
Matlab
A.7.1 Specialized math functions. . . . . . . . . . . . . . . . . . . . . . . . 109
A.7.2 Number theoretic functions. . . . . . . . . . . . . . . . . . . . . . . 109
A.7.3 Coordinate transforms. . . . . . . . . . . . . . . . . . . . . . . . . . 110
A.8 Matrix functions - numerical linear algebra. . . . . . . . . . . . . . . . . . . 110
A.8.1 Matrix analysis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
A.8.2 Linear equations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
A.8.3 Eigenvalues and singular values. . . . . . . . . . . . . . . . . . . . . 110
A.8.4 Matrix functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
A.8.5 Factorization utilities . . . . . . . . . . . . . . . . . . . . . . . . . . 111
A.9 Data analysis and Fourier transforms. . . . . . . . . . . . . . . . . . . . . . 111
A.9.1 Basic operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
A.9.2 Finite differences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
A.9.3 Correlation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
A.9.4 Filtering and convolution. . . . . . . . . . . . . . . . . . . . . . . . 112
A.9.5 Fourier transforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
A.9.6 Sound and audio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
A.9.7 Audio file inport/export. . . . . . . . . . . . . . . . . . . . . . . . . 112
A.10 Interpolation and polynomials. . . . . . . . . . . . . . . . . . . . . . . . . . 112
A.10.1 Data interpolation. . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
A.10.2 Spline interpolation. . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
A.10.3 Geometric analysis. . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
A.10.4 Polynomials. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
A.11 Function functions and ODE solvers. . . . . . . . . . . . . . . . . . . . . . 113
A.11.1 Optimization and root finding. . . . . . . . . . . . . . . . . . . . . . 113
A.11.2 Numerical integration (quadrature). . . . . . . . . . . . . . . . . . . 113
A.11.3 Plotting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
A.11.4 Inline function object. . . . . . . . . . . . . . . . . . . . . . . . . . . 114
A.11.5 Ordinary differential equation solvers. . . . . . . . . . . . . . . . . . 114
A.11.6 ODE Option handling. . . . . . . . . . . . . . . . . . . . . . . . . . 114
A.11.7 ODE output functions. . . . . . . . . . . . . . . . . . . . . . . . . . 114
A.12 Sparse matrices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
A.12.1 Elementary sparse matrices. . . . . . . . . . . . . . . . . . . . . . . 114
A.12.2 Full to sparse conversion. . . . . . . . . . . . . . . . . . . . . . . . . 114
A.12.3 Working with sparse matrices. . . . . . . . . . . . . . . . . . . . . . 115
A.12.4 Reordering algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . 115
A.12.5 Linear algebra. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
A.12.6 Linear Equations (iterative methods). . . . . . . . . . . . . . . . . . 115
A.12.7 Operations on graphs (trees). . . . . . . . . . . . . . . . . . . . . . . 115
A.12.8 Miscellaneous. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
A.13 Two dimensional graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
A.13.1 Elementary X-Y graphs. . . . . . . . . . . . . . . . . . . . . . . . . 116
A.13.2 Axis control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
A.13.3 Graph annotation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
A.13.4 Hardcopy and printing. . . . . . . . . . . . . . . . . . . . . . . . . . 116
A.14 Three dimensional graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
A.14.1 Elementary 3-D plots. . . . . . . . . . . . . . . . . . . . . . . . . . . 116
A.14.2 Color control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
6 Copyright 2003 Roberto Bucher 7 aprile 2003
INDICE Introduzione a Matlab
A.14.3 Lighting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
A.14.4 Color maps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
A.14.5 Axis control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
A.14.6 Viewpoint control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
A.14.7 Graph annotation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
A.14.8 Hardcopy and printing. . . . . . . . . . . . . . . . . . . . . . . . . . 118
A.15 Specialized graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
A.15.1 Specialized 2-D graphs. . . . . . . . . . . . . . . . . . . . . . . . . . 118
A.15.2 Contour and 2-1/2 D graphs. . . . . . . . . . . . . . . . . . . . . . . 118
A.15.3 Specialized 3-D graphs. . . . . . . . . . . . . . . . . . . . . . . . . . 119
A.15.4 Images display and file I/O. . . . . . . . . . . . . . . . . . . . . . . 119
A.15.5 Movies and animation. . . . . . . . . . . . . . . . . . . . . . . . . . 119
A.15.6 Color related functions. . . . . . . . . . . . . . . . . . . . . . . . . . 119
A.15.7 Solid modeling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
A.16 Handle Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
A.16.1 Figure window creation and control. . . . . . . . . . . . . . . . . . . 120
A.16.2 Axis creation and control. . . . . . . . . . . . . . . . . . . . . . . . 120
A.16.3 Handle Graphics objects. . . . . . . . . . . . . . . . . . . . . . . . . 120
A.16.4 Handle Graphics operations. . . . . . . . . . . . . . . . . . . . . . . 120
A.16.5 Hardcopy and printing. . . . . . . . . . . . . . . . . . . . . . . . . . 121
A.16.6 Utilities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
A.17 Graphical user interface tools. . . . . . . . . . . . . . . . . . . . . . . . . . 121
A.17.1 GUI functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
A.17.2 GUI design tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
A.17.3 Dialog boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
A.17.4 Menu utilities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
A.17.5 Toolbar button group utilities. . . . . . . . . . . . . . . . . . . . . . 122
A.17.6 User-defined figure/axes property utilities. . . . . . . . . . . . . . . 122
A.17.7 Miscellaneous utilities. . . . . . . . . . . . . . . . . . . . . . . . . . 122
A.18 Character strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
A.18.1 General. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
A.18.2 String tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
A.18.3 String operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
A.18.4 String to number conversion. . . . . . . . . . . . . . . . . . . . . . . 123
A.18.5 Base number conversion. . . . . . . . . . . . . . . . . . . . . . . . . 123
A.19 File input/output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
A.19.1 File opening and closing. . . . . . . . . . . . . . . . . . . . . . . . . 124
A.19.2 Binary file I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
A.19.3 Formatted file I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
A.19.4 String conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
A.19.5 File positioning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
A.19.6 File name handling . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
A.19.7 File import/export functions. . . . . . . . . . . . . . . . . . . . . . . 125
A.19.8 Image file import/export. . . . . . . . . . . . . . . . . . . . . . . . . 125
A.19.9 Audio file import/export. . . . . . . . . . . . . . . . . . . . . . . . . 125
A.19.10 Command window I/O . . . . . . . . . . . . . . . . . . . . . . . . . 125
A.20 Time and dates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
7 aprile 2003 Copyright 2003 Roberto Bucher 7
Introduzione a INDICE
Matlab
A.20.1 Current date and time. . . . . . . . . . . . . . . . . . . . . . . . . . 125
A.20.2 Basic functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
A.20.3 Date functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
A.20.4 Timing functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
A.21 Data types and structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
A.21.1 Data types (classes) . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
A.21.2 Multi-dimensional array functions. . . . . . . . . . . . . . . . . . . . 126
A.21.3 Cell array functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
A.21.4 Structure functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
A.21.5 Object oriented programming functions. . . . . . . . . . . . . . . . 127
A.21.6 Overloadable operators. . . . . . . . . . . . . . . . . . . . . . . . . . 127
A.22 Dynamic data exchange (DDE). . . . . . . . . . . . . . . . . . . . . . . . . 127
A.22.1 DDE Client Functions. . . . . . . . . . . . . . . . . . . . . . . . . . 127
A.23 Examples and demonstrations. . . . . . . . . . . . . . . . . . . . . . . . . . 128
A.23.1 MATLAB/Introduction. . . . . . . . . . . . . . . . . . . . .
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.