How can i store values into an array while making summation
Afficher commentaires plus anciens
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 commentaires
Guillaume
le 30 Oct 2018
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
le 30 Oct 2018
madhan ravi
le 30 Oct 2018
Modifié(e) : madhan ravi
le 30 Oct 2018
explain step by step what you are trying to achieve ? if its summation you don't even have to use loop
Zuy
le 30 Oct 2018
madhan ravi
le 30 Oct 2018
N=10:2:100;
n=(N./2)+1:1:10e5;
S=cumsum((1./n).^2);
plot(N,S(1:numel(N)))
Zuy
le 30 Oct 2018
madhan ravi
le 30 Oct 2018
Modifié(e) : madhan ravi
le 30 Oct 2018
As always! Anytime :)
Guillaume
le 30 Oct 2018
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
le 30 Oct 2018
Ah thank you Guillame how to overcome this issue ? Any insights would be helpful
Zuy
le 30 Oct 2018
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
le 30 Oct 2018
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
le 30 Oct 2018
Guillaume
le 31 Oct 2018
Possibly that would work. As I don't have the symbolic toolbox, I can't help with that.
Zuy
le 31 Oct 2018
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Image Arithmetic dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!