Error using sym>convertChar

2 visualizaciones (últimos 30 días)
ninu
ninu el 14 de Mayo de 2020
Comentada: sanidhyak el 26 de Jun. de 2025
Dear all,
Can you please help me with this error. I'm trying to run DGSE model and I don't understand why I have a lor of error becaouse the code is from the author.
Error using sym>convertChar (line 1448)
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 1214)
S = convertChar(x);
Error in sym (line 211)
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});

Respuestas (1)

sanidhyak
sanidhyak el 25 de Jun. de 2025
Hi @ninu,
I understand that you are trying to run the "DGSE" model and are encountering an error related to the use of MATLAB’s symbolic toolbox, specifically the "sym" function while dealing with character vectors.
When running the code snippet you shared, I also observed a similar error. This error occurs because the input equations like:
eqI = '-x_t+R*a/Ra1*(R_t+a_t)+w*F/Ra1*(w_t+F_t)-w_t+c_t';
are being treated as variable names rather than symbolic expressions. The "sym" function expects a single variable name (e.g., "x") or number, not a full equation string. To resolve this issue, you should use the "str2sym" function, which is specifically designed to convert character vectors into full symbolic expressions.
Kindly refer to the following corrected code snippet:
% Define symbolic expressions using str2sym
eqI = str2sym('-x_t+R*a/Ra1*(R_t+a_t)+w*F/Ra1*(w_t+F_t)-w_t+c_t');
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');
% ... repeat for all eqII*, eqIII*, ..., eqXV
% Ensure all variables used are declared as symbolic
syms x_t c_t R a Ra1 w F R_t a_t w_t F_t alpha theta gh_t ...
r1 r_t H2_t H21 x a1 m_t R1 pi1 pi_t gamma v_t ...
f_t bet k_t s_t l_t z_t deltaK r u_t ...
y_h k_h c_h gh H_h eps s1 x1 L1 f1
Wrapping your equation strings with "str2sym" ensures that MATLAB interprets them correctly as symbolic equations. Also, defining all variables used with "syms" prevents further substitution or undefined variable issues later on.
For further reference, kindly refer to the following official documentations:
I hope this helps!
  2 comentarios
Walter Roberson
Walter Roberson el 25 de Jun. de 2025
Note that there was a time, before str2sym() existed (R2017b), when it used to be quite acceptable to sym() a character expression in order to convert it to a symbolic expression. That has not been valid since roughly R2019a.
sanidhyak
sanidhyak el 26 de Jun. de 2025
Yeah, absolutely right @Walter Roberson!

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by