Understanding the output returned by anonymous function
6 views (last 30 days)
Show older comments
Purnjay Peshawaria
on 18 Jul 2020
Answered: Abhishek Gangwar
on 18 Jul 2020
I have a function blackscholes.m that returns 2 outputs: c and dcds. Here is the code:
function [c, dcds] = blackscholes(S, K, r, sigma, Tmt)
s = sigma * sqrt(Tmt);
d1 = ( log(S/K) + ( r + sigma.^2/2)*(Tmt) ) ./ s;
d2 = d1 - s;
% Use normpdf and normcdf from Statistics toolbox
c = S .* normcdf(d1) - K * exp(-r*Tmt) * normcdf(d2);
% Derivative of call vlaue w.r.t. volatility sigma
dcds = S .* normpdf(d1) * sqrt(Tmt);
sigma is a vector defined in the code below. I have created an anonymous function f. My problem is when I calculate f(sigma) , I'm getting the first ouput of blackscholes function - cmkt. Is there a way for the anonymous function to return multiple outputs. I have read the documentation and it says you can invoke anonymous function to access the outputs returned by your function (blackscholes in this case) . But I get error when I try the last line of the following code:
format compact
Tmt = 0.5; % time to maturity in years
r = 0.053; % risk-free interest rate per year
S = 32.75; % current stock price
K = 32.00; % strike price 1
cmkt = 2.74; % current market price of call option
pmkt = 1.16; % current market price of put option
% Anonymous function for difference between Black-Scholes price and market price
f = @(sigma) blackscholes(S, K, r, sigma, Tmt) - cmkt;
sigma = linspace(0,0.8);
result = f(sigma); % this works fine
[result1, result2] = f(sigma); % this is giving error
0 Comments
Accepted Answer
Abhishek Gangwar
on 18 Jul 2020
Yes, you are right, you are invoking the function 'f' correctly but the problem is function 'f' is returning only one output which is a row vector of dimension 1x100, lets consider the statement where you are defining anonymous function.
You are calling blackscholes() function with appropriate inputs but because of you are not storing its output anywhere, this output is being stored in matlab's default variable 'ans' and eventually this variable is storing first output row vector of blackscholes() function which is 'c' and then subtracting 'cmkt' from every element of that row vector and final resulting row vector is being returned from 'f' function.
You can also check this by modifying this line "f = @(sigma) blackscholes(S, K, r, sigma, Tmt) - cmkt;" as
"f = @(sigma) blackscholes(S, K, r, sigma, Tmt);" and then try to invoke this modified anonymous function, you will get both the output vectors of blackscholes() function.
You need to modify your code, you can subtract 'cmkt' within the blackscholes() function or just capture both the row vectors returned from blackscholes() function and then subtract 'cmkt' from them.
0 Comments
More Answers (1)
madhan ravi
on 18 Jul 2020
f = @(S, K, r, sigma, Tmt,cmkt)deal(blackscholes (S, K, r, sigma, Tmt) - cmkt);
[o1, o2] = f(S, K, r, sigma, Tmt, cmkt);
0 Comments
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!