heavy rocker dude
heavy rocker dude

Reputation: 2301

Weird Matlab Input argument error?

Hey there I have the following Matlab code. It complains about:

??? Input argument "r" is undefined.

Error in ==> BlackScholesPriceTmp at 8 b=r;

Code:

%function [price,delta,gamma,vega,theta]=BlackScholesPriceTmp(CallPutFlag,S,X,T,r,v,noutparams)
function [price,delta,gamma,vega,theta]=BlackScholesPriceTmp(CallPutFlag,S,X,T,r,v,noutparams)
delta=0;
gamma=0;
vega=0;
theta=0;

    b=r;
    d1 = (log(S / X) + (b + v ^ 2 / 2) * T) / (v * T^0.5);
    d2 = d1 - v * T^0.5;

        price=0;
    if CallPutFlag == 'c' ,
        price = S * normal_cdf(d1) - X * exp(-r * T) * normal_cdf(d2);
        %endif
    end
    if noutparams>1,
        delta=exp((b-r)*T)*normal_cdf(d1);
        theta_tmp1= -( S*exp((b-r)*T)*normal_pdf(d1)*v )/(2*T^0.5);
        theta_tmp2= -(b-r)*S*exp((b-r)*T)*normal_cdf(d1);
        theta_tmp3= -r*X*exp(-r*T)*normal_cdf(d2);
        theta=theta_tmp1+theta_tmp2+theta_tmp3;
    %end
    %endif
    else
        %price = X * exp(-r * T) * normal_cdf(-d2) - S * normal_cdf(-d1);
        price = X * exp(-r * T) * normalcdf(-d2) - S * normcdf(-d1);
    end
    if noutparams>1,
        delta=exp((b-r)*T)*(normal_cdf(d1)-1);
        theta_tmp1= -( S*exp((b-r)*T)*normal_pdf(d1)*v )/(2*T^0.5);
        theta_tmp2= (b-r)*S*exp((b-r)*T)*normal_cdf(-d1);
        theta_tmp3= r*X*exp(-r*T)*normal_cdf(-d2);
        theta=theta_tmp1+theta_tmp2+theta_tmp3;
    endif
    end
    if noutparams>1,
    gamma=(normal_pdf(d1)*exp((b-r)*T)) / (S*v*T^0.5);
    vega=S * exp((b-r)*T)*normal_pdf(d1)*T^0.5;
    endif
end

As being to new to Matlab, why does it complain about the r parameter but not the rest. I don't understand why the others are being complained? What am I doing right in the rest but not in r?

Upvotes: 0

Views: 889

Answers (3)

user1477622
user1477622

Reputation: 132

Most likely you are trying to run the function itself. You must call the function externally using [price,delta,gamma,vega,theta]=BlackScholesPriceTmp(CallPutFlag,S,X,T,r,v,noutparams) in the command window (console). However make sure to initialize each of the inputs with values that you want or you will again receive an error.

Upvotes: 0

SCFrench
SCFrench

Reputation: 8374

In MATLAB, you are allowed to call a function with less than the number of declared input arguments. It only becomes an error if the called function tries to access the value stored in a parameter that was not specified. In that case, the error message will be the one you are getting, namely, 'Input argument "parameterName" is undefined.'.

MATLAB functions can use the nargin function to determine at run time the number of actual arguments passed. Often, the following idiom is used to provide default values for those parameters that aren't specified:

function foo(a, b)
if nargin < 1
    a = 0; % default for a is 0
end
if nargin < 2
    b = 1; % default for b is 1
end
... code that uses a and/or b ...

The function foo can be called in any of the following ways:

foo(); % equivalent to foo(0, 1);
foo(5); % equivalent to foo(5, 1);
foo(5, 6);

Upvotes: 2

AhmedAZ
AhmedAZ

Reputation: 11

I think there are several problems apart from r

using lines containing normal_pdf() , normal_cdf() , mormalcdf() , the correct form is normpdf() , normcdf()

also you have several endif statements , matlab uses end statements instead.

I can call the following as a function successfully using Matlab 7.9 or GNU Octave 3.2.3 .

    function [price,delta,gamma,vega,theta]=BlackScholesPriceTmp(CallPutFlag,S,X,T,r,v,noutparams)

delta=0; gamma=0; vega=0; theta=0;

b=r;
d1 = (log(S / X) + (b + v ^ 2 / 2) * T) / (v * T^0.5);
d2 = d1 - v * T^0.5;

price=0;

if CallPutFlag == 'c' ,
    price = S * normcdf(d1) - X * exp(-r * T) * normcdf(d2);
end

if noutparams>1,
    delta=exp((b-r)*T)*normcdf(d1);
    theta_tmp1= -( S*exp((b-r)*T)*normpdf(d1)*v )/(2*T^0.5);
    theta_tmp2= -(b-r)*S*exp((b-r)*T)*normcdf(d1);
    theta_tmp3= -r*X*exp(-r*T)*normcdf(d2);
    theta=theta_tmp1+theta_tmp2+theta_tmp3;
else
    price = X * exp(-r * T) * normcdf(-d2) - S * normcdf(-d1);
end

if noutparams>1,
    delta=exp((b-r)*T)*(normcdf(d1)-1);
    theta_tmp1= -( S*exp((b-r)*T)*normpdf(d1)*v )/(2*T^0.5);
    theta_tmp2= (b-r)*S*exp((b-r)*T)*normcdf(-d1);
    theta_tmp3= r*X*exp(-r*T)*normcdf(-d2);
    theta=theta_tmp1+theta_tmp2+theta_tmp3;
end

if noutparams>1,
    gamma=(normpdf(d1)*exp((b-r)*T)) / (S*v*T^0.5);


    vega=S * exp((b-r)*T)*normpdf(d1)*T^0.5;

end

Upvotes: 1

Related Questions