Patrick Jane
Patrick Jane

Reputation: 53

how do I compute this infinite sum in matlab?

I want to compute the following infinite sum in Matlab, for a given x and tau:

infsum

I tried the following code, given x=0.5 and tau=1:

symsum((8/pi/pi)*sin(n*pi*0.5)*sin(n*pi*0.5)*exp(-n*n*pi*pi)/n/n,1,inf)

But I get this:

(228155022448185*sum((exp(-pi^2*n^2)*((exp(-(pi*n*i)/2)*i)/2 - (exp((pi*n*i)/2)*i)/2)^2)/n^2, n == 1..Inf))/281474976710656

I want an explicit value, assuming the sum converges. What am I doing wrong? It seems like Matlab doesn't compute exp() when returning symsum results. How do I tell Matlab to compute evaluate the exponentials?

Upvotes: 1

Views: 10455

Answers (3)

febrian
febrian

Reputation: 1

You should first define your variable "n" using syms. Then, you can include this variable in your symsum code.

Here's what I did:

syms n; AA = symsum((8/pi/pi)*sin(n*pi*0.5)*sin(n*pi*0.5)*exp(-n*n*pi*pi)/n/n,n,1,inf); BB = double(AA)
BB = 4.1925e-05

Upvotes: 0

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

Just to show you a different way, one that does not require the symbolic toolbox,

summ  = 0;
summP = inf;
n = 1;
while abs(summP-summ) > 1e-16
    summP = summ;    
    summ = summ + sin(n*pi*0.5)*sin(n*pi*0.5)*exp(-n*n*pi*pi)/n/n;
    n = n + 1;
end

8/pi/pi * summ

which converges after just 1 iteration (pretty obvious, since exp(-4*6.28..)/n/n is so tiny, and sin(..) is always somewhere in [-1 1]). So given tau==1 and x==0.5, the infinite sum is essentially the value for n==1.

Upvotes: 2

Oleg
Oleg

Reputation: 10676

Convert to double

double(symsum(...))

Upvotes: 3

Related Questions