Feeds:
Posts

Posts Tagged ‘Series’

Two days ago I was trying to expand in series a lot of functions… so I ask myself, Could it be done in SAGE? It should be possible… but, How? ðŸ˜›

Solution by Andrzej Chrzeszczyk

sage: var('r');
sage: f=2*r/sinh(2*r)
sage: f.taylor(r,0,5)
14/45*r^4 - 2/3*r^2 + 1
sage: maxima(f).powerseries('r',0)
-4*r*'sum((2^(2*i2-1)-1)*2^(2*i2-1)*bern(2*i2)*r^(2*i2-1)/
(2*i2)!,i2,0,inf)

This solution uses a power series expansion from maxima… really nice feature! Isn’t it?
Ah… and this expansion is around $r=0$.

If one would like the asymptotic expansion $r\to\infty$,

sage: maxima(f).powerseries('r',infinity)
-4*r*'sum((2^(2*i3-1)-1)*2^(2*i3-1)*bern(2*i3)*r^(2*i3-1)/
(2*i3)!,i3,0,inf)

However, note that this expansion coincides with the previous one, i.e., it’s the function itself. It couldn’t be that perfect. ðŸ˜‰

Solution by Francois Maltey

Use the Taylor command of SAGE,

• Around zero
sage: taylor (2*x/sinh(2*x), x, 0, 10)
-292/13365*x^10 + 254/4725*x^8 - 124/945*x^6 + 14/45*x^4 - 2/3*x^2 + 1
• Around infinity… a trick! change $x\mapsto 1/x$ and expand around zero ðŸ™‚
sage: taylor (2*1/x/((exp(2/x)-exp(-2/x))/2), x, 0, 12)
4*e^(-10/x)/x + 4*e^(-6/x)/x + 4*e^(-2/x)/x
• Thank you guys!

Enjoy!

Dox

SAGE tip: Fourier Series Approximation

Inspired by a post in sage-devel (or support) group of SAGE, I came along with this few lines which allows me to plot a Fourier Series Approximation of the line, to a given order,

sage: reset() sage: var('x,i,n') (x,i,n) sage: def b(n): ... return 2.*(-1)^(n+1)/n ... sage: def f(x,n): ... return sum(b(i)*sin(i*x),i,1,n) ... sage: p = Graphics() sage: for n in [1,2,3,4,5,6]: ... p = plot(f(x,n), (x,-pi,pi), color=hue((n+1)/7.0)) + plot(x, -pi, pi, color='black') + text("Fourier Approximation of order %d" %n, (-1,3), fontsize=14, color='blue') ... p ...

and get as result, the series of plots that follows,

1st Fourier Approximation to a Line

2nd Fourier Approximation to a Line

3rd Fourier Approximation to a Line

4th Fourier Approximation to a Line

5th Fourier Approximation to a Line

6th Fourier Approximation to a Line

However, I was not happy ’cause I introduce the Fourier coefficients… So, I did a second try.

sage: reset() sage: var('x,n,i') (x,n,i) sage: f(x) = x^2

 sage: def a(n): ... coeff = integral(f(x)*cos(n*x), (x,-pi,pi))/pi ... return coeff ... sage: def b(n): ... coeff = integral(f(x)*sin(n*x), (x,-pi,pi))/pi ... return coeff ... sage: def FS(n): ... return a(0)/2 + sum(a(i)*cos(i*x)+b(i)*sin(i*x), i, 1, n) ... 

Where I’ve defined the Fourier coefficients and the Fourier Series of a given function $f(x)$, introduced by the user.

With the line

sage: for n in [1,2,3,4,5,6]: ... p = plot(FS(n), (x,-1.1*pi,1.1*pi), color=hue((n+1)/7.0)) + plot(f(x), (x, -1.1*pi, 1.1*pi), color='black') + text("Fourier Approximation of order %d" %n, (0,-2), fontsize=14, color='blue') ... p ...

One get the Fourier Series Approximation for $f(x)=x^2$,

1st Approximation for the parabola

2nd Approximation for the parabola

3rd Approximation for the parabola

4th Approximation for the parabola

5th Approximation for the parabola

6th Approximation for the parabola

NOTE…

• The use of the last program is restricted to the interval $[-\pi,\pi]$.
• In order to find the Fourier coefficients the integrals might be doable… so no every function $f(x)$ can be shown as a Fourier series approximation.
• I tried to define the above using numerical_integrategral command, but didn’t work. Does anyone knows why?
• I also tried to use range command instead of a list for the loop… Didn’t work!!! Any clues?
• Ok, that was it. Enjoy!!!

DOX.