How to fill inside of an array using a maths function?

5 ビュー (過去 30 日間)
Okan Sen
Okan Sen 2020 年 3 月 3 日
コメント済み: Okan Sen 2020 年 3 月 3 日
Hello, I've started using MatLab today for signal processing problems. However, I can't seem to find a way to fill the array using a function then plotting it.
I need to plot
x1[n] = x1(n*Ts)
where
x1(t) = sin(2*pi. * f0 * t)
and f0 = 440
I've changed x1 function's name to y1 in the code to avoid complications. x1 is my array here.
My code might be a bit confusing since I haven't had a grasp on many of the types of variables.
The plot grid and axes are correct but I get no curve or line on the plot. The command prompt also prints the array as empty. What might I be doing wrong? Any help I get is appreciated.
f0 = 440;
Ts = 0.0001;
syms y1(t)
y1(t) = sin(2*pi.*f0*t);
x1 = []
for n = 1:3
x1(n) = y1(n*Ts);
end
t = 0:seconds(0.0001):seconds(0.01);
plot(t, x1(:,1),'r', 'LineWidth',2);
grid on;
xlabel('t');
ylabel('x1');
title('Sinusoidal Signal');
  2 件のコメント
darova
darova 2020 年 3 月 3 日
I suggest you to start from plot
There are few simple examples
Okan Sen
Okan Sen 2020 年 3 月 3 日
Thanks for the reply! I've actually read all the plot manuals but I couldn't understand the array filling and plotting. The problem has now been solve though. Thanks for your time ^^

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

採用された回答

David Hill
David Hill 2020 年 3 月 3 日
f0 = 440;
x1 = @(t)sin(2*pi.*f0*t);
t = 0:0.0001:0.01;
plot(t, x1(t),'r', 'LineWidth',2);
grid on;
xlabel('t');
ylabel('x1');
title('Sinusoidal Signal');
  2 件のコメント
Stephen23
Stephen23 2020 年 3 月 3 日
Simpler without the function:
t = 0:0.0001:0.01;
x1 = sin(2*pi.*f0*t);
plot(t, x1, ...)
Okan Sen
Okan Sen 2020 年 3 月 3 日
Thanks so much for the quick replies! It works! So we make t a vector so that its matching with x1 array. In the end it does not create any problems when plotting. Did I get it right? O.o
I tried removing the function sign(if I understood it correctly @(t) means dependant on t function) and it works as the way it did before too so yeah this is even more polished!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by