Improve running time of code with integrals
Mostrar comentarios más antiguos
Hello,
I'm aiming to create the following covariance matrix,
,where the set of possible
combinations is given by
This results in
different combinations, meaning Σ is of dimension
.
The integrand is given by
with some parameters
.
with some parameters Below is my created function, which takes as input an array of parameters
, the output is the resulting covariance matrix Σ.
Because the covariance matrix is symmetric, I just compute the lower triangular part and add the upper triangular part in the last step.
Computing this takes very long, after 5 minutes I just got the first 200 lines of the matrix. Regarding that all this is just one step in computing a Likelihood-function, which I aim to maximize for
numerically, the running time has to be reduced. The problem is obviously the part where I'm integrating in the middle of the code, can anyone give me an advice how to optimize this part?
function Sigma = covariance_matrix(P)
sig = @(t,x) (P(1) + P(2).*exp(P(3).*(x+t))) .* (P(4)+t) .* exp(-P(5).*t);
i = 0;
for x1 = 30:95
for t1 = 0:95-x1
i = i+1;
j = 1;
for x2 = 30:x1
t2 = 0;
while j <= i && t2 <= 95-x2
Sigma(i,j) = integral(@(s) integral(@(u) sig(u,x1-1+s), ...
t1+1-s,t1+2-s).*integral(@(u) sig(u,x2-1+s), ...
t2+1-s,t2+2-s),0,1,'ArrayValued',true);
t2 = t2+1;
j = j+1;
end
end
end
end
Sigma = Sigma + (Sigma-diag(diag(Sigma)))';
end
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Numerical Integration and Differentiation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!