Error using syms and subs

Hi all,
I'm trying to run the code that was writen in 2009 and I have this error:
Error using sym>convertChar (line 1537)
Character vectors and strings in the first argument can only specify a variable or number. To evaluate character vectors and strings representing symbolic expressions, use 'str2sym'.
Error in sym>tomupad (line 1253)
S = convertChar(x);
Error in sym (line 220)
S.s = tomupad(x);
Error in subs (line 68)
r_unique_name = subs(sym(f_unique_name),varargin{:});
The code is:
clear all
clear all;
close all;
global A1 AG AH AF rho rhod sigma alpha bet gamma eps theta deltaK deltaH Omega omeg omega0 omega1 omega3 omega4 sigma
syms c_t x_t s_t l_t f_t a_t R_t pi_t k_t m_t gh_t z_t u_t v_t
syms r_t w_t F_t H2_t
CALIB_NCC;
SYMLOG_NCC;
FINSOL_NCC;
IMP_NCC_pi;
%SIM_NCC_pi;

4 comentarios

Walter Roberson
Walter Roberson el 20 de Mayo de 2020
the code you posted is fine in itself.
ninu
ninu el 21 de Mayo de 2020
but why this error occured?
Walter Roberson
Walter Roberson el 21 de Mayo de 2020
please post the complete error message including showing the position of the error.
The code you posted does not invoke subs() unless CALIB_NCC; SYMLOG_NCC; FINSOL_NCC; IMP_NCC_pi; are functions or scripts that invoke it. We cannot guess what those might happen to contain.
ninu
ninu el 21 de Mayo de 2020
This is the complete message and I attached and SYMLOG_NCC:
Error using sym>convertChar (line 1537)
Character vectors and strings in the first argument can only specify a variable or number. To evaluate character vectors and strings representing symbolic expressions, use
'str2sym'.
Error in sym>tomupad (line 1253)
S = convertChar(x);
Error in sym (line 220)
S.s = tomupad(x);
Error in subs (line 68)
r_unique_name = subs(sym(f_unique_name),varargin{:});
Error in SYMLOG_NCC (line 26)
eqI = subs(eqI,{'Ra1','a','w','F','R'},{Ra1,a,w,F,R});
Error in RBC_NCC (line 14)
SYMLOG_NCC;
This is for subs() error. but I think these errors are not related to my code, can it comes from matlab compatibility?
if nargin == 1 && isa(f_unique_name,'char')
cmd_unique_name = sprintf('subs(sym(''%s''))',f_unique_name);
r_unique_name = evalin('caller',cmd_unique_name);
else
r_unique_name = subs(sym(f_unique_name),varargin{:});
end

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Mayo de 2020

0 votos

Replacement code.
It seems likely to me that the other files would also have to be editted .
The key changes:
  1. everywhere there is a quoted expression that will be used symbolically, use str2sym() around the expression:
  2. replace simple() with simplify()
% SYMLOG_NCC
% symbolic log-linearization
% solution in x,c,R,a,f,s,l,pi,m',k',gh'
% reduced system by 1; f=c*F
eqI = str2sym('-x_t+R*a/Ra1*(R_t+a_t)+w*F/Ra1*(w_t+F_t)-w_t+c_t'); % (I)
eqIIL = str2sym('-theta*c_t+alpha*(1-theta)*x_t-R*a/Ra1*(R_t+a_t)-w*F/Ra1*(w_t+F_t)+theta*gh_t'); % LHS of (II)
eqIIR = str2sym('-theta*c_t+alpha*(1-theta)*x_t-R*a/Ra1*(R_t+a_t)-w*F/Ra1*(w_t+F_t)+r1*r_t'); % RHS of (II)
eqIIIL = str2sym('(1-theta)*c_t+(alpha*(1-theta)-1)*x_t-H2_t+theta*gh_t'); % LHS of (III)
eqIIIR = str2sym('(1-theta)*c_t+(alpha*(1-theta)-1)*x_t-H2_t+H21*(H2_t-x/(1-x)*x_t)'); % RHS of (III)
eqIV = str2sym('-a1*a_t+F_t+w_t-R_t'); % (IV)
eqVL = str2sym('c_t+a_t'); % LHS of (V)
eqVR = str2sym('m_t');
eqVI = str2sym('-R_t+(pi1*pi_t+r1*r_t)/R1'); % (VI)
eqVII = str2sym('-F_t-1/gamma*a1*a_t-1/gamma*v_t'); % (VII)
eqVIII = str2sym('-f_t+F_t+c_t'); % (VIII)
eqIX = str2sym('-w_t+(1-bet)*(k_t+s_t-l_t)+z_t'); % (IX)
eqX = str2sym('-r_t+(-bet*(s_t-l_t+k_t)+z_t)/r*(r+deltaK)'); % (X)
eqXI = str2sym('-pi1*pi_t+u_t+m_t-gh_t-c_t-a_t'); % LHS of (XI)
eqXIIL = str2sym('y_h*((1-bet)*(k_t+s_t)+bet*l_t+z_t)+(1-deltaK)*k_h*k_t-c_h*c_t-k_h*gh*gh_t'); % LHS of (XII)
eqXIIR = str2sym('k_h*gh*k_t'); % RHS of (XII)
eqXIII = str2sym('H_h*((1-eps)*(k_t-s1*s_t)-eps*(x1*x_t+L1*l_t+f1*f_t))-gh*gh_t'); % LHS of (XIII)
eqXIV = str2sym('-(1+s1)*s_t+x1*x_t+(1+L1)*l_t+f1*f_t'); % (XIV)
eqXV = str2sym('-H2_t+(1-eps)*(k_t-s1*s_t+x1*x_t+L1*l_t+f1*f_t)'); % (XV)
eqI = subs(eqI,{'Ra1','a','w','F','R'},{Ra1,a,w,F,R});
eqIIL = subs(eqIIL,{'alpha','theta','Ra1','a','w','F','R'},{alpha,theta,Ra1,a,w,F,R});
eqIIR = subs(eqIIR,{'alpha','theta','Ra1','a','w','F','R','r1'},{alpha,theta,Ra1,a,w,F,R,r1});
eqIIIL = subs(eqIIIL,{'alpha','theta'},{alpha,theta});
eqIIIR = subs(eqIIIR,{'alpha','theta','H21','x'},{alpha,theta,H21,x});
eqIV = subs(eqIV,{'a1'},{a1});
eqVI = subs(eqVI,{'r1','R1','pi1'},{r1,R1,pi1});
eqVII = subs(eqVII,{'gamma','a1'},{gamma,a1});
eqIX = subs(eqIX,{'bet'},{bet});
eqX = subs(eqX,{'bet','deltaK','r'},{bet,deltaK,r});
eqXI = subs(eqXI,{'pi1'},{pi1});
eqXIIL = subs(eqXIIL,{'bet','y_h','deltaK','k_h','c_h','gh'},{bet,y_h,deltaK,k_h,c_h,gh});
eqXIIR = subs(eqXIIR,{'k_h','gh'},{k_h,gh});
eqXIII = subs(eqXIII,{'H_h','eps','s1','x1','L1','f1','gh'},{H_h,eps,s1,x1,L1,f1,gh});
eqXIV = subs(eqXIV,{'s1','x1','L1','f1'},{s1,x1,L1,f1});
eqXV = subs(eqXV,{'s1','x1','L1','f1','eps'},{s1,x1,L1,f1,eps});
w_t = simplify(solve(eqIX,'w_t')); %
F_t = simplify(solve(eqVII,'F_t'));
H2_t = simplify(solve(eqXV,'H2_t'));
r_t = simplify(solve(eqX,'r_t'));
eqVIII = subs(eqVIII,{'F_t'},{F_t});
f_t = simplify(solve(eqVIII,'f_t'));
H2_t = subs(H2_t,{'f_t'},{f_t});
eqXIII = subs(eqXIII,{'f_t'},{f_t});
gh_t = simplify(solve(eqXIII,'gh_t'));
eqVI = subs(eqVI,{'r_t'},{r_t});
R_t = simplify(solve(eqVI,'R_t'));
eqVI = subs(eqVI,{'r_t'},{r_t});
eqXI = subs(eqXI,{'gh_t'},{gh_t});
pi_t = simplify(solve(eqXI,'pi_t'));
R_t = subs(R_t,{'pi_t'},{pi_t});
eqIV = subs(eqIV,{'F_t','w_t','R_t'},{F_t,w_t,R_t});
a_t = simplify(solve(eqIV,'a_t'));
%eqV = subs(eqV,{'a_t'},{a_t});
% c_t = simplify(solve(eqV,'c_t'));
% a_t = subs(a_t,{'c_t'},{c_t});
%f_t = subs(f_t,{'a_t','c_t'},{a_t,c_t});
f_t = subs(f_t,{'a_t'},{a_t});
eqXIV = subs(eqXIV,{'f_t'},{f_t});
s_t = simplify(solve(eqXIV,'s_t'));
a_t = subs(a_t,{'s_t'},{s_t});
%c_t = subs(c_t,{'s_t'},{s_t});
R_t = subs(R_t,{'s_t','a_t'},{s_t,a_t});
w_t = subs(w_t,{'s_t'},{s_t});
F_t = subs(F_t,{'a_t'},{a_t});
r_t = subs(r_t,{'s_t'},{s_t});
eqI = subs(eqI,{'w_t','F_t','R_t','a_t'},{w_t,F_t,R_t,a_t});
x_t = simplify(solve(eqI,'x_t'));
s_t = subs(s_t,{'x_t'},{x_t});
%c_t = subs(c_t,{'x_t'},{x_t});
a_t = subs(a_t,{'x_t'},{x_t});
f_t = subs(f_t,{'x_t','s_t'},{x_t,s_t});
w_t = subs(w_t,{'x_t'},{x_t});
F_t = subs(F_t,{'x_t'},{x_t});
H2_t = subs(H2_t,{'s_t','a_t','x_t'},{s_t,a_t,x_t});
gh_t = subs(gh_t,{'s_t','f_t','x_t','a_t'},{s_t,f_t,x_t,a_t});
R_t = subs(R_t,{'x_t'},{x_t});
r_t = subs(r_t,{'x_t'},{x_t});
pi_t = subs(pi_t,{'s_t','a_t','x_t'},{s_t,a_t,x_t});
% solution in k,m,m1,l,z,u,v
eqIIL = subs(eqIIL,{'x_t','R_t','a_t','w_t','F_t','gh_t'},{x_t,R_t,a_t,w_t,F_t,gh_t});
eqIIR = subs(eqIIR,{'x_t','R_t','a_t','w_t','F_t','r_t'},{x_t,R_t,a_t,w_t,F_t,r_t});
eqIIIL = subs(eqIIIL,{'x_t','H2_t','gh_t'},{x_t,H2_t,gh_t});
eqIIIR = subs(eqIIIR,{'x_t','H2_t'},{x_t,H2_t});
eqXIIL = subs(eqXIIL,{'gh_t','s_t','x_t','f_t'},{gh_t,s_t,x_t,f_t});
eqVL = subs(eqVL,{'a_t'},{a_t});
eqVR = subs(eqVR,{'m_t'},{m_t});

6 comentarios

ninu
ninu el 21 de Mayo de 2020
Thank you very much!
This error was solved but another appeared:
Error using sym/solve (line 240)
No value was given for 'w_t'. Name-value pair arguments require a name followed by a value.
Error in SYMLOG_NCC (line 40)
w_t = simplify(solve(eqIX,'w_t')); %
Error in RBC_NCC (line 14)
SYMLOG_NCC;
Walter Roberson
Walter Roberson el 21 de Mayo de 2020
w_t = simplify(solve(eqIX,sym('w_t')));
and likewise on other lines with solve(), replace the quoted variable names with sym() around the quoted variable name.
The code has weaknesses in the way that it uses variable names for symbolic purposes but also wants the same variable to be numeric. Fixing that is not hard, but it requires more careful analysis of the code, more thought, whereas I can instead just tell you the mechanical rule of putting sym() around the quoted variable names.
ninu
ninu el 21 de Mayo de 2020
Walter, it really warked for this part of the code.
But when I triyed to do the same steps for next part this error occured:
Error using sym>tomupad (line 1268)
Unable to convert 'struct' to 'sym'.
Error in sym (line 220)
S.s = tomupad(x);
Error in sym/privResolveArgs (line 1005)
argout{k} = sym(arg);
Error in sym/vertcat (line 14)
args = privResolveArgs(varargin{:});
Error in FINSOL_NCC (line 47)
vv_t = [k_t;m_t;c_t;l_t;z_t;u_t;v_t];
Do you know if it is related to sym() chnages?
Walter Roberson
Walter Roberson el 21 de Mayo de 2020
I speculate that somewhere there is a solve() for more than one variable but with one output. That would return a struct() of variables. If the code did not expect that, it might try to put the struct into part of a vector of symbols.
Unfortunately I do not seem to find source code for that function on the Internet.
ninu
ninu el 21 de Mayo de 2020
Yes, you are right, here solve() is for more than one variable:
c_t = solve(EQ(1),sym('c_t'));
EQ(2) = subs(EQ(2),{'c_t'},{c_t});
EQ(3) = subs(EQ(3),{'c_t'},{c_t});
m_t = solve(EQ(2),sym('m_t'));
EQ(3) = subs(EQ(3),{'m_t'},{m_t});
l_t = solve(EQ(3),sym('l_t'));
m_t = subs(m_t,{'l_t'},{l_t});
c_t = subs(c_t,{'l_t','m_t'},{l_t,m_t});
Walter Roberson
Walter Roberson el 21 de Mayo de 2020
I think I will need the other files as well.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 20 de Mayo de 2020

Comentada:

el 21 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by