What is learned in the field will stay with you forever Matlab More on Matrices Functions Graphics Cannot use spaces in names of matrices variables everything in matlab is a matrix cool ID: 306211
Download Presentation The PPT/PDF document "Book material is easily memorized and ea..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Book material is easily memorized and easily forgotten.
What is learned in the field will stay with you forever.Slide2
Matlab
More on Matrices; Functions; GraphicsSlide3
Cannot
use spaces in names of matrices (variables, everything in
matlab
is a matrix)cool x = [1 2 3 4 5] Cannot use the dash sign (-) because it represents a subtraction.cool-x = [1 2 3 4 5] Don’t use a period (.) unless you want to created something call a structure.cool.x = [1 2 3 4 5]
Naming VariablesSlide4
Your
best option, is to use the underscore ( _ ) if you need to assign a long name to a matrix
my_cool_x = [1 2 3 4 5]Slide5
a =
1 4 3 0
0 0 0 5
>> size(a)ans = 2 4>> sizea=size(a);>> whos Name Size Bytes Class Attributes a 2x4 64 double ans 1x2 16 double sizea 1x2 16 double Dimension of matrix (mathematically)
Size of MatricesSlide6
>>
sizea
sizea
= 2 4>> size(a,1)ans = 2>> size(a,2)ans = 4>> length(a)ans = 4>> length(a(:))ans = 8Can do by individual dimensionsLinear size (as vector – amount memory)Slide7
>> rand(3)
ans
=
0.8147 0.9134 0.2785 0.9058 0.6324 0.5469 0.1270 0.0975 0.9575>> rand(3,1)ans = 0.9572 0.4854 0.8003>> eye(3)ans = 1 0 0 0 1 0 0 0 1Predefined Matrix Making ToolsAlso – ones, zeros, magic, hilbSlide8
pi - predefined to be 1.3146
i,
j
- predefined to be 0.0 + 1.000ieps – returns distance from 1.0 to the next largest double-precision number To see what variables are definedwho, who vari_name To clear variablesclear vari_name, clear (does all of them)Predefined ValuesSlide9
Here are a few. How they work is context sensitive.
max
min
summeanThese functions work on vectors, or columns for matrix input (matrix is treated like group of column vectors)Predefined FunctionsSlide10
Work element by element when appropriate
sin
cos
(Other trig fns)explogabs…Perform matrix operations(output can be same size matrix, different size matrix or matrices, scalar, other.)inveigtriutril…Slide11
Round/truncate
round(f
)
fix(f)ceil(f)floor(f)>> help round ROUND Round towards nearest integer. ROUND(X) rounds the elements of X to the nearest integers. >> help fix FIX Round towards zero. FIX(X) rounds the elements of X to the nearest integers towards zero.>> help ceil CEIL Round towards plus infinity. CEIL(X) rounds the elements of X to the nearest integers towards infinity.>> help floor FLOOR Round towards minus infinity. FLOOR(X) rounds the elements of X to the nearest integers towards minus infinity. Slide12
Graphics Basics
Types of Graphics
Predefined graph types
Create your own graphicsCreating a GraphUse plotting tools to create graphs interactivelyUse the command interface to enter commands in the Command Window or create plotting programs.Slide13Slide14
Creating a plot
The plot function has different forms, depending on the input arguments.
If
y is a vector, plot(y) produces a piecewise linear graph of the elements of y versus the index of the elements of y. If you specify two vectors as arguments, plot(x,y) produces a graph of y versus x.>>x = 0:pi/100:2*pi;>>y = sin(x);>>plot(x,y)Slide15
>>
xlabel
('x
= 0:2\pi')>>ylabel('Sine of x')>>title('Plot of the Sine Function','FontSize',12)Slide16
>>
seis
=loadsac('BJT.BHZ_00.Q.2005:01:23:41');
>> plot(seis)>> ylabel('Digital Counts')>> xlabel('Number of points')>> title('BJT BHZ Seismogram')Slide17
Plotting multiple data sets
Multiple
x-y
pair arguments create multiple graphs with a single call to plot, which automatically cycles through a predefined (but customizable) list of colors>>x = 0:pi/100:2*pi;>>y = sin(x);>>y2 = sin(x-.25);>>y3 = sin(x-.5);>>plot(x,y,x,y2,x,y3)>>legend('sin(x)','sin(x-.25)','sin(x-.5)')Slide18
>>
size(seis
)
25138 1>> tmp=size(seis,1);>> x=1:tmp;>> plot(x,seis,x,seis2)>> legend('Raw','Mean Removed’)Slide19
Specifying line colors/styles
It is possible to specify color, line styles, and markers (such as plus signs or circles) when you plot your data using the plot command:
>>
plot(x,y,'color_linestyle_markertype')Change color>> plot(x,seis,'r',x,seis2,'b’)>> plot(x,seis,'r',x,seis2,'b:')Slide20
Color
'
c
''m''y''r''g''b''w''k'cyanmagentayellowredgreenbluewhiteblackSlide21
Line style
'-'
'--'
':''.-'nocharactersoliddasheddotteddash-dotno lineSlide22
Specifying lines and markers
If you specify a marker type but not a line style, only the marker is drawn.
>>
plot(x,y,'ks’) #plots black squares at each data point, but does not connect the markers with a line>>plot(x,y,'r:+') #plots a red-dotted line and places plus sign markers at each data pointSlide23
>>x1 = 0:pi/100:2*pi;
>>x2 = 0:pi/10:2*pi;
>>plot(x1,sin(x1),'r:',x2,sin(x2),'r+')
#only plots the + every 10 pointsSlide24
Marker Type
'+'
'
o''*''x''s''d''^''v''>''<''p''h'no characterplus markunfilled circleasteriskletter xfilled squarefilled diamondfilled upward trianglefilled downward trianglefilled right-pointing trianglefilled left-pointing trianglefilled pentagramfilled hexagramno markerSlide25
Graphing imaginary and complex data
Reminder: complex numbers can be represented by the expression
a+bi
where a and b are real numbers and i is a symbol with the property i2=-1Complex numbers can be plotted using Real and Imaginary axes.Slide26
When the arguments to plot are complex, the imaginary part is ignored
except
when you pass plot a single complex argument. For this special case, the command is a shortcut for a graph of the real part versus the imaginary part.
>>t = 0:pi/10:2*pi;>>plot(exp(i*t),'-o')>>axis equal>>xlabel(‘Real’)>>ylabel(‘Imaginary’)Slide27
Figure Handling
Graphing functions automatically open a new figure window if there are no figure windows already on the screen. If a figure window exists, it is used for graphics output.
The default is to graph to the
current figure (usually the last active figure)To create a new figure without overwriting the old, use the figure command Slide28
When multiple figures already exist, you can set the
current figure
is the command
figure(n) where n is the number at the top of the figure window.>> plot(x,seis,x,seis2)>> legend('Raw','Mean Removed') >> figure #creates 2 >> plot(seis,'r') >>figure(1) #makes 1 currentSlide29
Creating subplots
The
subplot
command enables you to display multiple plots in the same window or print them on the same piece of paper.t = 0:pi/10:2*pi;[X,Y,Z] = cylinder(4*cos(t));subplot(2,2,1); mesh(X)subplot(2,2,2); mesh(Y)subplot(2,2,3); mesh(Z)subplot(2,2,4); mesh(X,Y,Z)#creates a 2 x 2 matrix of subplots1234Slide30
Controlling axes lengths
The
axis
command provides a number of options for setting the scaling, orientation, and aspect ratio of graphs.Set the axis limitsaxis autoaxis([xmin xmax ymin ymax zmin zmax])Set the axis aspect ratioaxis auto normalaxis square; axis equalSet axis visibility axis on; axis offSet grid linesgrid on; grid offSlide31
>> figure(2)
>> plot(seis2)
>> axis([6500 10500 -4000 4000])Slide32
Overlaying new graphs
Use the command
hold on
to overlay different types of plots on one another>>[x,y,z] = peaks;>>pcolor(x,y,z)>>shading interp>>hold on>>contour(x,y,z,20,'k')>>hold offSlide33
Saving & Exporting Graphics
The default graphics file is a MATLAB Figure or .fig formatted file.
This format retains the most data about how it was created within
matlabit is not particularly portableyou can also save as eps (encapsulated postscript), which can be read by Illustratoror you can save in one of the picture formats like tiff and jpg which all no additional editing but maintain good resolutionSlide34
Cool Feature
After creating the perfect figure, you can generate a
m
-file so that the figure can be recreated using different data in the future.This feature is found under the File drop down menu in the Figure toolbar.