MATLAB Answers

0

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

Asked by HADIMARGO on 13 Dec 2018
Latest activity Edited by HADIMARGO on 13 Dec 2018
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 Comments

Show 1 older comment
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

Sign in to comment.

Tags

2 Answers

Answer by HADIMARGO on 13 Dec 2018
Edited by HADIMARGO on 13 Dec 2018
 Accepted Answer

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)

  0 Comments

Sign in to comment.


Answer by 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)

  1 Comment

tnx for answering but the answer should be:
3.jpg

Sign in to comment.