how to plot a summation function in matlab using a for loop

how can i plot this summation function using a for loop?
i start write this:
t = -pi : pi/6 : pi;
s=0;
for i = 0:1
s=s+(2/3)*(pi^2) + ((-1)^i)* (-4*cos(i*t))/i^2

 채택된 답변

Rik
Rik 2023년 5월 28일

1 개 추천

It's a good start.
You should only put the summation part in the loop, and you should store the results in a vector. (remember that the first element of a vector is index 1)
I would also suggest that you use n instead of i.
If you need help implementing this, feel free to post a comment.

댓글 수: 4

Dimitra
Dimitra 2023년 5월 28일
편집: Dimitra 2023년 5월 28일
Thank you very much!!
Yes, can you please help me, because it seems difficult for me
e.g. I would like to plot it for n=2
I run the my code , and the figure is empty
The code you sent me by email is the exact same you posted in your original question. Why didn't you try to implement my advice?
Also, the code you posted will not result in a figure at all, so you didn't show all the code you're using. Neither of these two things are an effective way to solve the problem.
Please post your full code and format it properly. That way you can run it immediately in the editor.
I understand it may be difficult to ignore the spammer, but feel free to follow up with a comment.

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

추가 답변 (1개)

Torsten
Torsten 2023년 5월 28일
편집: Torsten 2023년 5월 28일
For reference:
syms k t
f(t) = 4*symsum((-1)^(k+1)*cos(k*t)/k^2,k,1,Inf)
f(t) = 
f(0)
ans = 
fplot(f+2/3*pi^2,[-2*pi,2*pi])

카테고리

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

제품

릴리스

R2023a

질문:

2023년 5월 28일

편집:

2023년 5월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by