Definite integral computed for different data points, where limits of integration change with each data point

20 vues (au cours des 30 derniers jours)
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 commentaires
Walter Roberson
Walter Roberson le 14 Fév 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 .
Robert Pelgrift
Robert Pelgrift le 14 Fév 2020
Yes, the 14th integral depends on the values of r(14), S(14), q(14), K(14), Kfixed(14), P(14), tau(14),sigma(14).
The 15th integral depends on the values of r(15), S(15), q(15), K(15), Kfixed(15), P(15), tau(15),sigma(15), etc.
The values of all of these variable differ between the 14th and 15th data point, between the 15th and 16th data point, etc.

Connectez-vous pour commenter.

Réponses (3)

Walter Roberson
Walter Roberson le 14 Fév 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 commentaire
Walter Roberson
Walter Roberson le 16 Fév 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

Connectez-vous pour commenter.


Robert Pelgrift
Robert Pelgrift le 14 Fév 2020
I just don't understand what I would put after the for in the for loop. Can you please give me an example where r, S, q, K, Kfixed, P, tau,sigma are each 81X1 arrays, and I am integrating the function P .* G(r,S,q,K,tau,sigma) with respect to K, where the limits of integration are 0 and Kfixed?
  1 commentaire
Walter Roberson
Walter Roberson le 14 Fév 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.

Connectez-vous pour commenter.


Robert Pelgrift
Robert Pelgrift le 14 Fév 2020
Sorry, would you mind writing an example of the code that does this?

Catégories

En savoir plus sur Adding custom doc dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by