1 / 23

Matlab Introduction

Matlab Introduction. By Grant Keady July 09, and Jan 14. Some basics. Matlab is designed to easily manipulate vectors and matrices You can define a vector by [4;2;6] You can define a transposed vector by [4,2,6] You can define a matrix with [4,2,6;3,2,1;5,3,5]. More basics.

Download Presentation

Matlab Introduction

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. Matlab Introduction By Grant Keady July 09, and Jan 14

  2. Some basics • Matlab is designed to easily manipulate vectors and matrices • You can define a vector by [4;2;6] • You can define a transposed vector by [4,2,6] • You can define a matrix with [4,2,6;3,2,1;5,3,5]

  3. More basics • Be careful when multiplying matrices in Matlab • If you have two matrices A and B, A*B will perform the matrix multiplication. If instead you want to multiply element-wise, use A.*B

  4. More basics, ctdMore on this later in this class • A\b does the least squares solutionA=[1,0;1,0;0,1]; b=[1;2;1];A\b

  5. If you haven’t already,Start Matlab • http://www.cwr.uwa.edu.au/~keady/Matlab/MatlabIntro/indexm.htmlDownload perthMeanTempsData.mIn matlab,cd to whereever you have put it. For me:cd('/Users/keady/Documents/Matlab/MatlabIntro/')

  6. perthTemps, ctd • ls % to check the file is theretype perthMeanTempsData.mtype('perthMeanTempsData.m') • YOU DO THE LITTLE PLOT TASK THERE

  7. perthTemps, ctd 2 • % PLOT EXAMPLE FOR YOU % Plot months on the horizontal axis and % with temperature in degrees C % on the vertical axis, show for each month, % the meanMaxTemp as a red o % the meanMinTemp as a blue o

  8. perthTemps, ctd 3 • SOLUTIONhold onplot(months,meanMaxTemp,'ro');plot(months,meanMinTemp, 'bo');title('perthTemps');

  9. perthTemps, ctd 4 • And next I will show you Matlab doing some matrix sums on the data Least squares fit of sinusoidal curvesIs a break for you … I will set other exercises, more in the later lectures than now

  10. What I use Matlab for • Numerical computing tasksMost recently, heating of a parked car(the car heating exercise was a little task)1997-2003 integral equation – entry of a wedge into water • Symbolic algebra in Eng. Math teaching • More details in the LaTeX beamer

  11. Skills we will need to do things:Writing functions • This is done by creating an M-(function) file • Example (use Matlab’s edit pane, fun.m): function out=fun(x,y) out=x*y-2 • In the command window writing fun(2,5) and pressing return will give 8

  12. Things we will do:Simulations • These just consist of generating random numbers to put into one’s functions, and examining the results • There are two types of random number generators; rand and randn • You can generate an n-m array of random numbers by writing rand(n,m)

  13. Look at Matlab – Using Help • Students to get help on rand and randnEASY QUESTION: What is the difference between them??? • hist(randn(200,1))

  14. Things we will do • I plan to lecture from Chapman’s book.

  15. More about Matlab • Matlab is extended by various Toolboxes(Cynical view is that this is to make money, but then again some are free) • Mapping Toolbox, limited licenceGoogle Earth … Mma • Own teaching obsession, Symbolic ToolboxCould re-assemble my ENVE3605 handouts

  16. History • I have downloaded another person’s account of the history in a ppt file.(2014, just said the story. SHOW IT HERE only if requested) • There is also an account on wikipedia/MATLAB

  17. Symbolic algebra • Not esp. important in this course • You can define symbolic variables in Matlab by writing syms x y • If you then write f=x/y you can use the subs function to substitute in numbers for x and y • subs(f, [x,y], [1,2]) will return 0.5 • subs(f, [x,y], sym([1,2])) will return 1/2

  18. Problems with the Sym Toolbox • Symbolic algebra can become tricky in Matlab. E.g. when you want to define a very large number of symbolic variables • You don’t want to write syms x1 x2 x3…x300 by hand • To do this you need something like n = 300; x = sym(zeros(n)); for k = 1:numel(x) x(k) = sym(sprintf('x%d', k)); end

  19. What the Sym Toolbox is for? • These are personal opinions • I would use Maple, Mathematica, MuPAD or whatever for larger Symbolics tasks • I like code writing from Symbolics packages, e.g. optimization, and having the Symbolics code the f (function), g (gradient), and h (hessian)

  20. Matlab’s inbuilt functions • Matlab has a huge number of functions built in already, including several minimization functions • If possible it is best to use them; they’re fast • If you can’t use these try to avoid for loops as this slows down your function

  21. Tips • The Matlab help menu is very useful, it has examples covering all of the basics and many more advanced problems • If that doesn’t work there is a good website http://www.mathworks.com/matlabcentral/ • It is usually enough just to search for your problem on the forum, otherwise you can join yourself and ask

  22. A few ‘cool’ things in Matlab • You can create a magic square by just typing magic(n) where n is the size of the square you want • 3D plots look good • To end this PowerPoint (using a demo)(: load handel; player=audioplayer(y,Fs); play(player,[1,(get(player,'SampleRate')*3)])

  23. Later lectures – I will be more systematic about programming aspects

More Related