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.

0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!