matlab basics fin250f: lecture 3 spring 2010 grifths web notes
TRANSCRIPT
![Page 1: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/1.jpg)
Matlab Basics
FIN250f: Lecture 3
Spring 2010
Grifths Web Notes
![Page 2: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/2.jpg)
Loading Software
Matlab Follow matlab instructions for your own
machine Downloads
We will eventually be downloading various matlab programs and data from the website
![Page 3: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/3.jpg)
Matlab Basics
Math Operations Variables Vectors Functions Fancy subscripts Program files
![Page 4: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/4.jpg)
Math Operations
Super calculator +,-,*,/,^, ()
![Page 5: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/5.jpg)
Variables
x = 1 1+x x = 10 y = 20 x*y The importance of ; (no printing) x = 1;
![Page 6: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/6.jpg)
Vectors
In matlab variables can be vectors Vector = list of numbers
x = [1 5 2 1 5]; 2*x x+1
![Page 7: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/7.jpg)
Generating Vectors
: operator 1:10 1:2:10 60:-1:35 x = 1:5;
![Page 8: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/8.jpg)
Parts of Vectors
x(2) x(10) x(b) x(10:15), x(20:end)
![Page 9: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/9.jpg)
Vector Operations(dot operators)
x = [1 2 3]; y = [ 2 1 3]; x .* y x ./ y x .^ y
![Page 10: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/10.jpg)
Matrices
x = [1 2 3; 4 5 6];
x = 1 2 3 4 5 6
rows = 2, columns = 3
![Page 11: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/11.jpg)
Parts of Matrices
x = 1 2 3 4 5 6 x(row, column) x(2,1) = 4 x(2,:) = 4 5 6 x(1,1:2) = 1 2
![Page 12: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/12.jpg)
Functions
Scalar functions: f(x) log(x), exp(x), sqrt(x) sin(x), cos(x), tan(x)
Vector functions sum(x), mean(x), median(x)
![Page 13: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/13.jpg)
More Functions
plot(x) plot(x,'*'); Most important function!!!!
Help help functionname help plot
![Page 14: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/14.jpg)
Even More Functions
zeros(m,n) : returns a matrix of zeros ones(m,n) : returns a matrix of ones rand(m,n) : returns a matrix of uniform
[0,1] random numbers randn(m,n) : returns a matrix of normal,
mean 0, variance 1, random numbers (Note: We will often use my own function
normal instead of randn)
![Page 15: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/15.jpg)
Programs (mfiles)
Execute commands in a file filename.m Edit using matlab editor (colors) Examples:
![Page 16: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/16.jpg)
Conditions
>, <, >=, <=, ==, ~=
x > 5 x < 10
![Page 17: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/17.jpg)
If conditions
if (x>1) x = x/2; end
![Page 18: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/18.jpg)
If/else
if (x<0) x = x+1; else x = -1*x; end
![Page 19: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/19.jpg)
For loops
for j = [1 2 3] x(j) = j+1; end
![Page 20: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/20.jpg)
For Loops
for j = 1:10 x(j) = 2^j; end
also x = 2.^(1:10);
![Page 21: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/21.jpg)
For Loops
Useful for dealing with arrays Costly in terms of computer time
![Page 22: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/22.jpg)
While Loops
x = 20; y = 0; while (x>3) y = y+sqrt(x); x = x-5; end
![Page 23: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/23.jpg)
If’s With Vectors
if (a==b) disp(‘a equals b’) end
What if a and b are vectors? True if every element of a equals every
element of b
![Page 24: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/24.jpg)
If’s With Vectors
if (a~=b) disp(‘a not equals b’) end
True if every element of a is not equal
![Page 25: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/25.jpg)
Any and All
if any(a==b) disp(‘at least one equal’) end
if all(a==b) disp(‘all are equal’) end For matrices this gets more complicated
![Page 26: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/26.jpg)
Fancy Subscripts (find function)
x = [5 2 -3 -4 1]; y = x(find(x<0));
all x values less than zero put in y k = find(x<0); k = subscripts k = [ 3 4];
![Page 27: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/27.jpg)
Fancy Subscripts
x = [5 2 -3 -4 1]; y = x(x<0); all x values less than zero put in y k = x<0; k = [0 0 1 1 0]; y = x(k) = [-3 -4] s = sum(k) = 2
![Page 28: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/28.jpg)
Important: Comment Lines
Lines starting with % are comments
x = 1; y = 2; z = x*y; % this really is not doing very much
![Page 29: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/29.jpg)
Getting and Storing Results
diary filename (all results to filename) diary off (stop)
![Page 30: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/30.jpg)
Load
load x.dat File x.dat = 1 2 3 4 5 6
Same thing in matrix x x file must be matrix
![Page 31: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/31.jpg)
Save
save (saves everything to file matlab.mat) load (restores all settings) useful for taking a break, and starting
where you left off
![Page 32: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/32.jpg)
Save
save filename x y z (save to filename.mat variables x, y, z)
save filename x y z -ascii (save in ascii format)
![Page 33: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/33.jpg)
User Functions
Define your own functions Very important and powerful
![Page 34: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/34.jpg)
Function file (positives.m)
function count = positives(x) % usage: count = positives(x) % x = vector % count = number greater than zero count = sum(x>0);
![Page 35: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/35.jpg)
Fancy function stuff
Multiple returns
function [ count, count2] = positives(x) count = sum(x>0); count2 = sum(x>2);
![Page 36: Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f205503460f94c38987/html5/thumbnails/36.jpg)
Summary
We will see many more of these matlab related commands as we make use of them