Symbolic environment and loops

3 views (last 30 days)
Hi all,
I am trying to use the symbolic environment to generate T symbolic objects in my function. Since T is an input, I would like to generate the symbolic objects in a loop.
I was thinking in something like this:
for j = 1:T
syms x'j' real
x'j' = expression
Where the 'j' part is there to denote each symbolic variable. For T = 3, I would have 3 symbolic objects: x1, x2, and x3
Do you know if that is possible in Matlab?
I was trying to use vectors, but the problem is that I don't know how to declare a different symbolic object for each period in my loop:
sym_vector = zeros(T1,1);
for j = 1:T
syms x real
sym_vector(j) = x
All the best,

Accepted Answer

Swetha Polemoni
Swetha Polemoni on 16 Apr 2021
Hi Bruno Souza
It is my understanding that you want to use loop variable j to create different symbolic variable names. Following code snippent might help to serve the purpose.
for j = 1:5 % line 1
symbolicvariable=strcat("x",int2str(j)); % line2
syms symbolicvariable real % line 3
end % line 4
line 2 gives variable names such as x1, x2.... xT which can be used as symbolic variable in every iteration.

More Answers (0)


Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!