1
votes

I'm using "integral" subroutine in MATLAB to evaluate some integrals. We know that "integral" is based on an adaptive quadrature rule.
Is it possible and how to return number of function evaluations in "integral"?

1
please add more details for your question. For example: how does the input looks like and what is the desired output? - ibezito
Maybe just have a look into integral.m in the MATLAB application folder (quite often MATLAB functions are written in MATLAB-code themselves). Maybe you can modify the code or copy it to integralFctCount and then introduce a counter for function evaluations in it. - tim
@drorco For example, I want "[S, n] = integral(f,a,b)", where "S" is the value of integral and "n" is the number of function evaluations. Here we use default error tolerance in "integral" subroutine. - booksee
@tim I've already checked the "integralCalc" source code, but there are too many cases and functions in it to modify... - booksee

1 Answers

0
votes

YES!
I just unfortunately had to do this to compare a MATLAB to Python implementation. Say your MATLAB function is:

function value = integrand(x, flag, F,K,vol,T2,T1)
value = (log(x ./ (x+K)) + 0.5 .* (vol.^2) .* (T2-T1)) ./ (vol .* sqrt(T2 - T1));end

And your call to the function is:

quadgk(@(x) integrand(x, flag, F, K, vol, T2, T1), -K, Inf, 'AbsTol',tolerance);

Put a breakpoint at the function entry point (by clicking on the left of the function, value in this example), run the function with its inputs, and then click on the x variable in the workspace and it will show you the size which equals the number of integration points in the function. It also gives you the values passed during the integration.