How do I improve Numerical Integration Speed?
조회 수: 12 (최근 30일)
이전 댓글 표시
Hi,
I have created a function which will loop several thousand times. I have noticed the time taken to perform the integration necessary is too long to produce the results I need. My integration method is as follows:
fun1= @(x) exp(-(abs(x)).^(1.73)).*((cos(eta1*x)));
fun2= @(x) exp(-(abs(x)).^(1.96)).*((cos(eta2*x)));
R1 =integral(fun1,0,inf);
R4 =integral(fun2,0,inf);
Here I state the function required to be integrated with respect to x and then perform the integral. How would I do this more efficiently. Thank you.
Pierce
댓글 수: 3
Walter Roberson
2017년 8월 25일
You can truncate your integration at
solve(exp(-x^1.73)==eps)
which is about 7 1/2.
However, I do not know if this will help much. In my timing tests it made no difference. If eta1 were large then it probably would make a difference.
David Goodmanson
2017년 8월 25일
Hello Peirce, some ways around this depend on the size of eta1 and eta2. What is the range of these variables?
채택된 답변
Jan
2017년 8월 25일
편집: Jan
2017년 8월 26일
eta1 = 17; % Guessed
fun1 = @(x) exp(-(abs(x)).^(1.73)).*((cos(eta1*x)));
tic
R1 = integral(fun1,0,inf);
toc
Elapsed time is 0.003751 seconds.
It is some percent faster to use a function instead of an anonymous function.
This is not that much, and it was measured on an old Core2Duo. Which speed do you need?
You can reduce the tolerance:
R1 = integral(fun1, 0, inf, 'RelTol', 1e-4, 'AbsTol', 1e-6)
This increases the speed by 10%, but of course it reduces the accuracy.
By the way: Compare these two formulations:
fun1 = @(x) exp(-(abs(x)).^(1.73)).*((cos(eta1*x)));
fun1 = @(x) exp(-(x .^ 1.73)) .* cos(eta1*x);
If the integral has the limits [0, Inf] you can omit the abs. While the two pairs of parentheses around the cos() are not useful, I'd prefer an explicit "-(x .^ y)" to emphasize, that the power has a higher precedence than the unary minus.
댓글 수: 1
Walter Roberson
2017년 8월 26일
My tests with tic/toc had it coming in at about 0.001 seconds on my system, but with timeit() had it coming in at about 0.0004 on my system.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!