HOW SHOULD DRAW THIS IN LOOP FORM? WHERE IS THE PROBLEM IN MY CODE?

my code:
s=0;
for n=1:0.01:10
s=s+(-1^n+1*sin(n*pi*x));
end
f=(2/pi)*s;
plot(x,f)
Where is wrong in my code?

댓글 수: 4

x is not defined by your code, so it will have whatever value is sitting around in memory.
HADIMARGO
HADIMARGO 2018년 12월 13일
편집: HADIMARGO 2018년 12월 13일
i defined x but the result is not correct yet. and the amount of f are so big, why?
my code:
x=-5:1:5;
s=0;
for n=1:0.01:10
s=s+(-1^n+1*sin(n*pi*x));
end
f=(2/pi)*s;
plot(x,f)
You are defining x to have only 11 points, to exist only at the integers. Your required plot is clearly defined much denser than that.
i could do this but it is not still correct completely
4.jpg

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

 채택된 답변

HADIMARGO
HADIMARGO 2018년 12월 13일
편집: HADIMARGO 2018년 12월 13일

0 개 추천

finally i could write the correct answer! hahaha
tnx guys for pushing me lonely!
x=linspace(-5,5,1000);
s=0;
for n=1:1:10000
p=((-1)^(n+1)*sin(n*pi*x))/n;
s=s+p;
end
f=(2/pi)*s;
plot(x,f)

추가 답변 (1개)

madhan ravi
madhan ravi 2018년 12월 13일
x=linspace(-5,5,10000);
s=0;
for n=1:0.01:10
s=s+(-1^n+1*sin(n*pi*x));
end
f=(2/pi)*s;
plot(x,f)

카테고리

도움말 센터File Exchange에서 Functions에 대해 자세히 알아보기

태그

질문:

2018년 12월 13일

편집:

2018년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by