1 / 18

Introduction to MATLAB Session 5

Introduction to MATLAB Session 5. Simopekka Vänskä, THL 2010. Session 4 Function functions. Contents of this course. Session 1 General Matrices M-files Session 2 Some matrix commands Logical expressions Graphics 1 Session 3 My functions + strings, cells Controlling program flow.

tammy
Télécharger la présentation

Introduction to MATLAB Session 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introductionto MATLABSession 5 Simopekka Vänskä, THL 2010

  2. Session 4 Function functions Contents of this course Session 1 • General • Matrices • M-files Session 2 • Some matrix commands • Logical expressions • Graphics 1 Session 3 • My functions + strings, cells • Controlling program flow • Session 5 • Graphics 2 • More linear algebra • Starting homework Introduction to MATLAB - Session 5

  3. Graphics 22-3 dimensional graphs

  4. 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

  5. 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)) Line plot extensions Introduction to MATLAB - Session 5

  6. 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. Scalar functions on plane - meshgrid Introduction to MATLAB - Session 5

  7. 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) Scalar functions on planeSome commands Introduction to MATLAB - Session 5

  8. 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); Vectorfields on plane or in 3D Introduction to MATLAB - Session 5

  9. 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) Some other plotting functions Introduction to MATLAB - Session 5

  10. 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’,[]) Changing general figure properties Introduction to MATLAB - Session 5

  11. More linear algebra

  12. 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. Matrix decompositions Introduction to MATLAB - Session 5

  13. ProblemsSession 5

  14. Problems • Draw 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). Introduction to MATLAB - Session 5

  15. Homeworks

  16. 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. Introduction to MATLAB - Session 5

  17. 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. Introduction to MATLAB - Session 5

  18. >>I hope you …have learned some MATLAB. And wish you a successful career with computational math!

More Related