Sunteți pe pagina 1din 50

MATLAB Lecture Note (1/2)

MATLAB Programming
11
Contents
Introduction
Matrices and Arrays
Graphics
22
I. Introduction - Overview
MATLAB : High-performance language for technical computing
Computation, visualization, programming p , , p g g
Problem and solution are expressed in math notation
Typical uses
Math and computation
Algorithm development
Data acquisition Data acquisition
Modeling, simulation, and visualization
Scientific and engineering graphics
Application development, including graphical UI building
Basic data element
Array : not require dimensioning
Allow to solve problem with matrix and vector formulations
33
I. Introduction - Overview
Desktop tool and development environment
Set of tools and facilities
Graphical UI : MATLAB desktop, Command Windows, editor, debugger
Mathematics Function Library
Collection of computational algorithm : sum, sine, matrix functions
Language
High-level matrix/array language with flow, functions, structure
Graphics
Extensive facilities for displaying vectors and matrices as graphs
High-level functions for 2-D and 3-D data visualization
External Interfaces
Allows to write C and Fortran programs that interact with MATLAB
44
I. Introduction Starting & Quitting
Stating the MATLAB program
Start the MATLAB program p g
by double-click in the desktop
Automatically loading
Tools for managing Tools for managing
files, variables and applications
Quitting the MATLAB program
To end MATLAB, File > Exit MATLAB in the desktop
Type quit in the Command Window
55
II. Matrices and Arrays - Matrices
Matrices
Rectangular array of numbers in MATLAB environment g y
Follow basic conventions to enter matrices
Separate the elements of a row with blanks or commas.
Use a semicolon ; to indicate the end of each row Use a semicolon, ; , to indicate the end of each row
Surround the entire list of elements with square brackets, [ ].
66
II. Matrices and Arrays - Matrices
Sum, Transpose, and Diag
Sum : row vector containing the sums of the columns of A g
Transpose : using apostrophe operation (A)
Diag : elements on the main diagonal using diag function
77
II. Matrices and Arrays - Matrices
Subscripts
Element in row i and column j of A : denoted by A(i,j). j y ( ,j)
A(4,2) : the number in the fourth row and second column
Magic function
MATLAB actually has a built-in function that creates magic squares of
almost any size almost any size.
88
II. Matrices and Arrays - Matrices
Colon operation
The colon, :, is one of the most important MATLAB operators. , , p p
1: 10 a row vector containing the integers from 1 to 10
100:-7:50 to obtain nonunit spacing, specify an increment.
0:pi/4:pi
A(1:k,j) the first k elements of the jth column of A
99
II. Matrices and Arrays - Expressions
Variables
MATLAB does not require any type declarations or dimension q y yp
statements.
creates a 1-by-1 matrix named num_students and stores the value 25
in its single element.
Numbers
conventional decimal notation.
optional decimal point and leading plus or minus sign for numbers optional decimal point and leading plus or minus sign, for numbers
Scientific notation uses the letter e
Imaginary numbers use either i or j as a suffix
10 10
II. Matrices and Arrays - Expressions
Numbers complex number
Real part and imaginary part p g y p
Magnitude and phase
11 11
II. Matrices and Arrays - Expressions
Operators
Expressions use familiar arithmetic operators and precedence rules. p p p
12 12
II. Matrices and Arrays - Expressions
Functions in MATLAB
Standard elementary mathematical functions (abs, sqrt, exp, sin) y ( , q , p, )
For a list of the elementary functions help elfun
Advanced mathematical functions (Bessel and gamma function)
For a list of more advanced mathematical and matrix functions For a list of more advanced mathematical and matrix functions
help specfun, help elmat
abs(x)
sign(x)
exp(x) p( )
log(x)
log10(x)
t( ) sqrt(x)
rem(x)
13 13
II. Matrices and Arrays - Expressions
Values of useful constants
Several special functions provide values of useful constants. p p
14 14
II. Matrices and Arrays - Expressions
Examples of Expression
more examples using several examples of MATLAB expressions p g p p
15 15
II. Matrices and Arrays Working with Matrices
Generating Matrices
Four functions to generate basic matrices g
Some examples
16 16
II. Matrices and Arrays Working with Matrices
Load function
Read the binary files or text files from outside of MALTAB y
M-files M files
Create your own matrices using M-files, including MATLAB code
Save the file under a mane that ends in .m
Concatenation
process of joining small matrices to make bigger ones
17 17
II. Matrices and Arrays Working with Matrices
Deleting rows and columns
delete rows and columns from a matrix using just a pair of square g j p q
brackets
18 18
II. Matrices and Arrays More about Matrices
Linear Algebra
The mathematical operations defined on matrices are the subject of p j
linear algebra.
matrix transpose : A matrix transpose : A
Adding a matrix : A+B
Multiplying : A*B
Determinant : det (A)
Inverse matrix : inv (A)
Eigenvalues : eig (A) g g ( )
Dividing : A/34
Repeated power : A^5
Coefficients in the characteristic polynomial of matrix Coefficients in the characteristic polynomial of matrix
: poly (A)
19 19
II. Matrices and Arrays More about Matrices
Arrays
Arithmetic operations on arrays are done element by element p y y
addition and subtraction are the same for arrays and matrices,
20 20
II. Matrices and Arrays Input and Output
Format function
Numeric format of the values displayed p y
21 21
II. Matrices and Arrays
Example on matrices and arrays
22 22
II. Matrices and Arrays
Example on matrices and arrays (cont)
23 23
II. Matrices and Arrays
Example on matrices and arrays
24 24
II. Matrices and Arrays
Example on matrices and arrays
25 25
III. Graphics Overview
Plotting process
The MATLAB environment provides a wide variety of techniques to p y q
display data graphically
Using interactive tools, to manipulate graphs
Annotate and print graphs for presentations Annotate and print graphs for presentations,
Export graphs to standard graphics formats for presentation in Web
browsers or other media.
Creating a Graph
Exploring Data
Editing the Graph Components
Annotating Graphs
Printing and Exporting Graphs Printing and Exporting Graphs
Adding and Removing Figure Content
Saving Graphs for Reuse
26 26
III. Graphics Overview
Graph components
Basic components of typical graph p yp g p
27 27
III. Graphics Overview
Figure tools
Set of tools to operate on graphs, access to many graph tools p g p , y g p
28 28
III. Graphics Overview
Figure tools Accessing the tools
Access or remove the figure and plotting toolbars from View menu g p g
Figure tools - Figure toolbars g g
easy access to many graph modification features
29 29
III. Graphics Overview
Figure tools Plotting tools
create an environment for creating graphs g g p
Select from a wide variety of graph types.
change the type of graph to represent a variable.
See and set the properties of graphics objects See and set the properties of graphics objects.
Annotate graphs with text, arrows, etc.
Create and arrange subplots in the figure.
Drag and drop data into graphs Drag and drop data into graphs.
30 30
III. Graphics Overview
Figure tools Plotting tools
create an environment for creating graphs g g p
31 31
III. Graphics Plotting Functions
Creating plot
plot function plot (x), or plot (x, y) p p ( ), p ( , y)
depending on the input arguments.
32 32
III. Graphics Plotting Functions
Example on creating plot
33 33
III. Graphics Plotting Functions
Plotting multiple data sets in one graphs
Multiple x-y pair arguments create multiple graphs with plot p y p g p g p p
Predefine the list of colors for discrimination among sets of data
34 34
III. Graphics Plotting Functions
Specifying line styles and colors
It is possible to specify color, line styles, and markers p p y , y ,
35 35
III. Graphics Plotting Functions
Plotting line and markers
plot (x, y, ks) : black squares p ( , y, ) q
plot(x, y, r:+) : red-dotted line and places plus sign markers
36 36
III. Graphics Plotting Functions
Adding plots to an existing graph
hold command enables you to add plots to an existing graph y p g g p
New data to the current graph, rescaling the axes if necessary.
The hold on command combines the pcolor plot with the contour plot The hold on command combines the pcolor plot with the contour plot
in one figure.
37 37
III. Graphics Plotting Functions
Figure windows
Graphing functions automatically open a new figure window if there p g y p g
are no figure windows already on the screen.
To make an existing figure window (current figure),
where n is the number in the figure title bar
To open a new figure window and make it the current figure
Cl i th fi i d f l t Clearing the figure windows for new plot
If you have set any figure properties in the previous plot,
38 38
III. Graphics Plotting Functions
Displaying multiple plots in one figure
subplot command enables you to display multiple plots in the same p y p y p p
window :
subplot(m,n,p) : partitions the figure window into an m-by-n matrix
of small subplots and selects the pth subplot for the current plot. p p p p
39 39
III. Graphics Plotting Functions
Controlling the axes setting axis limits
axis command enables to specify your own limits: p y y
Controlling the axes setting axis aspect ratio
axis command enables to specify a number of predefined modes
axis square : makes the x-axis and y-axis the same length. axis square : makes the x axis and y axis the same length.
axis equal : makes the individual tick mark increments on the x-axes
and y-axes the same length
axis auto normal : returns the axis scaling to its default automatic axis auto normal : returns the axis scaling to its default automatic
mode.
40 40
III. Graphics Plotting Functions
Controlling the axes setting axis visibility
You can use the axis command to make the axis visible or
invisible.
axis on : makes the axes visible.
i ff k h i i ibl axis off : makes the axes invisible.
Controlling the axes setting grid lines Controlling the axes setting grid lines
The grid command toggles grid lines on and off.
Grid on : turns the grid lines on,
Grid off : turns them back off
41 41
III. Graphics Plotting Functions
Adding axis labels and titles
xlabel', ylabel, and zlabel' commands add x-, y-, and z-axis labels. , y , , y ,
title command adds a title at the top of the figure
text function inserts text anywhere in the figure.
42 42
III. Graphics Plotting Functions
Example
43 43
III. Graphics Plotting Functions
Example
44 44
III. Graphics Plotting Functions
Example on subplot
45 45
III. Graphics Plotting Functions
Example on Sinusoidal Signal (Time-domain analysis)
46 46
III. Graphics Mesh and Surface Plots
Mesh and Surface plots
The mesh and surf plotting functions display surfaces in three p g p y
dimensions.
mesh produces wireframe surfaces that color only the lines
connecting the defining points. g g p
surf displays both the connecting lines and the faces of the surface
in color.
Visualizing Functions of two variables
To display a function of two variables, z = f (x, y),
1. Generate X and Y matrices consisting of repeated rows and
columns, respectively, over the domain of the function.
2. Use X and Y to evaluate and graph the function. 2. Use X and Y to evaluate and graph the function.
47 47
III. Graphics Mesh and Surface Plots
Example graphing the sinc function
graphs the two-dimensional sinc function, sin(r)/r, between the x and g p , ( ) ,
y directions.
48 48
III. Graphics Mesh and Surface Plots
Example colored surface plots
The rectangular faces of the surface are colored g
The color of each face is determined by the values of Z and the
colormap
49 49
Thank you Thank you
50 50

S-ar putea să vă placă și