Feeds:
Posts

## SAGE tip: Fourier Series Approximation with Interactive mode

After my last post, about Fouries Series, I decided to give a try to the interactive mode of SAGE.

🙂

What did I do?

First I check out the examples worked at the Wiki page. In the section of graphics I got inspired from the post Interactive 2D Plotting by Timothy Clemans and Newton’s Method by William Stein post in the Calculus section. So… there it is!

The code is too long to be posted here… but here it goes anyway

`var('x,n,i')`

```def error_msg(msg): ... print '<p style="font-family: Arial, sans-serif; color: #000;"><span     style="color: red; font-weight: bold;">Error</span>: %s</p>' % msg```

`...`

```def a(f, n, init, final): ... x = f.variables()[0] ... L = (final - init)/2 ... coeff = integral(f(x)*cos(n*pi*x/L), (x,init,final))/L ... return coeff```

``` ... def b(f, n, init, final): ... x = f.variables()[0] ... L = (final - init)/2 ... coeff = integral(f(x)*sin(n*pi*x/L), (x,init,final))/L ... return coeff ... def FS(f, n, init, final): ... x = f.variables()[0] ... L = (final - init)/2 ... return a(f, 0, init, final)/2 + sum(a(f, i, init, final)*cos(i*x*pi/L)+b(f, i, init, final)*sin(i*x*pi/L), i, 1, n) ... ```

```@interact def interactive_2d_plotter(clr=Color('green'), expression=input_box('x^2', 'Expression', str), x_range=range_slider(-6,6,1,(-4,4), label='X Range'), order=slider([0..100],None,None,3), square=checkbox(True, 'Square'), axes=checkbox(False, 'Show Axes')): ... if expression: ...   try: ...      expression = SR(expression) # turn string into a Sage expression ...   except TypeError: ...      print error_msg('This is not an expression.') ...      return ...   try: ...      xmin, xmax = x_range ...      n = order ...      if square or not axes: ...         print "var('%s')\nplot(%s).show(%s%s%s)" % (expression.variables()[0], repr(expression), 'aspect_ratio=1' if square else '', ', ' if square and not axes else '', 'axes=False' if not axes else '') ...         if square: ...            P = plot(FS(expression, n, xmin, xmax), xmin, xmax, color=clr) + plot(expression, xmin, xmax) ...            P.show(aspect_ratio=1, axes=axes) ...         else: ...            P = plot(FS(expression, n, xmin, xmax), xmin, xmax, color=clr) + plot(expression, xmin, xmax) ...            P.show(axes=axes) ...      else: ...         print "var('%s')\nplot(%s)" % (expression.variables()[0], repr(expression)) ...         P = plot(FS(expression, n, xmin, xmax), xmin, xmax, color=clr) + plot(expression, xmin, xmax) ...         P.show(axes=axes) ...   except ValueError: ...      print error_msg('This expression has more than one variable.') ...      return ...   except TypeError: ...      print error_msg("This expression contains an unknown function.") ...      return```

because it’s almost in-understandable 😛

Some pictures!!!

The code is set to give the Fourier approximation of the $f(x)=x^2$ with aspect_ratio = (1,1).

Initial Plot. Approx. of x^2

One can set a non one-to-one aspect_ratio, by un-checking the box,

Initial Plot with-no-square

And we can view the frame as well by checking the other box,

Initial plot no-square and frame

The first box allows us to change the colour, by using a palette,

Initial plot with color-palette

for example, set the colour to red,

Initial plot in red

We can also change the function, to an exponential, $f(x)=e^x$,

Fourier approximation of the exponential

and change the domain of the function,

Changing the interval

or the order of the approximation,

Exponential changing the order of the approximation

I published my sage file on my page (at the end of the page in the attached files)… for the sake of clarity (and completeness).

Enjoy,

Dox