How to ensure that dynamically create symbolically variables are real?

Hello,
I'm creating symbolic variables dynamically and I wanted to ensure that the symbolic variables are real.
I have the code
clear all
index = {'1'};
List = {'A','B','C'};
A=sym(strcat(List,index))
How can I modify this to make sure that the symbolic variables stored in A are real? I have tried changing to A=sym(strcat(List,index),'real'), but this doesn't work.
Thank you, Kevin

1 件のコメント

Kevin Bachovchin
Kevin Bachovchin 2014 年 2 月 14 日
編集済み: Kevin Bachovchin 2014 年 2 月 14 日
Al, B1, and C1 can be declared to be real syms using the following syntax.
syms A1 B1 C1 real
A = [A1 B1 C1]
It seems to be there should be some equivalent way when declaring the syms dynamically.

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 14 日

0 投票

I don't think you can, for example force the variable A1 to be real. If
A1=5+j
Then
index = {'1'};
List = {'A','B','C'};
A=sym(strcat(List,index))
out=subs(A(1))
The result is a complex number

5 件のコメント

Kevin Bachovchin
Kevin Bachovchin 2014 年 2 月 14 日
編集済み: Kevin Bachovchin 2014 年 2 月 14 日
I'm not setting a value for A1, just solving for things symbolically in terms of A1, B1, and C1. Since the symbolic variables are not declared as real, I'm getting unwanted conjugates in my solution.
You can declare
syms A1 B1 C1 real
A = [A1 B1 C1]
and all syms are real. There should be some way to do the same thing with declaring syms dynamically, right? (My actual code is much more complex and involves many more variables than this example so declaring symobolic variable names dynamically saves a ton of time for the user.)
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 14 日
n=3
a=sprintf('A%d ',1:n)
eval(['syms ' a ' real'])
eval(['A=[' a ']'])
Kevin Bachovchin
Kevin Bachovchin 2014 年 2 月 14 日
Is there a way to make this work for A1, B1, and C1? I can't assume the list entered by the user is going to be a numerically increasing string like that.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 14 日
s=regexp('A':'Z','.','match')
n=3
a=strjoin(strcat(s(1:n),'1'))
eval(['syms ' a ' real'])
eval(['A=[' a ']'])
Kevin Bachovchin
Kevin Bachovchin 2014 年 2 月 14 日
I can't assume that the list is going to be alphabetic either. Basically the list can be whatever cell array the user inputs. Is there a way to do something like
index = {'1'};
List = {'A','B','C'};
A=sym(strcat(List,index))
out=subs(A(1))
and just ensure the symbolic variables are real for the purposes of symbolic math that comes later.
Kevin Bachovchin
Kevin Bachovchin 2014 年 2 月 20 日

0 投票

clear all
index = {'1'};
List = {'A','B','C'};
A=sym(sym(strcat(List,index)),'real')

2 件のコメント

Walter Roberson
Walter Roberson 2014 年 2 月 21 日
Not if the variable names are not all the same number of characters.
Kevin Bachovchin
Kevin Bachovchin 2014 年 2 月 21 日
It seems to work fine even when the variables names in List have different number of characters.
clear all
index = {'1'};
List = {'AR','BRR','C'};
A=sym(sym(strcat(List,index)),'real')

この質問は閉じられています。

タグ

質問済み:

2014 年 2 月 14 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by