Problems with declaring the variable right

5 ビュー (過去 30 日間)
Jan Faber
Jan Faber 2022 年 5 月 15 日
コメント済み: Voss 2022 年 5 月 15 日
Good day everyone, sadly I got the following problem with my current task that I don't know how to define the right variable for the current calculation:
for i= 0:14
xkt1(i+1)= xg*sin(2*pi*F1*(k1.*T-i*T));
end
with:
xg= 7*2*5*1*6*3;
xh= 3/10;
F1=3;
F2=100;
T= 1/500;
xkt1=zeros(0:14);
xkt2=zeros(0:14);
k1 is the variable and I tried syms, @(k1) and k1=[ ];, sadly nothing worked and either it had problems of converting it into double, or simply wasnt able to use it correctly, if someone with more knowledge about matlab could help me, I would be really grateful.
Sincerely
Jan C. Faber

採用された回答

Voss
Voss 2022 年 5 月 15 日
Using symbolic k1 and xkt1:
syms k1 xkt1
xg= 7*2*5*1*6*3;
xh= 3/10;
F1=3;
F2=100;
T= 1/500;
for i= 0:14
xkt1(i+1)= xg*sin(2*pi*F1*(k1.*T-i*T));
end
xkt1
xkt1 = 
% evaluate xkt1(1) at k1 = 0, get a symbolic expression:
subs(xkt1(1),k1,0)
ans = 
0
% evaluate xkt1(1) at k1 = 0, get a numeric value:
double(subs(xkt1(1),k1,0))
ans = 0
% evaluate xkt1(2) at k1 = 0, get a symbolic expression:
subs(xkt1(2),k1,0)
ans = 
% evaluate xkt1(2) at k1 = 0, get a numeric value:
double(subs(xkt1(2),k1,0))
ans = -47.4896
Alternatively, using a cell array of anonymous functions:
xg= 7*2*5*1*6*3;
xh= 3/10;
F1=3;
F2=100;
T= 1/500;
xkt1=cell(1,15);
for i= 0:14
xkt1{i+1}= @(k1)xg*sin(2*pi*F1*(k1.*T-i*T));
end
xkt1
xkt1 = 1×15 cell array
{@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))} {@(k1)xg*sin(2*pi*F1*(k1.*T-i*T))}
% evaluate xkt1{1} at k1 = 0:
xkt1{1}(0)
ans = 0
% evaluate xkt1{2} at k1 = 0:
xkt1{2}(0)
ans = -47.4896
  2 件のコメント
Jan Faber
Jan Faber 2022 年 5 月 15 日
Thanks a lot, I'll definitely give those options a try, when it should've worked, then I'll mark the answer as accepted.
Sincerely
Jan C. Faber
Voss
Voss 2022 年 5 月 15 日
Sounds good. Let me know if you have any questions about it.
-_

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

その他の回答 (1 件)

Torsten
Torsten 2022 年 5 月 15 日
編集済み: Torsten 2022 年 5 月 15 日
xg= 7*2*5*1*6*3;
xh= 3/10;
F1=3;
F2=100;
T= 1/500;
fun = @(k1,i) xg*sin(2*pi*F1*(k1-i)*T);
fun(14,0:14)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by