确定输入"conscalc: calc"的类型时出错。不能联合编码器。具有不同字段集的结构类型

Error determining type for input 'conscalc: calc '. Cannot union coder.StructTypes with different sets of fields

本文关键字:类型 编码器 结构 字段 输入 calc 不能 出错 conscalc      更新时间:2023-10-16

当我尝试将包含多个函数的 Matlab 程序转换为 Matlab 编码器应用程序C++程序时,我收到一个错误,说: 在此处输入图像描述。变量 calc 是 Matlab 中的一个结构体。但是,如果我尝试函数 conscalc 本身,则完全没有问题。 问题出在哪里?

以下是函数 conscalc 的代码:

function [calc] = conscalc(rho, gv, calc)
tp = 1 + rho;
sqrt2 = sqrt(2);  
consadj = 2 ^ (0.5 * (1 - calc.alpha));
% initialization;
cv = zeros(gv.nacatlim, 1); % consumption equivalents of future expected marginal utility;
yac = zeros(gv.nacatlim, 1); % income (y) - end of period asset (a) - optimal consumption given end of period asse (c);
margu = zeros(gv.nvcatlim, gv.nacatlim); % marginal utility next period by next period survival status (1 - 3) and initial asset (1 - gv.nacatlim);
cons = zeros(gv.T - gv.beginage + 1, gv.nvcatlim, gv.nacatlim); % optimal consumption at the current period by age (25 - 99), survival status (1 - 3) and initial asset (1 - gv.nacatlim);
% simplified backward induction;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculate optimal consumption and marginal utility for the terminal age %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the optimal decision is to consume everything at the terminal age;
for vcat = 1: 3; % survival status;
for acat = 1 : gv.nacatlim; % asset;
y = calc.acats(acat, 1) + calc.income(vcat, gv.T - gv.beginage + 1); % total resources in the last period given initial asset and income;
mu = y ^ (calc.alpha - 1); % marginal utility given next period survival status and initial asset; 
if vcat == 1; % married couple adjustment;
mu = consadj * mu;
end;
% save to marginal utility next period (when calculating backward to age - 1) for later calculations;
margu(vcat, acat) = mu;
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% calculate optimal consumption and marginal utility for ages gv.t to gv.T - 1 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for age = gv.T - 1 : -1 : gv.beginage; % age;
for vcat = 1 : 3; % survival status;
y = calc.income(vcat, age - gv.beginage + 1); % income given survival status and age;
% calculate expected marginal utility next period given current period end of period asset;
for acat = 1 : gv.nacatlim; % asset;
mu = 0; % expected marginal utility next period given current period survival status and end of period asset;
for rcat = 1 : gv.nrcatlim; % asset return shock;
mur = 0; % marginal utility next period given current period survival status, end of period asset and asset return shock;
% (end of period asset + saving) * asset return shock is asset next period;
% interpolation;
% find corresponding asset grid point of the next period initial asset given current period end of period asset and asset return shock;
acatf = floor(calc.rtransa(acat, rcat));
if acatf >= gv.nacatlim;
acatf = gv.nacatlim - 1;
end;
fa = calc.rtransa(acat, rcat) - acatf;
for vcatt = 1 : 3; % survival status next period;
if vcatt == 1 || (vcat == 1 && age >= gv.surviveage); % the codes are not right. if vcat == 2/3, the program uses margu(1, acatf); should use margu(2/3, acatf); ??? 

mu0 = margu(vcatt, acatf);
mu1 = margu(vcatt, acatf + 1);
if mu0 <= 0 || mu1 <= 0;
fprintf('Interpolaton Error: Bad mu in rho section: %2d %2d %14.6e %14.6e %2d %2d %2d', ...
calc.obs, age, mu0, mu1, vcat, acat, rcat);
return;
end;
if fa <= 1;
murv = (1 - fa) * mu0 + fa * mu1;
else
if mu0 > mu1;
dmu = mu0 - mu1;
mufact = dmu / mu0;
murv = mu1 / (1 + (fa - 1) * mufact);
else
murv = mu1;
end;
end;
if vcat == 1 && age >= gv.surviveage; % both spouses alive;
mur = mur + calc.transrate(vcatt, age - gv.beginage + 1) * murv;
else
mur = mur + murv;
end;
end;
end;
mu = mu + calc.rprob(rcat, 1) * mur;
end;
% marginal utility this period should equal to the discounted expected marginal utility next period;
% convert optimal discounted expected marginal utility back to consumption level;
if vcat == 1; % both spouses alive;
cv(acat, 1) = sqrt2 * (mu / tp) ^ (1 / (calc.alpha - 1));
elseif vcat == 2 || vcat == 3; % only one spouse alive;
cv(acat, 1) = (mu * calc.srate(vcat - 1, age - gv.beginage + 1) / tp) ^ (1 / (calc.alpha - 1));
end;
yac(acat, 1) = y - calc.acats(acat, 1) - cv(acat, 1); % income - end of period asset - consumption;
end;
% find optimal consumption at the current period given initial asset;
k = 1; % initialize asset grid point;
for acat = 1 : gv.nacatlim; % asset;
nassets = - calc.acats(acat, 1); % - initial asset level at the current period;
% find how much asset left after consumption;
% - asset(t) = income - end of period asset(t) - optimal consumption(t) given end of period asset(t); 
% interpolation;
if yac(k, 1) < nassets;
k = k - 1;
while k >= 1 && yac(k, 1) < nassets;
k = k - 1;
end;
if k < 1; % optimal to leave no assets to next period;
f = 0;
k = 1;
elseif k >= 1;
f = (yac(k, 1) - nassets) / (yac(k, 1) - yac(k + 1, 1));
end;
elseif yac(k, 1) >= nassets;
while k < gv.nacatlim && yac(k + 1, 1) >= nassets;
k = k + 1;
end;
if k > gv.nacatlim - 1; % requires extrapolation;
k = gv.nacatlim - 1;
if cv(k + 1, 1) > cv(k, 1);
f = (yac(k, 1) - nassets) / (yac(k, 1) - yac(k + 1, 1));
else
f = 1 + (yac(k + 1, 1) - nassets) / (calc.acats(k + 1, 1) - calc.acats(k, 1));
end;
elseif k <= gv.nacatlim - 1;
f = (yac(k, 1) - nassets) / (yac(k, 1) - yac(k + 1, 1));
end;
end;
c = y + calc.acats(acat, 1) - ((1 - f) * calc.acats(k, 1) + f * calc.acats(k + 1, 1)); % optimal consumption at the current period;
% calculate marginal utility at the current period given optimal consumption;
if vcat == 1; % married couple adjustment;
mu = consadj * c ^ (calc.alpha - 1);
elseif vcat == 2 || vcat == 3; 
mu = c ^ (calc.alpha - 1);
end;
% save optimal consumption to corresponding optimal consumption matrix for later calculations; 
cons(age - gv.beginage + 1, vcat, acat) = c; % optimal consumption at the current period;
margu(vcat, acat) = mu; % marginal utility next period (when calculating backward at age - 1), given survival status and initial asset;
end;
end;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% assign the values to structure variable calc for future calculations %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
calc.cons = cons;

end

该错误表明,在自动输入类型定义期间,使用具有不同字段集的结构调用函数。

若要进行调试,请在 MATLAB 的入口点函数中放置断点,然后运行输入定义测试脚本。记下传入的结构,以查看不匹配的来源。