Examples:
 

 

Example - Fourier

* The "\" symbol has been used at the end of lines to indicate that the line was too long for the display and has been
wrapped to the line below in order to fit within the width of the web page. These lines would need to be joined in the normal
program editor.


I. Introduction

This program demonstrates the use of Fourier series to approximate periodic functions, making use of the Integrate() method in Superbase.



II. Mathematics Involved

We can expand any ‘well-behaved' (see an analysis textbook for exceptions) real periodic function f(x) with period 2 (i.e. for all x ) as:

,

where and are as follows:

.

The series is often rapidly convergent, and an approximation can therefore be calculated by taking the first N terms.



III. How the Program Works

When the form has been set up, the procedure formplot plots the current formula (initially a square wave) on the graph using techniques from the Graph program, and then the procedure draw() calculates the approximation.

First the Fourier coefficients are calculated within the calcab() procedure:

SUB calcab(formula$,a%(),b%(),n%%)
   'Calculate a and b coefficients for the Fourier approximation
   DIM x%,i%%
   a%(0) = Superbase.Integrate(formula$,"x%", - 1,1,0.00001,10)
   FOR i%% = 1 TO n%%
     a%(i%%) = Superbase.Integrate("(" + formula$ + ")" + "*cos(i%%*pi*x%)"\
     ,"x%", - 1,1,0.00001,10)
     b%(i%%) = Superbase.Integrate("(" + formula$ + ")" + "*sin(i%%*pi*x%)"\
     ,"x%", - 1,1,0.00001,10)
   NEXT i%%
 END SUB 

The Integrate() method provides a quick and easy way to do this. Note that for this program the Intervals argument (the last one) has been set to 4 times the value of n%% rather than the default of 1, because with trigonometric integrals there is a danger that the default interval size may coincide with the period of the trigonometric function.

The approximation is then plotted:

   FOR x% = - 2 TO 2 STEP 0.005
     y% = a%(0) / 2 + Superbase.Summation("a%(i%%)*cos(i%%*pi*x%)+b%(i%%)*\
     sin(i%%*pi*x%)","i%%",1,n%%)
     IF x% > - 2 THEN 
       SET fc = f("la" + STR$ (x% - 0.005,"9.000"))
       'Only plot if within y-range of graph
       IF ABS (oldy%) <= 1.25 AND ABS (y%) <= 1.25 THEN 
         fc.Move(250 + oldx% * 100,175 - oldy% * 100,250 + x% * 100,175 - \
         y% * 100)
         fc.Visible = - 1
       ELSE 
         fc.Visible = 0
       END IF 
     END IF 
     oldx% = x%:oldy% = y%
   NEXT x%

The Summation() method is used to sum the terms of the approximation.

Clicking the “>” or “<” buttons will result in the approximation having one term more or one term less.

The function to be plotted can be changed by means of a combo box. This enables the selection of some pre-defined functions, or the entry of any formula (in terms of x ). When the combo box loses focus (i.e. the Enter button is pressed), the FnChange() procedure is called, which in turn calls the formcheck() procedure. This replaces the names of the pre-defined functions with their actual formulae, and checks the syntax.

Below is an example of plotting seven terms of a sawtooth wave series; the Fourier approximation is shown in red.

If a formula is entered, only the values between –1 and +1 is relevant. For example, if x3 is plotted:

the Fourier approximation takes just these values and extrapolates it forwards and backwards, resulting in discontinuities.