How to use an integral inside another integral

I have the following code:
function [answer]=find_integral2(z)
F=@(y) find_integral(z-y).*find_integral(y);
answer= integral(F,-Inf,Inf);
end
function [answer]=find_integral(u)
F = @(x)normpdf(x).*normpdf(u./x);
answer= integral(F,-Inf,Inf);
end
When I call the function find_integral2(4), I get the following error:
Error using ./ Matrix dimensions must agree.
I know I can solve this with triple integral but I want to call find_integral2 several times, i.e. , I want to again use it in another function:
F=@(y) find_integral(z-y).*find_integral2(y);answer= integral(F,-Inf,Inf);
I would appreciate if you could help me fix this error. Thank you.

 채택된 답변

Mike Hosea
Mike Hosea 2013년 9월 23일

1 개 추천

Either this (find_integral is unchanged)
function [answer]=find_integral2(z)
F=@(y) arrayfun(@(y)find_integral(z-y).*find_integral(y),y);
answer= integral(F,-Inf,Inf);
end
or this
function [answer]=find_integral2(z)
F=@(y) find_integral(z-y).*find_integral(y);
answer= integral(F,-Inf,Inf,'ArrayValued',true);
end

댓글 수: 4

may
may 2013년 9월 23일
편집: may 2013년 9월 23일
Thanks a lot! It worked! Just a question, do you know any faster way to calculate integral with matlab? because I want to use find_integral2 recursively, and for the following function it took one hour to get the result!
function [answer]=find_integral3(z)
F=@(y) arrayfun(@(y)find_integral2(z-y).*find_integral(y),y);
answer= integral(F,-Inf,Inf);
end
Well, you could loosen the tolerances, say add the arguments
'AbsTol',1e-5,'RelTol',1e-3
to the integral calls.
That took about 110 seconds on my machine, which is admittedly a new workstation. For something completely different you might look into Monte Carlo or Smolyak methods. These iterated integrals just aren't that efficient as the dimension goes up. Nice set of slides here
may
may 2013년 9월 24일
Thank you SO MUCH! using 'AbsTol',1e-5,'RelTol',1e-3, now it takes around 1 minute!
may
may 2013년 9월 27일
편집: may 2013년 9월 27일
Thanks a lot for your help again. so now using 'AbsTol',1e-5,'RelTol',1e-3, I could speed up the function find_integral3(z) (mentioned above), but now I want to again use it to calculate the following integral (this is actually the final problem I want to solve)
F = @(y)find_integral3(z)(z-y).*find_integral3(y);
answer= integral(F,-Inf,Inf,'AbsTol',1e-5,'RelTol',1e-3);
Even after around 8 hours, it did not terminate! I would appreciate if you could help me again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

제품

태그

질문:

may
2013년 9월 19일

댓글:

may
2013년 9월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by