This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: An Introduction to Matlab Brad Baxter School of Economics, Mathematics and Statistics, Birkbeck College, University of London, Malet Street, London WC1E 7HX [email protected] You can obtain these notes from http://econ109.econ.bbk.ac.uk/brad/matlab octave 1. Introduction You will each be assigned a userid for this computer room. The door code is 5858 and you are welcome to use it whenever it is available. All machines should be equipped with Matlab and other useful software. You can buy the Matlab Student Edition from www.mathworks.co.uk , if you don’t already have access to the full version, which comes with a useful book. However, the free product Octave is provided with many linux distributions and a Windows version can be obtained from www.octave.org . Octave isn’t quite as polished a product as Matlab, but it’s extremely useful. There are many websites containing Matlab introductions and programs, so you should supplement your learning with Google searches. I also recom- mend An Introduction to Financial Option Valuation , by D. Higham; this inexpensive paperback is extremely useful. The best way to learn Matlab is to play with it. If you’re already familiar with the basics, then there are more substantial programs below. If you’re still bored, you can type demo . Our first code can be typed in as a program, using the create m-file facility, or just entered in the command window. If you decide to type it in, then omit the comments for brevity (i.e. every line beginning with a per cent sign). 2 Brad Baxter % % This program illustrates the Central Limit Theorem: suitably % scaled averages of uniformly distributed random variables % look Gaussian. % % First we create a 20 x 10000 matrix of pseudorandom numbers uniformly % distributed on the interval [0,1] % m = 20; n = 10000; v = rand(m, n); % % We now use the Matlab histogram function to see the distribution of % one row of this matrix % nbins = 20; hist(v(1,:), nbins); % % We now sum each column of this matrix, divide by sqrt(m) % and histogram the new sequence % w = sum(v)/sqrt(m); hist(w,nbins); % % Now play with the constants, m, n, and nbins. % Matlab can also generate Gaussian pseudorandom numbers directly: % We generate a 1 x 5000 array of N(0,1) numbers a = randn(1,5000); % histogram from -3 to 3 using bins of size .2 [n,x] = hist(a, [-3:.2:3]); % draw a normalized bar chart of this histogram bar(x,n/(5000*.2)); % draw the next curve on the same plot hold on % draw the Gaussian probability density function plot(x, exp(-x.^2/2)/sqrt(2*pi)) % % Note the Matlab syntax here: x.^2 generates a new array % whose elements are the squares of the original array x....
View Full Document
This note was uploaded on 01/20/2010 for the course ASE 311 taught by Professor Kraczek during the Spring '08 term at University of Texas at Austin.
- Spring '08