MATLAB: Trying to input a range of numbers and generate a matrix…

MATLABmatrix

Hi
I am trying to input a mesh into a slight variant of the standard Black-Scholes function. I wish to vary the 'S' and the 't'. I am using the linspace for this so that I generate 100 equally spaced points in equally spaced intervals.
function C=bsf3(S,t)
% Here our function is C=bsf3(S,t,--all the rest given ---K,r,sigma,T)
% We will construct the Black-Scholes formula for
% a European call option
% We set up our variables:
T=1; % Time to Expiry
K=1; % Strike Price
r=0.05; % Riskless Interest Rate
sigma=0.25; % Market Volatility
tau=T-t;
if tau>0
d1=(log(S/K)+(r+0.5*sigma^2)*tau)/(sigma*sqrt(tau));
d2=d1-sigma*sqrt(tau);
% From standard Black-Scholes materials
N1=0.5*(1+erf(d1/sqrt(2)));
N2=0.5*(1+erf(d2/sqrt(2)));
C=S*N1-K*exp(-r*tau)*N2;
else
C=max(S-K,0);
end
I try to call bsf3 using the following:
>> [S,t]=meshgrid(linspace(0,2),linspace(0,1));
>> C=bsf3(S,t);
>> mesh(S,t,C)
C seems to be a 100 x 100 matrix with repeated rows, which is not what I am looking for as each row should be different. In particular the mesh has no 'skew' or tilt.
How do I generate C by varying the input along S and along t so that I have 100×100 different entries?
Should I be using different inputs?
Thanks
Joe

Best Answer

You write "C seems to be a 100 x 100 matrix with repeated rows", but it is not. The differences between the rows are just quite small compared to the difference between the first and last column.
Try
plot(C(1,:) - C(2,:))
or
mesh(S,t,C)
view(-pi/2, 0)
There are some small differences.
You can see them also if you zoom in and use a different colormap
colormap hsv
surf(S(:,20:end-40),t(:,20:end-40),C(:,20:end-40)), shading interp