MATLAB: Efficient way to compute an integral within another integral

integralprobability

Hi all,
Is there an efficient way to compute an integral within another integral as shown in the attached figure?
I am using the following code that is very slow, and I have to compute this function for a large number of input values.
pd_X = makedist('Normal',mu_X,sigma_X);
pd_Y = makedist('Normal',mu_Y,sigma_Y);
P = zeros(n,1); % set of probabilities for n input values
for i = 1:n
f_Y = @(y) pdf(pd_Y,y);
f_X = @(x) pdf(pd_X,x).*integral(f_Y,a*i-x,inf,'ArrayValued',true);
P(i,1) = integral(f_X,-inf,(i-1)*a,'ArrayValued',true);
end
Thanks in advance!

Best Answer

Creating an anonymous function to calculate the normal PDF functions resulted in an average speed increase of about 3.6 over 5 runs:
pdf_N = @(x,mu,sd) exp(-(x-mu).^2./(2*sd.^2)) ./ (sd*sqrt(2*pi));
P = zeros(n,1); % set of probabilities for n input values
for i = 1:n
f_Y = @(y) pdf_N(y, mu_Y, sigma_Y);
f_X = @(x) pdf_N(x, mu_X, sigma_X).*integral(f_Y,a*i-x,inf,'ArrayValued',true);
P(i,1) = integral(f_X,-inf,(i-1)*a,'ArrayValued',true);
end