vector-valued function element as function handle

Dear all,
I guess I have a relatively simple problem, but I could not find any solution online. The built-in Matlab function
[AssetPrice,OptionValue] = binprice(Price,Strike,Rate,Time,Increment,Volatility,Flag)
generates the two outputs AssetPrice and OptionValue. The first is a matrix the second a scalar. I need the scalar only and I would like to make it as a function of one specific variable, i.e.
[~,OptionValue] = @(Volatility) binprice(Price,Strike,Rate,Time,Increment,Volatility,Flag)
being all the other inputs known. However, defined in this way I get the error "The expression cannot be assigned to multiple values". If the function binprice would return only one output (instead of two), the previous syntax would work.
How do you overcome this problem?
Thank you,
F

 Accepted Answer

Matt J
Matt J on 24 Jul 2019
Edited: Matt J on 24 Jul 2019
With a wrapper,
fun=@(Volatility) mybinprice(Price,Strike,Rate,Time,Increment,Volatility,Flag);
function OptionValue = mybinprice(varargin)
[~,OptionValue] = binprice(varargin{:});
end

4 Comments

Thank you for the reply. I still have some problems however.
Should I save the code you plosted as a function? If that is the case I have to exclude the first line fun = @... . If that is correct, I name the function mybinprice and in another script I define
fun=@(Volatility) mybinprice(Price,Strike,Rate,Time,Increment,Volatility,Flag);
but, say, fun(.2) returns the matrix (AssetPrice) and not the scalar (OptionValue).
If you want to test it numerically you can use the values
Price = 100;
Strike = 100;
Rate = 0.01;
Time = 0.5;
Increment = 0.0002;
Flag = 1;
Thank you
Should I save the code you plosted as a function?
You can put the function wherever it is legitimate in Matlab to put a function. I don't know what version of Matlab you have. If you have anything more recent than R2015b, then there is no need to locate mybinprice in a separate file.
but, say, fun(.2) returns the matrix (AssetPrice) and not the scalar (OptionValue).
No, that should not happen. It should be trivial to debug, however.
I don't have the Financial Toolbox, so cannot test it. What happens if you put a break point at
[~, OptionValue] = binprice(varargin{:});
What is returned in OptionValue? Also, the documentation says that OptionValue is vector-valued, so why exactly are you expecting a scalar?
Thank you. It works perfectly.

Sign in to comment.

More Answers (0)

Asked:

on 24 Jul 2019

Edited:

on 24 Jul 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!