My symbolic variable is not recognised

14 visualizaciones (últimos 30 días)
jeremy
jeremy el 14 de Oct. de 2023
Respondida: SAI SRUJAN el 25 de Oct. de 2023
I have the symbolic toolbox and syms normally works and it works on part of the code but says unrecognised in the bottom part of the code.
In the start of the code i put:
zoj="syms";
for w0=1:N-1 % N is any number
zoj=strcat(zoj," golp",string(w0));
end
eval(zoj);
Then I made a symbolic column matrix u2 with the symbols glop1 to glopN-1 where there was no error. I also made a function pagetimes,in a different file but in the same folder, which outputs a N-1*N-1 matrix as pagemtimes did not work for symbolic matrix. DD is a N-1*N-1*N-1 matrix. The functions used in the code work with no error and the variables and matrices are defined.
Then I did this:
seq10="glop1"
for w2=2:N-1
seq10=strcat(seq10,",glop",string(w2));
end
seq1="["
for w3=1:N-1
seq1=strcat(seq1," diff((MM2+(1/2)*(T1)*SS2*a)*u2+pagetimes(((1/2)*(T1)*DD),u2)*u2-(MM2-(1/2)* (T1)*SS2*a)*u00+pagetimes(((1/2)*(T1)*DD),u00)*u00-(1/2)*(T1)*(fvs((j11-1)*(T1),a)+fvs(j11*(T1),a)),glop",string(w3),")"); % This is to make a Jacobian square matrix out of column matrices in string form.
end
seq1=strcat("seq11(",seq10,")=",seq1,"];seq11=matlabFunction(seq11)")% This is to make a function to input numbers into the seq11 function later.
eval(seq1)
The red text is:
Error using eval
Unrecognized function or variable 'glop1'.
Error in function (line 137)
eval(seq1)
  4 comentarios
jeremy
jeremy el 14 de Oct. de 2023
I will try it i did not know that a sym array can be made like that
jeremy
jeremy el 14 de Oct. de 2023
The code I showed is only part of the full code. The part that caused the error.

Iniciar sesión para comentar.

Respuestas (1)

SAI SRUJAN
SAI SRUJAN el 25 de Oct. de 2023
Hi Jeremy,
I understand that you are facing an issue in creating an array of "sym" variables.
You can follow below given example to resolve the issue,
syms a [1 4]
a
a = 
Dynamically accessing variable names can negatively impact the readability of your code and can cause it to run slower by preventing MATLAB from optimizing it as well. The most common alternative is to use simple and efficient indexing.
Please refer to the following documentation for a comprehensive understanding of the process to create an array of variables in MATLAB, as well as the rationale behind avoiding the use of the "eval" function.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by