My symbolic variable is not recognised

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 Comments

Dynamically naming variables and using eval is a bad idea, you should have a read here - TUTORIAL: Why Variables Should Not Be Named Dynamically (eval)
What you are doing is inefficient and difficult to work with. I have no idea what you are trying to do.
Can you share the mathematical equations/model that you are trying to code?
I too can't follow all of the code.
This first part seems like a complicated way to the create individual sym variables indexed by name.
N = 4;
zoj="syms";
for w0=1:N-1 % N is any number
zoj=strcat(zoj," golp",string(w0));
end
eval(zoj);
zoj
zoj = "syms golp1 golp2 golp3"
syms
Your symbolic variables are: golp1 golp2 golp3
I suspect it might be easier and more logical to make golp a sym array and use standard Matlab indexing and matrix/vector operations to develop the code.
golp = sym('golp',[1 3])
golp = 
I will try it i did not know that a sym array can be made like that
The code I showed is only part of the full code. The part that caused the error.

Sign in to comment.

Answers (1)

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.

Products

Release

R2023a

Asked:

on 14 Oct 2023

Answered:

on 25 Oct 2023

Community Treasure Hunt

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

Start Hunting!