how can i plot this discrete function in MATLAB

조회 수: 8 (최근 30일)
Batuhan Samet Demirci
Batuhan Samet Demirci 2021년 12월 10일
댓글: Voss 2021년 12월 10일
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
Batuhan Samet Demirci 2021년 12월 10일
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