Download presentation
Presentation is loading. Please wait.
Published byApril Norman Modified over 9 years ago
1
Introduction to MATLAB Session 5 Simopekka Vänskä, THL 2010
2
Introduction to MATLAB - Session 5 Contents of this course Session 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
3
Graphics 2 2-3 dimensional graphs
4
Introduction to MATLAB - Session 5 Graphics 2 2-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.
5
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))
6
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.
7
Introduction to MATLAB - Session 5 Scalar functions on plane Some 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)
8
Introduction to MATLAB - Session 5 Vectorfields on plane or in 3D A vector field on plane: F = (f 1 (x,y),f 2 (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);
9
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)
10
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’,[])
11
More linear algebra
12
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.
13
Problems Session 5
14
Introduction to MATLAB - Session 5 Problems Draw the contour plot of function F(x,y) = x 1.5 y 2 exp(-(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).
15
Homeworks
16
Introduction to MATLAB - Session 5 How to do your homework You 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.
17
Introduction to MATLAB - Session 5 Homework Topics Personal finance – how your savings accumulate SIR – 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.
18
>> I hope you …have learned some MATLAB. And wish you a successful career with computational math!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.