how can i plot this discrete function in MATLAB
9 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 10 Dec 2021
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 Comments
Voss
on 10 Dec 2021
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.
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!