introduction to matlab session 5 simopekka vänskä, thl 2010
Post on 05-Jan-2016
213 Views
Preview:
TRANSCRIPT
Introduction to MATLABSession 5
Simopekka Vänskä, THL2010
Introduction to MATLAB - Session 5
Contents of this courseSession 4
Function functions
Session 5 Graphics 2
More linear algebra
Starting homework
Session 1 General
Matrices
M-files
Session 2 Some matrix commands
Logical expressions
Graphics 1
Session 3 My functions + strings, cells
Controlling program flow
Graphics 22-3 dimensional graphs
Introduction to MATLAB - Session 5
Graphics 22-3 dimensional plots
The graphical library of MATLAB is large. See
>> help graph2d
>> help graph3d
>> help specgraph
When representing your computations, use some time to think effective visualization techniques
Besides the image itself, control the axis, labels, ticks, grids, etc.
Nothing too much, nothing missing.
Introduction to MATLAB - Session 5
Line plot extensions
plot3 draws the line in 3D
grid puts the grid mode on/off
area(x,Y) fills the columns of Y
and draws them one upon
theorher Practical when showing how
some partition developes
polar plots the line in the polar
coordinates
Try the following
>> t = (0:.001:1)’;
>> plot3(cos(10*t),sin(10*t),t)
>> grid
>> s = [sqrt(t), t, t.^2];
>> subplot(1,2,1)
>> area(t,s);
>> subplot(1,2,2)
>> plot(t,cumsum(s’))
>> polar(2*pi*t,cos(4*pi*t))
Introduction to MATLAB - Session 5
Scalar functions on plane - meshgrid
A scalar function on plane:
z = f(x,y).
Meshgrid is needed for
drawing:
If x and y are vectors, we need
to create all pairs
(x(i),y(j)).
>> [X,Y] = meshgrid(x,y);
Try the following:
>> x = 0:.1:1;
>> y = 0:.2:2;
>> [X,Y] = meshgrid(x,y);
>> X
>> Y
>> plot(X,Y,’o’)
Remark X and Y are constant
in different coordinates.
Introduction to MATLAB - Session 5
Scalar functions on planeSome commands
contour draws the contour plot
mesh and surf draw the
surface of the function (unfilled
and filled), surfc both surface
and contours shading interp clears the
surface lines
colorbar displays the scale
view sets the view angle
colormap sets the color scale
Image, imagesc for pixel plots
Try the following>> x = -1:.01:1;>> y = x;>> [X,Y] = meshgrid(x,y);>> Z = X.^2 + Y.^3;>> contour(x,y,Z)>> contour(x,y,Z,20)>> mesh(x,y,Z)>> surf(x,y,Z)>> shading interp;>> colorbar>> view(2)>> view([0 -1 1])>> colormap hot>> colormap default>> surfc(x,y,Z)>> imagesc(x,y,Z)
Introduction to MATLAB - Session 5
Vectorfields on plane or in 3D
A vector field on plane:F = (f1(x,y),f2(x,y))
quiver(X,Y,U,V) draws an
arrow (U(j),V(k)) to each grid
point (X(j),Y(k))
Similarly quiver3 in 3D
Try the following:
>> x = -1:.1:1;
>> y = x;
>> [X,Y] = meshgrid(x,y);
>> U = X;
>> V = Y.^2;
>> quiver(X,Y,U,V);
Introduction to MATLAB - Session 5
Some other plotting functions hist(X) makes a histogram of
each column of X Returns also the number of
elements falling in the
intervals.
bar(X) draws the columns of X
as vertical bars pie(x) draws the pie plot of
vector x (normalized) fill(X,Y,C) fills the polygons
defined by the columns of X
and Y with color C isosurface(X,Y,Z,F,V) draws
the isosurface F(X,Y,Z) = V
Try the following
>> hist(randn(10000,2),20);
>> N = hist(randn(1000,3),20);
>> bar(rand(2,4))
>> pie(1:4)
>> fill([0 1 2 1 0],[0 1 -1 -1 0],’r’)
>> x = -1:.1:1;
>> [X,Y,Z] = meshgrid(x,x,x);
>> FXYZ = X.^2+2*Y.^2 - 3*Z.^2;
>> isosurface(X,Y,Z,FXYZ,1)
Introduction to MATLAB - Session 5
Changing general figure properties
Figure and axes properties, and
many others, can be changed:
General principle: Set a handle to current
figure/axes with gcf/gca (get
current figure/axes) To see the properties, use get To change the property, use
set For more, see Handle
Graphics Properties of help
clf for clearing the figure
Try the following
>> bar(rand(2,4))
>> h = gca;
>> get(h)
>> set(h,’XtickLabel’,{’a’,’bcd’})
>> set(h,’YTick’,[])
More linear algebra
Introduction to MATLAB - Session 5
Matrix decompositions
We finish the course with matrix decompositions, or, with the most
important one SVD (for other decompositions, see HELP): Every matrix A has the singular value decomposition
A = U*S*V’ >> [U,S,V]=svd(A);where
U and V are unitary matrices (the columns define an orthonormal
basis) S is a diagonal matrix with positive and decreasing elements.
Statisticians, compare to the principal component analysis.
Every matrix has a pseudoinverse pinv(A)
pinv(A) = V*R*U’
where R is a diagonal matrix with R(j,j) = 1/S(j,j) for nonzero S(j,j)
and zero otherwise. x = pinv(A)*y is the best nonregularized solution for all linear
equations Ax=y.
ProblemsSession 5
Introduction to MATLAB - Session 5
ProblemsDraw the contour plot of function
F(x,y) = x1.5y2exp(-(x-2)-(y-0.5))
on [0,5]x[0,5] and its gradient field with quiver command in the same picture.
Use meshgrid to create the X,Y –grid. Compute the derivative by hand or by using your earlier
differf.m function.
Make a histogram of points randn(10000,1).
Homeworks
Introduction to MATLAB - Session 5
How to do your homeworkYou can co-operate with other students
…but do not just copy the code.Add graphics (into the code) which demonstrates the
calculations.
How to return: Write a m-file called runthis.m which calls your functions. Put the m-files and data files in the same directory, and make
sure that runthis.m file works. Send all homework files to the lecturer, who will copy your
program files to one directory and test it by executing runthis –
command. If the program works well enough, you pass the
homework. Lecturer contacts you by e-mail.
Introduction to MATLAB - Session 5
Homework Topics
Personal finance – how your savings accumulateSIR – model for measles
Homeworks are described in more details in pdf-files that are located at the course website.
If you find them very easy, do both. But return only one.
>> I hope you
…have learned some MATLAB.
And wish you a successful career with computational math!
top related