Could not integral: Infinite or Not-a-Number value encountered

조회 수: 1 (최근 30일)
Chao-Zhen Liu
Chao-Zhen Liu 2018년 9월 24일
댓글: Walter Roberson 2018년 10월 2일
Hi everyone,
Does anyone can tell me what's wrong with my code? I always receives the warnings:
Warning: Infinite or Not-a-Number value encountered.
U = 14; L = 7; d = (U-L)/2;
d_star = d;
T = ( U+L )/(1+1); % symmeric
alpha = 0.10;
Le = 0.05;
for kursi = [0 0.25 0.5 1 2 3]
sigma = fzero( @(sigma) Le - (sigma./d)^2 - ( kursi.*sigma./d).^2, 1 ) ;
mu = kursi*sigma + T;
j = 1;
for n = [25 50 100 150 200]
delta = ( n.^(1/2) ).*kursi ;
B = (n*d_star^2)/sigma^2;
i= 1;
for x = 7:0.01:14
sample = normrnd(mu,sigma,1,n);
% fK = ( 2^(-(n-1)/2)/gamma((n-1)/2) ).*((B.*x.*(1-t)).^(n-3)/2 ).*exp(-B.*x.*(1-t)/2);
fun = @(t) ( sqrt( (B^3).*x./t )./2 ).*( ( 2^(-(n-1)/2) / gamma((n-1)/2) ).*( (B.*x.*(1-t)).^((n-3)/2) ).*exp(-B.*x.*(1-t)/2) ).*( normpdf(sqrt(B*x*T)+delta,0,1) + normpdf(sqrt(B*x*T)-delta,0,1) );
pdf_Lehat(j,i) = integral(@(t) fun(t),0,1);
i = i + 1;
end
j = j + 1;
end
end
x = 7:0.01:14;
plot(x, pdf_Lehat(1,:)); hold on
plot(x, pdf_Lehat(2,:)); hold on
plot(x, pdf_Lehat(3,:)); hold on
plot(x, pdf_Lehat(4,:)); hold on
plot(x, pdf_Lehat(5,:)); hold on
xlabel('X')
I guess the problem may be the handle ,fun, especially the mid part of the code (i.e. the above code, fK). Hope you can give me some advice, thanks!
  댓글 수: 9
Torsten
Torsten 2018년 9월 27일
편집: Torsten 2018년 9월 27일
In the evaluation of plotfun, you use B=4.0e4, x=14, n=200 and T=10.5.
Now specify a value for t and evaluate all parts of "plotfun" separately for these parameter values for B,x,n and T. See where there might be problems in the evaluation (e.g. gamma((n-1)/2)= gamma(199/2) seems too huge, 2^(-(n-1)/2)=2^(-199/2) seems too small).
Best wishes
Torsten.
Chao-Zhen Liu
Chao-Zhen Liu 2018년 9월 29일
편집: Chao-Zhen Liu 2018년 9월 30일
Hi Torsten,
Thanks!
I have try to use log() or gammaln() and then use exp() to make some adjustment for the part that are too small or too big but it still could not work... could you have any idea?
If you want to see my code, please tell me and I will attach it right way when I am back to my computer.
Thanks!
========================================================================
U = 14; L = 7; d = (U-L)/2;
d_star = d;
T = ( U+L )/(1+1); % symmeric
alpha = 0.10;
Le = 0.05;
for kursi = [0 0.25 0.5 1 2 3]
sigma = fzero( @(sigma) Le - (sigma./d)^2 - ( kursi.*sigma./d).^2, 1 ) ;
mu = kursi*sigma + T;
j = 1;
for n = [25 50 100 150 200]
delta = ( n.^(1/2) ).*kursi ;
B = (n*d_star^2)/sigma^2;
i= 1;
for x = 7:0.01:14
sample = normrnd(mu,sigma,1,n);
% fK = ( exp(log(2^(-(n-1)/2)))/exp(gammaln((n-1)/2)) ).*((B.*x.*(1-t)).^(n-3)/2 ).*exp(-B.*x.*(1-t)/2);
fun = @(t) ( sqrt( exp(log(B^3)).*x./t )./2 ).*( exp(log(2^(-(n-1)/2)))/exp(gammaln((n-1)/2)) ).*exp(log(((B.*x.*(1-t)).^(n-3)/2 ))).*exp(-B.*x.*(1-t)/2).*( normpdf(sqrt(exp(log(B*x*T)))+delta,0,1) + normpdf(exp(log(sqrt(B*x*T)))-delta,0,1) );
pdf_Lehat(j,i) = integral(@(t) fun(t),0.001,1);
i = i + 1;
end
j = j + 1;
end
end
Above is my code, what's wrong with my code? Thanks!

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

채택된 답변

Walter Roberson
Walter Roberson 2018년 9월 30일
The values of your integral are so small that they cannot be represented in double precision, and can barely be represented in the Symbolic Toolbox either. Values like 2*10^(-87012)
  댓글 수: 12
Chao-Zhen Liu
Chao-Zhen Liu 2018년 10월 2일
Hi Walter,
About what you question me, I do not understand because I do not know why I have a 3D array of values near -81000... but I will recheck my equations as the top priority thing!
Thanks!
Walter Roberson
Walter Roberson 2018년 10월 2일
Your term exp(-B.*x.*(1-t)/2) is responsible. The -B*x/2 is coming out at about 35000 and the 1-t flips that to about exp(-35000 *t)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by