how to make symbolic variable by loop as we want

조회 수: 59 (최근 30일)
Pk Verma
Pk Verma 2022년 7월 7일
댓글: Walter Roberson 2022년 9월 27일
for i=1:4
p= str2sym(strcat('a',num2str(i)))
syms p
disp(p)
end
p = 
p
p = 
p
p = 
p
p = 
p
a1
Unrecognized function or variable 'a1'.
so if i call a1 then it says its unrecognized. so how can i make multiple variable in this format.
  댓글 수: 2
Pk Verma
Pk Verma 2022년 7월 8일
so, can i assume that in matlab there is no method for creating symbolic variable by only known is no of variables

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2022년 7월 8일
so, can i assume that in matlab there is no method for creating symbolic variable by only known is no of variables.
Sure there is, just not as individual variables (which is generally discouraged, see this Answers post for an explaination why this is discouraged and several alternative approaches.)
In this case rather than creating a1, a2, etc. or using eval just call sym.
n = 4;
A = sym('a', [1, n])
A = 
Now you can use A(1) for , A(2) for , etc.
B = A(1)+2*A(2)+A(3).^3+A(4)./4
B = 
This makes substituting values in for elements of A easy.
subs(B, A, [16 9 4 1])
ans = 
Let's check that subs gave us the correct result by performing the substitution manually.
check = 16 + (2*9) + (4^3) + (1/4)
check = 98.2500
check == 393/4
ans = logical
1

추가 답변 (3개)

Aiswarya
Aiswarya 2022년 7월 7일
The error unrecognized variable arises because str2sym does not add the variables contained in the string to the workspace. So when you try to use a1 it throws an error.
If you really want to add variables with symbolic names like a1,a2,a3,a4... then you can use a loop like:
for i=1:4
eval(sprintf('a%d = i', i));
end
Instead of the 'i' given as second argument to sprintf you can give a desired value.
However, note that eval can be slow and it is much better to use matlab arrays for this purpose (sequence or array of data). Eg:
a = zeros(1,4);
for i=1:4
a(i) = i % instead of assigning i you can replace it with your desired value
end
You can access these variables by a(1),a(2) and so on.
  댓글 수: 1
Pk Verma
Pk Verma 2022년 7월 8일
in first one code (eval) the class of a1,a2... are double not sym. they are not a symbolic variable.
so, can i assume that in matlab there is no method for creating symbolic variable by only known is no of variables.

댓글을 달려면 로그인하십시오.


Hrusheekesh
Hrusheekesh 2022년 7월 11일
Hi verma,
I understand that you want to create multiple symbolic variables with one command.
The following example will illustrate how to achieve the same
syms(sym('a', [1 10]))
Please refer to the Create symbolic variable documentation for more information on how to create symbolic variables.
  댓글 수: 2
Steven Lord
Steven Lord 2022년 7월 11일
There's no need to call both sym and syms here.
Walter Roberson
Walter Roberson 2022년 9월 27일
syms(sym()) constructs the names symbolically and puts the individual variables into the workspace. If you
syms a [1 10]
then the symbolic variables are constructed and the matlab variable a is assigned the vector of them but a1 a2 and so on are not individually added to the matlab workspace.

댓글을 달려면 로그인하십시오.


JVan
JVan 2022년 9월 27일
If you are wanting to do it in a for loop, it can also been done as folllows. Here I just chose n=3 but that was arbitrary.
n=3
n = 3
% Create a symbolic varaible for each value of n
for i=1:n
syms(['x' num2str(i)])
end
We can now call syms to see that all the variables were added
syms
Your symbolic variables are: x1 x2 x3

카테고리

Help CenterFile Exchange에서 Symbolic Variables, Expressions, Functions, and Preferences에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by