# Chapter 2

## Approximation with Orthogonal Functions using a Macro

### Find a Close Approximation to a Semicircle

An important feature of using a set of functions that are orthogonal to each other in approximating another function is that their contributions to the approximation are independent and do not interact.  They can be adjusted independently to minimize the error between the approximation and its objective.  Sine and Cosine functions with integer, n, arguments such as sin(nx) and cos(nx) form an orthogonal set and are often used in approximation because of this property.

The following four graphs illustrate the use of k*sin(x) + j*sin(3x) to approximate a semicircle on the interval x = 0 to x  = π.  At top left, red, is shown a half cycle of sin(x). At top right 1-1/2 cycles of sin(3x).  At bottom left these two are combined as sin(x) + 0.5*sin(3x).  At bottom right, the coefficient of sin(x) has been increased by about 80% and that of sin(3x) has been decreased by about 20% resulting in a much better fit to the semicircle.  By adding more odd sinusoids the approximation can be improved, theoretically, to any extent.

Even argument sine functions are not of use because they have odd symmetry on this interval. ### Use 17, odd argument, sinusoids, sin(x) through sin(33x)

The cosines, cos(nx), are not of use in this case because they would provide, independently, odd symmetry on this interval where the objective is even symmetry.

Relatively smooth objectives such as the semicircle tend to require more amplitude for lower frequencies than higher in their approximation.  For this reason a diminishing sequence of initial coefficients was  selected as 1, 1/2, 1/3,   , , , ,  1/17 for the approximating sine series. Reasonable starting values tend to reduce the number of calculation steps required to determine optimal coefficient values.

Objectives that contain sharp edges or spikes tend to employ more uniform coefficient values.

The semicircle was approximated in steps of π/80 over the interval x = 0 to x  = π. Values of the semicircle and its approximation, and the squared error between the two are shown in the table that follows. Coefficients were optimized one after the other with no noticeable interaction.

A macro,
a software program that runs in the Excel spread sheet and behaves much like a user designed function, to be described in detail, was employed.

Also shown is the sum of the squared errors, the chosen objective function.  This objective function is quite commonly used.   One reason for the prevalence of this objective function is that substantial difficulties are encountered if one tries to employ most other objective functions when analytic, as opposed to numerical, approximations are attempted.  Another reason is its optimality in applications to fitting randomly perturbed data. The fully optimized approximation is shown in graph form next. Some coefficients required more adjustment steps than others, some needed 81 steps: the fewest number was 54.

A macro was used in this topic to automate the adjustment of the coefficients so as to minimize the error.  This is a task that can be accomplished by a great many sequential  by-hand adjustments.

Excel2000 provides an Add-In, Solver, which could have been used to make the adjustments.  In our case we use a macro to illustrate how the automation could be accomplished in the absence of Solver.

Moreover, there can be situations in which a more specific adjustment tool is needed and the only way to proceed is to design one's own tool, a macro.

The macro that was used to adjust the coefficients to minimize the error is now shown.  Most spreadsheets will provide macro capability.  However, the code used to write the macro may differ from Visual Basic, the programming language that was used for this macro.  Some knowledge of VB could be required should one want to implement this macro in another programming language. The extensive commenting should be a great help. The use of GoTo in programming is generally frowned on as leading to poor clarity of the code.  In this case its use may add to clarity, particularly from the point of view of a novice programmer.

Try to create an improved version of this macro that is easier to use, and, possibly quicker to converge.

Approximation with sinusoids is widespread in Physics and Engineering, particularly in association with studies in thermal conduction, mechanical design, acoustics and communications.

### Next

The use of Sensitivities in the Method of Steepest Descent as applied to Approximation is explored.

 Top Previous Topic Next Topic Topics