(* 
      Mikroharjoitus 2    C3 - I - 
        07/Kari Eloranta  *) 

 (* 
      Testattu toimivaksi Mathematican versiolla 5.2  *)

(*  1  *) 

 (*  Z - 
        muunnoksia *)

Sum[ 
      z^(-n) 2^n/Factorial[n  +  2],  {n, 0, Infinity}]

Sum[ z^(-n),  {n, 0,  Infinity, 3} ]

k = 3;
Sum[ z^(-n) n^k,  {n, 0, Infinity}]

Sum[ z^(-n) Sin[ n ],  {n, 0, Infinity}]

Sum[ z^(-n) Sin[ n Pi/4],  {n, 0, Infinity}]

Simplify[%]

Sum[ z^(-n) Sin[ n Pi/2],  {n, 0, Infinity}]

Sum[ z^(-n) Cos[ n  Pi/2],  {n, 0, Infinity}]

(*  2  *) 

 (* 
      Kimurantteja kertoimia *)

k = 1;
2^(k  -  1)/Pi  NIntegrate[ 
        Log[1  +   (1/2) Exp[-I theta]  ] Exp[ 
            I k theta],  {theta,  0,  2 Pi}]

x[k_] := 2^(k  -  1)/Pi  NIntegrate[ 
          Log[1  +   (1/2) Exp[-I theta]  ] Exp[ 
              I k theta],  {theta,  0,  2 Pi}]

Table[Re[ x[k] ], {k, 1, 10}]

Table[Im[ x[k] ], {k, 1, 10}]

(*  3  *) 

 (*  Fibonaccin comeback aka il retorno di Leonardo di Pisa  *)

n =  30;
(5  +  Sqrt[5])/10 ((1  +  Sqrt[5])/2)^
          n  +  (5  -  Sqrt[5])/10 ((1  -  Sqrt[5])/2)^
          n

Simplify[%]

n =  30;
(5  -  Sqrt[5])/10 ((1  -  Sqrt[5])/2)^
        n

N[%]

(*  4  *) 

 (*  Gibbsin ilmio *)

Osasumma1[x_, n_] := 
      4/Pi Sum[ 
          Sin[(2  m  +  1) x]/(2 m  +  1), {m, 0, 
            n}]

Plot[Osasumma1[x, 15], {x, - Pi, Pi}];

Osasumma2[x_,n_] := Pi + 2 Sum[ (-1)^(k - 1)/k Sin[k x], {k, 1, n} ]

Plot[Osasumma2[x, 15], {x, - Pi, Pi}];

Plot[ Pi + x - Osasumma2[x, 50], {x, -Pi, Pi}];

NIntegrate[ (Pi + x - Osasumma2[x, 50] )^2,  {x, -Pi, 
        Pi}]