Definite integral computed for different data points, where limits of integration change with each data point
9 views (last 30 days)
Show older comments
Hello, I have a data set with 81 data points of following variables, each of which is an 81X1 array when I import the data into Matlab: r, S, q, K, Kfixed, P, tau,sigma. I have successfully evaluated the function G(r,S,q,K,tau,sigma), which yields an 81X1 array accurately.
I am trying to compute a definite integral for each of the 81 data points. Specifically, I want to get an 81X1 array by computing the definite integral of function G() with respect to K, where the limits of integration are 0 and Kfixed -- so the limits of integration change with each data point since Kfixed has a different value at each data point. I tried to use the formula below to calculate this definite integral. Unfortunately, I keep getting the error "Error using integral (line 85) A and B must be floating-point scalars." Can someone please help with this specific integral? The solutions presented in other forums (e.g., using for loops and arrayfun) are not working in this case -- or I'm not understanding how to apply them to this specific case. Thanks.
mu = integral(@(K) P .* G(r,S,q,K,tau,sigma),0,Kfixed,"ArrayValued",true)
2 Comments
Walter Roberson
on 14 Feb 2020
Okay, when you calculate, say the 14th of the 81 values, that depends upon Kfixed(14) for the end point, and the 15th depends upon Kfixed(15) for the end point. But is there any other difference between computing the 14th and 15th, other than the end point? For example is the 14th integral depending on sigma(14) but the 15th integral is depending on sigma(15) instead?
If the function is fixed and the endpoint is the only thing that changes, then sort the end points giving sK, and proceed piecewise. Integrate the function first between 0 and sK(1), then between sK(1) and sK(2), then between sK(2) and sK(3), and so on. cumsum() those so that the N'th result corresponds to 0 to sK(N). Now de-sort it, going back to the order of the original Kfixed .
Answers (3)
Walter Roberson
on 14 Feb 2020
You need to do each integration separately. Using 'ArrayValued' is incorrect for your purpose.
I would suggest you get the whole thing going with a loop that pulls out the appropriate parameters and uses them to calculate for that one datapoint.
It is probably possible to do the calculation in a more compact form using arrayfun, but you are having trouble understanding arrayfun . Get the functionality working first with a loop, and worry about making the code prettier later. Right results soon with a loop beat maybe-wrong results coded with more struggle, thus postponing a solution and not being sure if the solution is right...
1 Comment
Walter Roberson
on 16 Feb 2020
numK = length(Kfixed);
omu = zeros(numK, 1);
for idx = 1 : numK
mu(idx) = integral(@(K) P(idx) .* G(r(idx), S(idx), q(idx), K, tau(idx), sigma(idx)), 0, Kfixed(idx));
end
Robert Pelgrift
on 14 Feb 2020
1 Comment
Walter Roberson
on 14 Feb 2020
In the loop from 1 to 81, pull out the values of each of your variables corresponding to current loop indices, so that you have a "current" version of each of them that is a scalar. Then construct the function handle of the expression to be integrated using the "current" version of the variables. integrate() the handle between 0 and the "current" version of the kFixed variable. Store the result of the integration in the output variable at the offset indicated by the current loop index.
See Also
Categories
Find more on Adding custom doc 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!