フィルターのクリア

How to ensure that dynamically create symbolically variables are real?

3 ビュー (過去 30 日間)
Kevin Bachovchin
Kevin Bachovchin 2014 年 2 月 20 日
編集済み: Kevin Bachovchin 2014 年 2 月 21 日
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.
I'm looking to do something like
syms A1 B1 C1 real
A = [A1 B1 C1]
except I want to declare the variable names dynamically.
(I can't assume that List will contain alphabetically ordered letters. It is whatever list the user inputs.)
Thank you, Kevin
  1 件のコメント
Matt Tearle
Matt Tearle 2014 年 2 月 20 日
I'm assuming you know that dynamically created variable names make for hideous programming, right? ( Obligatory link )
But maybe you have a reason, involving users and symbolic stuff, so... answer below.

サインインしてコメントする。

採用された回答

Kevin Bachovchin
Kevin Bachovchin 2014 年 2 月 20 日
clear all
index = {'1'};
List = {'A','B','C'};
A=sym(sym(strcat(List,index)),'real')
  2 件のコメント
Walter Roberson
Walter Roberson 2014 年 2 月 21 日
Now try again with variable names that are more than one character long.
Kevin Bachovchin
Kevin Bachovchin 2014 年 2 月 21 日
編集済み: Kevin Bachovchin 2014 年 2 月 21 日
It seems to work fine even when the variables names in List are more than one character.
clear all
index = {'1'};
List = {'AR','BRR','C'};
A=sym(sym(strcat(List,index)),'real')

サインインしてコメントする。

その他の回答 (2 件)

Matt Tearle
Matt Tearle 2014 年 2 月 20 日
編集済み: Matt Tearle 2014 年 2 月 20 日
This, maybe?
names = strcat(List,index,{' '})
eval(['syms ',names{:},'real'])
eval(['A = [',names{:},']'])
Ergh. I shall now go take a shower...
  2 件のコメント
Kevin Bachovchin
Kevin Bachovchin 2014 年 2 月 20 日
編集済み: Kevin Bachovchin 2014 年 2 月 20 日
I'm looking to obtain a symbolic matrix A such as
clear all
index = {'1'};
List = {'A','B','C'};
A=sym(strcat(List,index))
Is there a way I can modify this to force A to a matrix of real syms? I don't understand why A=sym(strcat(List,index),'real') doesn't work.
Matt Tearle
Matt Tearle 2014 年 2 月 21 日
編集済み: Matt Tearle 2014 年 2 月 21 日
I don't understand the objection. You said:
I'm looking to do something like
syms A1 B1 C1 real
A = [A1 B1 C1]
except I want to declare the variable names dynamically.
As far as I can see, that's exactly what my code does -- I get A1, B1, and C1 as real symbolic variables, then A is a 1x3 sym created from them.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2014 年 2 月 21 日
A(1:length(List),1) = sym(0);
for K = 1 : length(List)
A(K) = sym(List{K});
end
A = sym(A, 'real');
The behavior you are trying to use does not work because there is no functionality for sym() that even approximates it. You can pass a single string (horizontal char vector) or you can pass an existing symbolic array or you can pass a numeric array. No syntax for multiple strings. No syntax for multiple existing symbolic arrays.
  1 件のコメント
Kevin Bachovchin
Kevin Bachovchin 2014 年 2 月 21 日
The following code works fine
clear all
index = {'1'};
List = {'A','B','C'};
A=sym(sym(strcat(List,index)),'real')

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by