How can i store values into an array while making summation
Mostra commenti meno recenti
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
Guillaume
il 30 Ott 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
il 30 Ott 2018
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
Zuy
il 30 Ott 2018
madhan ravi
il 30 Ott 2018
N=10:2:100;
n=(N./2)+1:1:10e5;
S=cumsum((1./n).^2);
plot(N,S(1:numel(N)))
Zuy
il 30 Ott 2018
madhan ravi
il 30 Ott 2018
Modificato: madhan ravi
il 30 Ott 2018
As always! Anytime :)
Guillaume
il 30 Ott 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
il 30 Ott 2018
Ah thank you Guillame how to overcome this issue ? Any insights would be helpful
Zuy
il 30 Ott 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
il 30 Ott 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
il 30 Ott 2018
Guillaume
il 31 Ott 2018
Possibly that would work. As I don't have the symbolic toolbox, I can't help with that.
Zuy
il 31 Ott 2018
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Image Arithmetic in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!