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

5 ビュー (過去 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 Center および File ExchangeCalculus についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by