How to avoid NaN with numerical integration (quadrature)
Ältere Kommentare anzeigen
Hi, I have written a program that I divided in several functions.
"*puzzle*" is the main function. But my question is related to a problem I have with quadv(Look in "*LnpdFut*" function).
s_ is a parameter of "*pdFut*" that I passed as a global to "*Lnpdfut*" function, the argument of integration being v.
In the loop in "*LnpdFut*", s_ takes successively values of the vector sglob_, but for the first two values of sglob_, it doesn't work. I get NaN.
Could someone help me figure out the reason? I know it's long but just run it, you can see for yourself.
function puzzle
global lnpdc_ sglob_ gamma_ g_ G_ rf_ phi_ sigmav_ sigmaw_ rho_ Sbar_ sbar_ smax_ delta_ LimInf_ LimSup_ ndivs_
gamma_= 2;
g_= 0.0174;
G_= exp(g_);
rf_ = 0.023;
phi_= 0.9016;
sigmav_= 0.01186;
sigmaw_= 0.465;
rho_= 0.3;
Sbar_= sigmav_*(gamma_/(1-phi_))^(0.5);
sbar_= log(Sbar_);
smax_= sbar_ + (1-Sbar_^2)/2;
delta_ = 1/exp(rf_)*G_^(gamma_)*exp(-gamma_*(1-phi_)/2);
ndivs_ = 15;
LimInf_ = -3*sigmav_;
LimSup_ = 3*sigmav_;
%Main function puzzle body
sglob_ = Setgrid(smax_,sbar_,ndivs_); % Grille
fprintf('sglob_ est %d x %d\n', size(sglob_,1),size(sglob_,2));
lnpdc_ = zeros(length(sglob_),1);
pdcInt = Lnpdfut(sglob_); % intègre numériquement
fprintf('pdcInt:%d x %d\n',size(pdcInt,1),size(pdcInt,2));
disp(pdcInt);
%==================================================================
function k = Lnpdfut(sg)
%Calls Futpd (below) and uses quadv
global s_ nwpdc_
nwpdc_ = zeros(length(lnpdc_),1);
j=1;
while(j<=length(lnpdc_))
s_= sg(j);
nwpdc_(j)= quadv(@Futpd,LimInf_,LimSup_); %integration sur v
j=j+1;
end
k = nwpdc_;
end
%------------------------------------------------------------------
function pdFut = Futpd(v)
%calls Transit belows
global s_
pdFut = delta_*G_^(1-gamma_).*exp(-gamma_*(Transit(s_,v)-s_.*ones(length(Transit(s_,v)),1))).*...
exp((1-gamma_)*v).*(1 + exp(interp1q(sglob_,lnpdc_,Transit(s_,v)))).*pdf('norm',v,0,sigmav_);
end
%------------------------------------------------------------------
function Futstate = Transit(s,v)
%Calls Lam below
Futstate =(1-phi_)*sbar_ + phi_.*s + Lam(s).*v;
if max(Futstate >=smax_)==1,
Futstate = (Futstate<smax_).*Futstate + (Futstate>=smax_).*smax_;
end
end
%------------------------------------------------------------------
function lambda = Lam(s)
express= 1-2*(s-sbar_);
express =(express>=0).*express;
express= express.^(0.5);
express= (1./Sbar_).*express-1;
express= (s<=smax_).*express;
lambda = express;
end
%------------------------------------------------------------------
function s_etat = Setgrid(smx,sb,nd)
s_etat = 0:exp(smx)/nd:exp(smx); %donne (ndivs+1) points
s_etat = log(s_etat(2:end));
if max(smx==s_etat)== 0,
s_etat = horzcat(s_etat,smx);
end
if max(sb==s_etat)== 0,
s_etat = horzcat(s_etat,sb);
end
s_etat = sort(s_etat);
s_etat = s_etat';
end
end
Akzeptierte Antwort
Weitere Antworten (1)
Kategorien
Mehr zu Entering Commands finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!