Clear Filters
Clear Filters

Error using matlab.int​ernal.math​.interp1 Input coordinates must be real.

33 views (last 30 days)
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
Error using matlab.internal.math.interp1
Input coordinates must be real.

Error in interp1 (line 188)
VqLite = matlab.internal.math.interp1(X,V,method,method,Xqcol);

Error in solution>valfunsto (line 50)
g1=interp1(k0,vlast1,k,'linear');

Error in fminbnd (line 325)
fu = funfcn(x,varargin{:});
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?

Accepted Answer

Torsten
Torsten on 23 Jun 2024 at 8:41
Edited: Torsten on 23 Jun 2024 at 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 Comment
Torsten
Torsten on 23 Jun 2024 at 13:29
Edited: Torsten on 23 Jun 2024 at 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!