how can i plot this discrete function in MATLAB
조회 수: 8 (최근 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!