Conversion to double from function_handle is not possible

조회 수: 7 (최근 30일)
giulio graziani
giulio graziani 2019년 12월 9일
댓글: giulio graziani 2019년 12월 10일
Hi, I'm new to Matlab and I got this error while trying to write down a function.
Can someone help me understanding where is the problem in my code?
function [errors] = first_fun(market_vol,strike_prices,ExerciseDates)
n = size(market_vol,2);
m = size(market_vol,1);
errors = zeros (n);
for j = 1:n
for i = 1:m
errors(i,j) = @(X) market_vol(i,j) - blackvolbysabr(X(1),Beta,X(2),X(3),Settle,ExerciseDates{j},ForwardValue,strike_prices(i));
end
end
end
Thanks

채택된 답변

Walter Roberson
Walter Roberson 2019년 12월 9일
errors = zeros (n);
That says that errors is to be created as an n by n array of double precision values initialized to all zeros.
errors(i,j) = @(X) market_vol(i,j) - blackvolbysabr(X(1),Beta,X(2),X(3),Settle,ExerciseDates{j},ForwardValue,strike_prices(i));
The right hand side starting from @(X) says that what follows is to be the handle to a function that accepts a single parameter, and that if the handle were ever to be invoked, then at the time of invocation it should calculate the part that follows, substituting the passed-in parameter in place of the variable named X. The only calculation involved in this process is to look through the right hand side expression and find all of the variables such as market_vol that are mentioned, and take copies of those variables and store them along with the function handle, but the subtraction and so on are to be deferred until the function handle is invoked.
You then try to store that function handle into a location in the array that is initialized to all 0. But function handles are not a compatible data type to themselves be converted to double.
What you can do is
errors = cell(m,n);
for j = 1:n
for i = 1:m
errors{i,j} = @(X) market_vol(i,j) - blackvolbysabr(X(1),Beta,X(2),X(3),Settle,ExerciseDates{j},ForwardValue,strike_prices(i));
end
end
This would result in a cell array of function handles being returned; the function handles can then be extracted and used in various ways, such as
optim_2_1 = fminunc(errors{2,1}, rand(1,3)); %minimize function value
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 12월 9일
Your current code is setting up to run a calculation between an element of market_vol and a value created by running blackvolbysabr() with parameters determined in part by position in the matrix.
It looks to me as if perhaps you want something like
function [best_errors, best_X] = first_fun(market_vol, strike_prices, ExerciseDates, Beta, Settle, ForwardValue)
best_X = lsqnonlin(@(X) first_fun_helper(X, market_vol, stripe_prices, ExerciseDates, Beta, Settle, ForwardValue), [0.5 0 0.5], [0 -1 0], [inf 1 inf]);
best_errors = first_fun_helper(best_X, market_vol, stripe_prices, ExerciseDates, Beta, Settle, ForwardValue);
end
function errors = first_fun_helper(X, market_vol, stripe_prices, ExerciseDates, Beta, Settle, ForwardValue)
n = size(market_vol,2);
m = size(market_vol,1);
errors = zeros(m,n);
for j = 1:n
errors(:,j) = market_vol(:,j) - blackvolbysabr(X(1), Beta, X(2), X(3), Settle, ExerciseDates{j}, ForwardValue, strike_prices(1:m));
end
end
Note that you need the three additional parameters Beta, Settle, ForwardValue in order to do the calculation -- unless, that is, you are willing to hard-code their values inside first_fun()
giulio graziani
giulio graziani 2019년 12월 10일
Is there a way to express immense gratitude for your help ?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by