when I enter n = 10, it reports an error Invalid second data argument
古いコメントを表示
function f=vedangsong()
syms t n i w f(t)
Ao=input('nhap Ao: ');
n=input('nhap so bac hai n: ');
w=input('nhap tan so goc w: ');
for i = 1:n
tb1=['nhap A',num2str(i),': ' ];
A(num2str(i))=input(tb1);
tb2=['nhap B',num2str(i),': ' ];
B(num2str(i))=input(tb2);
end
f(t)=0
for i=1:n
s=sin(2*pi*i*w*t)
c=cos(2*pi*i*w*t)
g= A(num2str(i))*s + B(num2str(i))*c
f(t)=f(t) + g
end
f(t) =Ao + f(t)
t=linspace(0,1/w,300)
plot(t,f(t),'k')
title('Dang song trong mien thoi gian')
xlabel('Truc t')
ylabel('f(t)')
legend('f(t)')
end
2 件のコメント
Walter Roberson
2019 年 4 月 25 日
What input values should we use to test with?
Note that your statement
B(num2str(i))=input(tb2)
will not create variables named B1, B2, B3, and so on. Instead, it will be assignments to B('1'), B('2'), B('3') and so on, which is the same as assigning to B(49), B(50), B(51)
Stephen23
2019 年 4 月 25 日
This is not a good sign:
B(num2str(i)) = ...
Dynamically creating or accessing variable names is one way that beginners force themselves into writing slow, complex, buggy code. Read this to know why:
You should just use indexing. Indexing is simple, neat, easy to debug, and very efficient (unlike what you are trying to do).
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!