Using the integral function for large upper limit
Show older comments
how do I know if integral(fun,x0,inf) is being accurate or not? Ive noticed that replacing inf by a very large number (given i know the fun decays sharply with x) gives a different and smaller answer? Why is this the case and what kind of magic is matlab doing when I give it an infinite limit?
Also, I notice a rescaling of my variables gives a different answer still. In SI units the lower bound x0 is of order 10^15. I have rescaled my units such that x0 is of order 1. Each gives a very different answer (yes i have accounted for converting back the units)...which can i trust?
8 Comments
Torsten
on 8 Jan 2022
Play with the tolerances AbsTol and RelTol. Choose these parameters such that the value for the integral does not change much if you tighten them.
will steel
on 8 Jan 2022
Torsten
on 9 Jan 2022
I don't know how the error is estimated in "integral", but to request an absolute error of 1e-6 for an integral with a very high value (e.g. 10000 or something) is of course inadequate and not to accomplish. So scaling the function to be integrated is recommended.
David Goodmanson
on 9 Jan 2022
Hi will,
could you supply an example integrand and limits of integration for the first paragraph in the question, and also for the SI case that does not work in the second paragraph? In the SI case it sounds, as Torsten mentioned, that 1e-6 is just too small a tolerance when the independent variable is as large as it its. As a hand-waving argument, 1e-6/3e15 is about 3e-22, which is far too small a relative number to be handled by double precision numbers which are good down to about a part in 10^16
will steel
on 9 Jan 2022
David Goodmanson
on 9 Jan 2022
Hi will,
this all can be reduced to two specific situations with the integral function. Your integral falls off like 1 / ( x*exp(x) ), but x varies slowly enough that the effects show up with just the use of exp(-x).
[1] inf replaced by a large number
fun = @(x) exp(-x);
integral(fun,1,inf)
ans = 0.3679 % 1/e, correct
integral(fun,1,1e7)
ans = 0.3679 % correct
integral(fun,1,1e8)
ans = 1.8175e-22
I have wondered in the past why that is.
[2] Scaling the variable of integration. Should still give 1/e. Note that at the lower limit of integration, the argument of exp is always -1, so it's not like exp is immediately forced into overflow or underflow..
fun = @(x,A) A*exp(-A*x);
A = 1; integral(@(x) fun(x,A),1/A,inf)
ans = 0.3679 % correct
other values of A:
A = 1e-13
ans = 0.3679 % correct
A = 1e-14
Warning: Minimum step size reached near x = 1e+14, etc.
ans = 1.0972e-07
A = 1e8
ans = 0.3679 % correct
A = 1e9
ans = 1.6570e-77
so a fair amount of scaling works. In your case the equivalent of A is h/(kT) = 2.3990e-15 which looks to be too small to work.
will steel
on 9 Jan 2022
Torsten
on 10 Jan 2022
@will steel You might also test MATLAB's "quad" for integration. "Old" does not always mean "Bad" :-)
Answers (0)
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!