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 view (last 30 days)

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?

madhan ravi
on 13 Dec 2018

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)

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/435495-how-should-draw-this-in-loop-form-where-is-the-problem-in-my-code#comment_649977

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/435495-how-should-draw-this-in-loop-form-where-is-the-problem-in-my-code#comment_649977

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/435495-how-should-draw-this-in-loop-form-where-is-the-problem-in-my-code#comment_649991

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/435495-how-should-draw-this-in-loop-form-where-is-the-problem-in-my-code#comment_649991

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/435495-how-should-draw-this-in-loop-form-where-is-the-problem-in-my-code#comment_649993

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/435495-how-should-draw-this-in-loop-form-where-is-the-problem-in-my-code#comment_649993

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/435495-how-should-draw-this-in-loop-form-where-is-the-problem-in-my-code#comment_650018

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/435495-how-should-draw-this-in-loop-form-where-is-the-problem-in-my-code#comment_650018

Sign in to comment.