What is the error in this computation?

조회 수: 1 (최근 30일)
Muhammad Usman
Muhammad Usman 2020년 1월 4일
댓글: Walter Roberson 2020년 1월 4일
Here I want to plot a simple sum.
I computed it manually by iterating different values, and then did it same by using for loop, but there is some mistake, can somebody figure it out for me.
Below given is my code:
x = 0:pi/16:pi;
s = (8/(pi*(1^3)))*sin(1*x)+...
(8/(pi*(3^3)))*sin(3*x)+...
(8/(pi*(5^3)))*sin(5*x)+...
(8/(pi*(7^3)))*sin(7*x)+...
(8/(pi*(9^3)))*sin(9*x)+...
(8/(pi*(11^3)))*sin(11*x)+...
(8/(pi*(13^3)))*sin(13*x)+...
(8/(pi*(15^3)))*sin(15*x);
ss = 0;
for k =1:16
ss = ss + (8/(pi*(k^3)))*sin(k*x);
end
plot(x,s,'b-',x,ss,'r-');
Why I choose odd values in manual sum because sin(k*pi) = 0, where k=2,4,... ,but i took all (even and odd) values in for loop
The plot generated is given below:
123.bmp

채택된 답변

Walter Roberson
Walter Roberson 2020년 1월 4일
sin(k*pi) is 0 for integer k but you have sin(k*x) where x is a fraction of pi and that does not vanish.
  댓글 수: 3
Steven Lord
Steven Lord 2020년 1월 4일
In theory sin(k*pi) is 0 for integer k. In practice if you're computing sin(k*pi) consider using the sinpi function introduced in release R2018b.
sin(pi) % not exactly 0
sinpi(1) % exactly 0
Walter Roberson
Walter Roberson 2020년 1월 4일
Using k=1:2:16 should duplicate your manual calculation.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by