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 Commenti

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.
It should converge actually 1000 is enough for infinity in my case but i couldnt obtain the true graph
madhan ravi
madhan ravi il 30 Ott 2018
Modificato: madhan ravi il 30 Ott 2018
explain step by step what you are trying to achieve ? if its summation you don't even have to use loop
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!
N=10:2:100;
n=(N./2)+1:1:10e5;
S=cumsum((1./n).^2);
plot(N,S(1:numel(N)))
madhan ravi
madhan ravi il 30 Ott 2018
Modificato: madhan ravi il 30 Ott 2018
As always! Anytime :)
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.
Ah thank you Guillame how to overcome this issue ? Any insights would be helpful
Yes unfortunatelly it works in a wrong way :(
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.
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 il 30 Ott 2018
Modificato: Zuy il 30 Ott 2018
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.
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))
Possibly that would work. As I don't have the symbolic toolbox, I can't help with that.
Thank you so much!!

Accedi per commentare.

 Risposta accettata

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

Più risposte (0)

Richiesto:

Zuy
il 30 Ott 2018

Commentato:

Zuy
il 31 Ott 2018

Community Treasure Hunt

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

Start Hunting!

Translated by