how can i plot this discrete function in MATLAB

10 次查看(过去 30 天)
Hi,
I am beginner and i dont know how can i plot a discrete function with coefficient in MATLAB. even odd.
For example this one. How can i type the code and plot it? It will be stem func that is i know.

采纳的回答

Voss
Voss 2021-12-10
First you have to define what values of n to calculate C_n for. I will use 0 to 10:
n = 0:10;
Now you can calculate C_n. First initialize C_n to be a vector the same size as n with all zeros:
C_n = zeros(size(n));
Then calculate the value of C_n where n is odd (the value of C_n where n is even is already 0 because that's how C_n was initialized, so we don't need to do anything else for that):
idx = mod(n,2) == 1; % logical index with value 'true' where n is odd and 'false' where n is even
C_n(idx) = 8/pi^2./n(idx).^2; % calculating and storing 8/pi^2/n^2 where idx is 'true', i.e., n is odd
Finally create the stem plot:
figure();
stem(n,C_n);
  2 个评论
Batuhan Samet Demirci
Thanks a lot....
Okay i got it, i want to ask one question more,
For values of n, if i want to make it for all values -infinity to + infinity,
or
for the value that i give matlab, i mean i want to enter a value and i want matlab to understand if the value is odd or even and calculate and plot it.
Voss
Voss 2021-12-10
Infinitely long vectors are not possible in MATLAB, as far as I know.
If you use a scalar n then the stem plot will have one point, but you can do that, sure. In any case, just redefine n to be what you want (instead of 0:100) in the first line of code in my answer. The rest of the code stays the same and will calculate and stem plot C_n vs n for the n you specify.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by