Decimal and very small values returning zeros
이전 댓글 표시
I am running a integral calculation which is returning all zeros.
So i am running an integral over a meshgrid for space and time which i believe is working correctly.
My time scale is 0 to 1e-11 but when i use these values I get all zeros. If I reduce the it to 0 to 1e-9 I get numerical answers.
Is Matlab having rounding errors? If so how do I tell matlab to keep the sig figs?
t=0:2e-13:9e-11;
x=0:1:5;
[time space]=meshgrid(t,x);
fun=@(c) (pi.*time).^(-1/2).*exp(-(space-c).^2./(time)).*exp(-abs(space)/1);
answer=integral(fun,-inf,inf,'ArrayValued',true)
댓글 수: 1
James Tursa
2022년 7월 19일
You will need to show us your code. There is very little meaningful advice we can give you until you do this.
채택된 답변
추가 답변 (2개)
No, Matlab uses the standard IEEE754 conventions and has no rounding errors.
The question is funny: A huge number of scientists use Matlab for decades to get reliable results. You can be sure, that such problem would have been found before.
How do you check, if the output is zero? Remember that the standard output to the command window has a limited size of decimal figures. So maybe all you need is:
format long g
See: format
x = [25 56.31156 255.52675 9876899999];
format short
x % Some zeros are shown, which are not zeros numerically:
format long g
x
Um, why are you doing this numerically at all??????? With the variable of integration as c, your kernel is:
(pi*t)^(-1/2).*exp(-(x-c).^2./t).*exp(-abs(x))
with limits from -inf to inf. c enters in there only in one place. And this will be a known integral. I'll do it effectively by hand though.
syms t real positive
syms x real
syms c
K = (pi*t)^(-1/2).*exp(-(x-c).^2./t).*exp(-abs(x))
First, I'll transform the problem to make it a little more clear.
u = (x-c)/sqrt(t)
Then we will have
du = -dc/sqrt(t)
Since the limits of integration were -inf to inf, the only impact is to swap the limits, since we have x-c, but then we have an extra factor of -1 in there in the du term. So the net result is we will still integrate over the limits [-inf,inf].
Ku = 1/sqrt(pi) * exp(-u^2)*exp(-abs(x))
But what is the integral of exp(-u^2) over -inf,inf]? (Yes, I know I can do this on paper, but this is a MATLAB forum.)
syms u
int(exp(-u^2),u,[-inf,inf])
Do you see that cancels the 1/sqrt(pi)? And since x is not a function of c, it just comes out of the integral.
The result is, EVERYTHING COLLAPSES. The value of the integral you want to compute is just
exp(-abs(x))
It is not even apparently a function of t. Using a numerical integration to solve the problem is just wrong, since you then have serious numerical problems solving it. Even if the kernel were a little more complicated, I might at worst suggest the use of a Gauss-Hermite integration. As a check, see that Bruno computed a nmerical solution.
format long g
x = (0:5)';
[x,exp(-abs(x))]
Indeed, that is the same as what Bruno found. There is no dependence on t at all.
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
