Matlab Introduction
This document provides an overview of MATLAB, focusing on its capabilities for manipulating vectors and matrices, performing matrix multiplication, and executing least squares solutions. It includes practical instructions on defining vectors and matrices, plotting temperature data, and using MATLAB for numerical computing tasks. The guide emphasizes the importance of MATLAB's built-in functions and toolboxes while also addressing symbolic algebra applications. It highlights the value of MATLAB's help resources for solving problems and explores various projects and simulations that can be developed using MATLAB.
Matlab Introduction
E N D
Presentation Transcript
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 • 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
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
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/')
perthTemps, ctd • ls % to check the file is theretype perthMeanTempsData.mtype('perthMeanTempsData.m') • YOU DO THE LITTLE PLOT TASK THERE
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
perthTemps, ctd 3 • SOLUTIONhold onplot(months,meanMaxTemp,'ro');plot(months,meanMinTemp, 'bo');title('perthTemps');
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
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
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
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)
Look at Matlab – Using Help • Students to get help on rand and randnEASY QUESTION: What is the difference between them??? • hist(randn(200,1))
Things we will do • I plan to lecture from Chapman’s book.
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
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
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
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
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)
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
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
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)])
Later lectures – I will be more systematic about programming aspects