How can i store values into an array while making summation

Hi, im trying to sum this series
I want to make S(in the code) an array to keep all of values from n=N/2 to infinity. Than i will sum them up and obtain the sum of my series. My code also should do the calculation for different N values.At the and i will make a plot of Sum of my series vs N. It is shown in the image also.Thanks!
clc;
clear;
clear all;
format long
N=10:2:100;
for i=1:length(N)
for n=(N(i)/2)+1:1:10e5
S(i)=(1/n)^2;
Stot=sum(S);
end
R(i)=2*pi*Stot;
end
loglog(N,R)

16 件のコメント

Guillaume
Guillaume 2018 年 10 月 30 日
keep all of values from [...] to infinity.
Unless you have infinite memory, that's going to be difficult!
The sample code you show seems to assume that infinity is 1e6 which is a very small number in the ream of floating point double (whose maximum value is about 1e308) and certainly not infinity.
I do not understand your summation notation and how it relates to the code you've written.
Zuy
Zuy 2018 年 10 月 30 日
It should converge actually 1000 is enough for infinity in my case but i couldnt obtain the true graph
madhan ravi
madhan ravi 2018 年 10 月 30 日
編集済み: madhan ravi 2018 年 10 月 30 日
explain step by step what you are trying to achieve ? if its summation you don't even have to use loop
Zuy
Zuy 2018 年 10 月 30 日
I am trying to sum this series (1/n^2) going from k=(N/2)+1 to k=inf. But at the same time i want to change N because i want to make a plot that shows N vs summation. This plot will show how my summation change according to N.(starting value of summation). Thanks for your help!
madhan ravi
madhan ravi 2018 年 10 月 30 日
N=10:2:100;
n=(N./2)+1:1:10e5;
S=cumsum((1./n).^2);
plot(N,S(1:numel(N)))
Zuy
Zuy 2018 年 10 月 30 日
Thanks!
madhan ravi
madhan ravi 2018 年 10 月 30 日
編集済み: madhan ravi 2018 年 10 月 30 日
As always! Anytime :)
Guillaume
Guillaume 2018 年 10 月 30 日
Beware!
N=10:2:100;
n=(N./2)+1:1:10e5;
Is exactly the same as
N = 10;
n = N/2+1:10e5;
There is no point in passing a vector to the : (colon) operator as it only uses the first element.
Notice that the step 2 of N has been lost. Fortunately, the plot still works because the original step of 2 of N would result in a step of 1 for n. But with any other step for the original N vector, the above would not work.
madhan ravi
madhan ravi 2018 年 10 月 30 日
Ah thank you Guillame how to overcome this issue ? Any insights would be helpful
Zuy
Zuy 2018 年 10 月 30 日
Yes unfortunatelly it works in a wrong way :(
Guillaume
Guillaume 2018 年 10 月 30 日
編集済み: Guillaume 2018 年 10 月 30 日
While you probably could work out a vectorised version that calculate the cumsum of n with a half step and selecting the correct sum depending on the parity of the elements in the N vector, I think you would be better off with an explicit loop over N:
N = 10:5:100
Nsum = zeros(size(N));
for Nidx = 1:numel(N)
n = N(Nidx)/2+1 : 1e6;
Nsum(Nidx) = sum(1./n.^2);
end
plot(N, Nsum)
I still have no idea how that relates in any way to the summation image in the question.
madhan ravi
madhan ravi 2018 年 10 月 30 日
wow your intelligent , I thought we could get rid of that loop by any chance , neither I could interpret the image without the question
Zuy
Zuy 2018 年 10 月 30 日
編集済み: Zuy 2018 年 10 月 30 日
Thank you so much!. fk in the summation image is 1/k. Bottom indice of the summation is N/2+1 and top indices is going inf. Also N is changes.
Zuy
Zuy 2018 年 10 月 30 日
What if i use symsum for the infinite summation? Like that
clear;
clc;
N = 0:2:70;
Nsum = zeros(size(N));
for Nidx = 1:numel(N)
syms n
Nsum(Nidx) = symsum(1./4.^n,n,(N(Nidx)/2)+1,inf);
end
plot(N,log10( Nsum))
Guillaume
Guillaume 2018 年 10 月 31 日
Possibly that would work. As I don't have the symbolic toolbox, I can't help with that.
Zuy
Zuy 2018 年 10 月 31 日
Thank you so much!!

サインインしてコメントする。

 採用された回答

madhan ravi
madhan ravi 2018 年 10 月 30 日

0 投票

Stot(i)=sum(S(i)); %use iterator as an index to avoid overwriting
also see preallocation for making code efficient

2 件のコメント

Zuy
Zuy 2018 年 10 月 30 日
Thanks!
madhan ravi
madhan ravi 2018 年 10 月 30 日
Anytime :)

サインインしてコメントする。

その他の回答 (0 件)

タグ

質問済み:

Zuy
2018 年 10 月 30 日

コメント済み:

Zuy
2018 年 10 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by