ORF335 Precept  MATLAB Basics
Feb 15th, 2011
Preparatory Steps:

Open MATLAB

Locate “Command Window”

Check “Current Directory”

Open new mfile: File ( New(Mfile
%%%%%%% Basic Commands %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% General
clear %free up system memory
help plot %display help for the command 'plot'
% Ctrl+C % To break a computation / infinite loop
%%%% Numbers
1+4*sqrt(16)
a=max(3,sqrt(8));
result=exp(log(3));
result^(1/2)
sin(pi*5/3)
%%%% Vectors
x=[1;2;4]
y=[2;3;4]
length(x)
x(1)
x(1:2)
x'
x_transp=[1 2 4]
% Entry by entry operation
x.*y; %entry by entry product
% Regular operation
x*y'
%x*y; % This is wrong
x.^y % Entry by entry power
%%%% Matrices
A=[1 1 1 ; 1 2 3 ; 1 3 6] %3 by 3 matrix
size(A) % dimensions of A, which is 3*3
Q = ones(3,3) % Matrix with all entries 1
P = zeros(size(A)) % Matrix with the size of A with all entries 0
B = A' % transpose of A
C1 = A * B % Regular matrix multiplication
C2 = A .* B % Entry by entry
X = inv(A)
I = inv(A) * A
%%%% Randomness
n=5;
u=rand(n); % matrix of n by n independent realizations
% of a uniform in [0,1]
v_0=randn(n); % matrix of n by n independent realizations
% of a standard gaussian N(0,1)
mu=1;
sigma=0.3;
v=mu+sigma*rand(n); % matrix of n by n independent realizations
% of a gaussian N(mu,sigma^2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Loops, Tests...%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
%%%% Tests
x=randn(1);
if x>0
disp('Pick is positive');
disp(x);
else
disp('Pick is negative and absolute value is');
disp(abs(x));
end
%%%% Logical operator
a=1; b=0;
(a==1)
(b~=0)
% '&' is AND
(a==1) & (b~=2)
% '' is OR
(a>3)  (b==0)
%%%% Loops
n=5;
sum=0;
for i=1:5
sum=sum+i;
end
disp(sum)
disp(n*(n+1)/2)
% when step is not 1
% for x=0:0.02:5
i=0; sum=0;
while (i
i=i+1;
sum=sum+i;
end
disp(sum)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Plots...%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=0:0.01:2*pi;
y=zeros(length(x),1);
for i=1:length(x)
y(i)=sin(x(i));
end
figure(1)
plot(y)
figure(2)
plot(x,y,'b')
title('Sin(x)')
xlabel('x')
ylabel('sin(x)')
figure(3)
plot(x,y,'r',x,y.^2,'b')
legend('sin x','sin^2 x')
% equivalently
figure(4)
plot(x,y,'r'); hold on;
plot(x,y.^2,'b'); hold off;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Example %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 4period Binomial tree, starting at 100, with u=1.1, d=0.9, r=0.05, T=1.
%set parameter values
S0=100;
u=1.1;
d=0.9;
r=0.05;
T=1;
%create matrix to represent stock price tree (only use top right)
S=zeros(5,5);
for n=1:5
for j=1:n
S(j,n)=d^(j1)*u^(nj)*S0;
end
end
S
%now the tree `looks’ correct (using the top right half of the matrix), and the indices are consistent with lecture notes, except with time and space swapped (time=1^{st} index in notes, 2^{nd} here), and each shifted by 1 (since can’t use 0).
% S_{n,j} in the notes = S(j+1,n+1) here. You can use a different convention if you prefer!
%price call option with strike K=93, T=1 (remember to work backwards now):
K=93;
C=zeros(5,5);
for j=1:5
C(j,5)=max(S(j,5)K,0); %payoffs
end
C
q=(exp(r*T/4)d)/(ud) %risk neutral prob
for n=4:1:1
for j=1:n
C(j,n)=exp(r*T/4)*(q*C(j,n+1)+(1q)*C(j+1,n+1));
end
end
C
%The same looping idea can be used to find the replicating portfolio (a and b) throughout the tree by adding extra lines above. First define additional matrices.
%Note that if the question asked only for initial option price, then we could use a vector instead of a matrix for C and avoid storing unnecessary numbers – important when the tree becomes very large!
%Finally, explain how the indices of matrices are counted in MATLAB. ie,
%they are in this order:
[(1:5)' (6:10)' (11:15)' (16:20)' (21:25)']
%
%Hence the up up up up stock path is:
path1=[1 6 11 16 21];
C(path1)
%whereas up down up down is:
path2=[1 6 12 17 23];
C(path2)
%to get both the stock and call prices along this path:
[S(path2); C(path2)]
% Each index is 5*n+j+1 where j is number of down steps so far. 