Understanding the output returned by anonymous function
Show older comments
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
Accepted Answer
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);
Categories
Find more on Numerical Integration and Differential Equations 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!