problem with taking Differentiate from variable with counter !

5 ビュー (過去 30 日間)
shahin hashemi
shahin hashemi 2018 年 4 月 26 日
コメント済み: shahin hashemi 2018 年 4 月 26 日

dear all

i need to write a code that have x symbolic variable that contain x=[x1 x2 x3 .... xn] and also x is depended to variable t (time).at last i want to Differentiate from x. for 3 or 4 x it is easy to write code like below :

>> syms x1(t) x2(t) x3(t)
>> x=[x1 x2 x3]
x(t) =
[ x1(t), x2(t), x3(t)]
>> diff(x,t)
ans(t) =
[ diff(x1(t), t), diff(x2(t), t), diff(x3(t), t)]

now when i want to write code that contain 30 x it is difficult to this in this way

i also try this but it doesnt work :

>> syms t
>> x(t) = sym('x%d',[1 4])
x(t) =
[ x1, x2, x3, x4]
>> diff(x,t)
ans(t) =
[ 0, 0, 0, 0]

is there any way to do this in this way and if it is i also need a way to call the specific x

for example :

for i=1:3
for j=1:3
xij(t)=...
end 
end 

i know writing in this way is wrong but just for clearing my idea i really appreciated if someone could help me

採用された回答

Birdman
Birdman 2018 年 4 月 26 日
編集済み: Birdman 2018 年 4 月 26 日
Well, the approach would be as follows:
n=30;
x=sym(zeros(1,n));%preallocation
syms t
for i=1:n
x(i)=str2sym(sprintf('x%d(t)',i));
end
Now, you can easily take derivative of x as
diff(x,t)
and observe the result you want.
  3 件のコメント
Birdman
Birdman 2018 年 4 月 26 日
Then use sym instead of str2sym:
n=30;
x=sym(zeros(1,n));%preallocation
syms t
for i=1:n
x(i)=sym(sprintf('x%d(t)',i));
end
shahin hashemi
shahin hashemi 2018 年 4 月 26 日
Ty for your answer mr birdman i search and figure out that str2sym add to matlab in 2017b
i use sym and i got my answer but is there any benefit in useing this command is stead of sym

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by