Error using matlab.internal.math.interp1 Input coordinates must be real.
33 ビュー (過去 30 日間)
古いコメントを表示
global vlast1 vlast2 beta delta theta k0 kt At p1 p2
hold off
hold all
vlast1=20*ones(1,40);
vlast2=vlast1;
k0=0.4:0.4:16;
kt11=k0;
kt12=k0;
beta=0.98;
delta=0.1;
theta=0.36;
A1=1.75;
p11=0.9;
p12=1-p11;
p21=0.4;
p22=1-p21;
A2=0.75;
numits=250;
for k=1:numits
for j=1:40
kt=k0(j);
At=A1;
p1=p11;
p2=p12;
%we are using a bounded function minimization routine here
z=fminbnd(@valfunsto,0.41,15.99);
v1(j)=-valfunsto(z);
kt11(j)=z;
At=A2;
p1=p21;
p2=p22;
z=fminbnd(@valfunsto,0.41,15.99);
v2(j)=-valfunsto(z);
kt12(j)=z;
end
if k/50==round(k/50)
plot(k0,v1,k0,v2)
drawnow
end
vlast1=v1;
vlast2=v2;
end
hold off
%optimal plotting prgram for the plans
%plot(k0,kt11,k0,kt12)
function val=valfunsto(k)
global vlast1 vlast2 beta delta theta k0 kt At p1 p2
g1=interp1(k0,vlast1,k,'linear');
g2=interp1(k0,vlast2,k,'linear');
kk=At*kt^theta-k+(1-delta)*kt;
val=log(kk)+beta*(p1*g1+p2*g2);
val=-val;
end
This codes from the textbook The ABCs of RBCs. Once I run them, the error-
Error using matlab.internal.math.interp1
Input coordinates must be real
keeps coming up. Is there anything wrong with the code?
0 件のコメント
採用された回答
Torsten
2024 年 6 月 23 日 8:41
編集済み: Torsten
2024 年 6 月 23 日 9:15
For some reason, the k suggested by "fminbnd" becomes complex-valued so that interpolation in function "valfunsto" is no longer possible.
Instead try
global vlast1 vlast2 beta delta theta k0 kt At p1 p2
%hold off
%hold all
hold on
vlast1=20*ones(1,40);
vlast2=vlast1;
k0=0.4:0.4:16;
kt11=k0;
kt12=k0;
beta=0.98;
delta=0.1;
theta=0.36;
A1=1.75;
p11=0.9;
p12=1-p11;
p21=0.4;
p22=1-p21;
A2=0.75;
numits=250;
interval = 0.41:0.01:15.99;
for k=1:numits
for j=1:40
kt=k0(j);
At=A1;
p1=p11;
p2=p12;
%we are using a bounded function minimization routine here
%z=fminbnd(@valfunsto,0.41,15.99);
[~,idx] = min(arrayfun(@(kk)valfunsto(kk),interval));
z = interval(idx);
v1(j)=-valfunsto(z);
kt11(j)=z;
At=A2;
p1=p21;
p2=p22;
%z=fminbnd(@valfunsto,0.41,15.99);
[~,idx] = min(arrayfun(@(kk)valfunsto(kk),interval));
z = interval(idx);
v2(j)=-valfunsto(z);
kt12(j)=z;
end
if k/50==round(k/50)
plot(k0,v1,k0,v2)
drawnow
end
vlast1=v1;
vlast2=v2;
end
hold off
%optimal plotting prgram for the plans
%plot(k0,kt11,k0,kt12)
function val=valfunsto(k)
global vlast1 vlast2 beta delta theta k0 kt At p1 p2
g1=interp1(k0,vlast1,k,'linear');
g2=interp1(k0,vlast2,k,'linear');
kk=At*kt^theta-k+(1-delta)*kt;
val=log(kk)+beta*(p1*g1+p2*g2);
val=-val;
end
1 件のコメント
Torsten
2024 年 6 月 23 日 13:29
編集済み: Torsten
2024 年 6 月 23 日 13:30
kk in function "valfunsto" becomes negative if k is big enough. Thus val becomes complex-valued. So my code suggestion from above also doesn't solve your problem.
In order for kk being positive, the search interval for k in "fminbnd" must be such that
At*kt^theta-k+(1-delta)*kt > 0
thus
k < At*kt^theta+(1-delta)*kt
Adopt the 15.99 accordingly.
その他の回答 (0 件)
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!