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 Comments

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)
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).

Sign in to comment.

 Accepted Answer

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!