Simpsons Rule
 Volume Number: 9 Issue Number: 10 Column Tag: Pascal workshop

# Simpson’s Rule

## An ingenious method for approximating integrals

By Marek Hajek, Incline Village, Nevada

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

Marek Hajek has been programming the Macintosh since 1989. He programmed two and a half years for Sierra Software Innovations where he wrote several in-house MacApp applications, participated in the development of SuperTEView, and the relational database engine - Inside Out II. Currently, he is receiving his bachelor's degree in Computer Science at the University of Nevada, Reno. He supports his college education by making useful programming tools - sorting/searching algorithms, and custom development. He welcomes your comments on this article either by phone at (702) 673-3341 or write to P.O. Box 7542, Incline Village, NV 89450.

Simpson's Rule, named after the great English mathematician Thomas Simpson, is an ingenious method for approximating integrals. If you don't know what integrals are used for, don't feel bad. Many college students who complete three semesters of calculus may be able to “compute” an integral, but won't know its practical application either. Computation of integrals is difficult to learn and easy to forget. [Many years out of school, I can attest to this! - Ed.]

Integrals are essential to the modern world. Practical applications of the integral are found in business, hydrostatics, highway construction, travel to the moon, solving of differential equations, and other branches of science. The first computer ever built was constructed to speed up ballistic missile trajectory calculations which meant solving a lot of integrals. [Given the forces imposed on a missile (gravity, thrust, wind resistance, etc.), integration is necessary to determine its path. - Tech. Ed.]

To illustrate the use of integrals, look at the curve in Figure 1.1a. The curve is described by the equation (1+X4). I want to compute the area of the shaded region. Notice, the area is between the curve, the x-axis, and the x-coordinates [-1,1]. The integral that will compute the area of the shaded region is in figure 1.1b. Anybody familiar with integrals will tell you that there is no known way to solve the integral abstractly (the quick and easy way).

Figure 1.1a

Figure 1.1b

If an integral can be solved on an abstract level, the computation is relatively easy. In practical applications, however, an integral can seldom be solved abstractly. That's where the Simpson's Rule finds its use. Thomas Simpson invented an equation, today called Simpson's Rule, which can be used to approximate an integral.

## APPROXIMATION

The following line shows this equation in abstract form.

Looks complicated? First, take a look at figure 1.2.

Figure 1.2

In the approximation equation, the variables a and b are the boundaries of the integral and correspond to -1 and 1 in figure 1.1a. The variable n is the number of times the region under the curve is partitioned into smaller regions. You only have to know two things about n. First, the larger n is, the more accurate the approximation. And second, n must be a positive even integer (+2, 4, 6, ...). The function f(x) is the function you are integrating. In my example, it is (1+X4). Whenever you encounter f(x) in the equation, pass it the appropriate parameter. The parameters are the x-coordinates of the partitions (X0, X1, X2, X3, , Xn).

[Simpson’s rule approximates the function on each subinterval of the partition by a parabola that passes through the endpoints and the midpoint. The area under a parabola is easily calculated. Adding up these areas gives an estimate of the integral. - Tech. Ed.]

## EXAMPLE COMPUTATION

To compute the integral in figure 1.1b, given four partitions (n=4), the approximation looks like this:

Simplified:

Simplified:

Result: 2.1791. . .

## SIMPSON'S RULE - PASCAL

Figure 1.3

I translated Simpson's Rule into several pascal functions. To help you see what each function does, the equation is divided into three parts - Head, Twos/Fours, and First/Last (Figure 1.3). Have fun!

```CODE LISTING

{--------------------Main Program----------------------------}
PROGRAM Simpson;
(* Author  - Marek Hajek *)
(* P.O. Box 7542 *)
(* Incline Village, NV 89450 *)

(* This program was written with Think Pascal 4.0.1 *)
USES
(* Make sure you include the sane library *)
Auxiliary, Sane;

CONST
kLowerLimit = -1;               (* Corresponds to "a" *)
kUpperLimit = 1;                (* Corresponds to "b" *)
kPartitions = 4;                (* Corresponds to n = 4 *)

VAR
result: Extended;
(* The approximated result of the Integral *)

BEGIN
ShowText;  (* Brings up the Think Pascal text window *)

result := ComputeIntegral(kLowerLimit, kUpperLimit,
kPartitions, IntegrandFunction);
writeln('Integral with lower/upper limits ', kLowerLimit : 0,
'/', kUpperLimit : 0, ', subintervals ', kPartitions : 0,
' is: ', result);

readln; (* Stop here before the text window disappears *)
END.

{--------------------ComputeIntegral-------------------------}
FUNCTION ComputeIntegral (lowerLimit, upperLimit: Extended;
partitionCount: LongInt;
FUNCTION IntegrandFunction (partitionCoordinate:
Extended): Extended): Extended;
(* The function ComputeIntegral calls the necessary *)
(* functions to compute the individual parts.*)
(* It returns the approximate result. *)
VAR
result: Extended;
partitionIncrement: Extended;
partitionCoordinate: Extended;
index: LongInt;

BEGIN
result := FirstAndLast(lowerLimit, upperLimit,
IntegrandFunction);

partitionIncrement :=
(upperLimit - lowerLimit) /  partitionCount;
partitionCoordinate := lowerLimit;

(* The FOR  loop computes the second part of the *)
(* integral -> Twos/Fours *)
FOR  index := 1 TO partitionCount - 1  DO
BEGIN
(* Partition coordinate corresponds to X0, X1, X2,.....Xn *)
partitionCoordinate :=
partitionCoordinate +  partitionIncrement;

(* Odd index means compute 4* f(x), even index *)
(* means compute 2 * f(x)  *)
IF Odd(index) THEN
result := result +
4 * IntegrandFunction(partitionCoordinate)
ELSE
result := result +
2 * IntegrandFunction(partitionCoordinate)

END;  (* FOR ... *)

END;

{------------------IntegrandFunction-------------------------}
FUNCTION IntegrandFunction (partitionCoordinate:
Extended): Extended;
(* The Integrand function is the function inside the *)
(* integral and needs to be defined by you. In my example, *)
(* the integrand function is  (1+X4) and is translated *)
(* into pascal. The function takes one argument which is *)
(* the x coordinate of the partition. *)

BEGIN
{ This functions computes ->  (X * X * X * X +1)  }
IntegrandFunction :=
SQRT(XpwrI(partitionCoordinate, 4) + 1);
END;

partitionCount: LongInt): Extended;
(* Computes the first part of the integral equation, *)
(* the Head.  Corresponds to (b - a)/(3*n)  *)

BEGIN
(upperLimit - lowerLimit) / (3 * partitionCount);
END;

{----------------------FirstAndLast--------------------------}
FUNCTION FirstAndLast (lowerLimit, upperLimit: Extended;
FUNCTION IntegrandFunction (partitionCoordinate:
Extended): Extended): Extended;
(* Computes the third part of the integral, the *)
(* FIRST/LAST.  Corresponds to [f(X0) + f(Xn)  *)

BEGIN
FirstAndLast := IntegrandFunction(lowerLimit) +
IntegrandFunction(upperLimit);
END;
```

Community Search:
MacTech Search:

Microsoft Remote Desktop 8.0.16 - Connec...
With Microsoft Remote Desktop, you can connect to a remote PC and your work resources from almost anywhere. Experience the power of Windows with RemoteFX in a Remote Desktop client designed to help... Read more
Spotify 1.0.4.90. - Stream music, create...
Spotify is a streaming music service that gives you on-demand access to millions of songs. Whether you like driving rock, silky R&B, or grandiose classical music, Spotify's massive catalogue puts... Read more
djay Pro 1.1 - Transform your Mac into a...
djay Pro provides a complete toolkit for performing DJs. Its unique modern interface is built around a sophisticated integration with iTunes and Spotify, giving you instant access to millions of... Read more
Vivaldi 1.0.118.19 - Lightweight browser...
Vivaldi browser. In 1994, two programmers started working on a web browser. Our idea was to make a really fast browser, capable of running on limited hardware, keeping in mind that users are... Read more
Stacks 2.6.11 - New way to create pages...
Stacks is a new way to create pages in RapidWeaver. It's a plugin designed to combine drag-and-drop simplicity with the power of fluid layout. Features: Fluid Layout: Stacks lets you build pages... Read more
xScope 4.1.3 - Onscreen graphic measurem...
xScope is powerful set of tools that are ideal for measuring, inspecting, and testing on-screen graphics and layouts. Its tools float above your desktop windows and can be accessed via a toolbar,... Read more
Cyberduck 4.7 - FTP and SFTP browser. (F...
Cyberduck is a robust FTP/FTP-TLS/SFTP browser for the Mac whose lack of visual clutter and cleverly intuitive features make it easy to use. Support for external editors and system technologies such... Read more
Labels & Addresses 1.7 - Powerful la...
Labels & Addresses is a home and office tool for printing all sorts of labels, envelopes, inventory labels, and price tags. Merge-printing capability makes the program a great tool for holiday... Read more
teleport 1.2.1 - Use one mouse/keyboard...
teleport is a simple utility to let you use one single mouse and keyboard to control several of your Macs. Simply reach the edge of your screen, and your mouse teleports to your other Mac! The... Read more
Apple iMovie 10.0.8 - Edit personal vide...
With an all-new design, Apple iMovie lets you enjoy your videos like never before. Browse your clips more easily, instantly share your favorite moments, and create beautiful HD movies and Hollywood-... Read more

## Latest Forum Discussions

Use Batting Average and the Apple Watch...
Batting Average, by Pixolini, is designed to help you manage your statistics. Every time you go to bat, you can use your Apple Watch to track  your swings, strikes, and hits. [Read more] | Read more »
Celebrate Studio Pango's 3rd Annive...
It is time to party, Pangoland pals! Studio Pango is celebrating their 3rd birthday and their gift to you is a new update to Pangoland. [Read more] | Read more »
Become the World's Most Important D...
Must Deliver, by cherrypick games, is a top-down endless-runner witha healthy dose of the living dead. [Read more] | Read more »
SoundHound + LiveLyrics is Making its De...
SoundHound Inc. has announced that SoundHound + LiveLyrics, will be one of the first third-party apps to hit the Apple Watch. With  SoundHound you'll be able to tap on your watch and have the app recognize the music you are listening to, then have... | Read more »
Adobe Joins the Apple Watch Lineup With...
A whole tidal wave of apps are headed for the Apple Watch, and Adobe has joined in with 3 new ways to enhance your creativity and collaborate with others. The watch apps pair with iPad/iPhone apps to give you total control over your Adobe projects... | Read more »
Z Steel Soldiers, Sequel to Kavcom'...
Kavcom has released Z Steel Soldiers, which continues the story of the comedic RTS originally created by the Bitmap Brothers. [Read more] | Read more »
Seene Lets You Create 3D Images With You...
Seene, by Obvious Engineering, is a 3D capture app that's meant to allow you to create visually stunning 3D images with a tap of your finger, and then share them as a 3D photo, video or gif. [Read more] | Read more »
Lost Within - Tips, Tricks, and Strategi...
Have you just downloaded Lost Within and are you in need of a guiding hand? While it’s not the toughest of games out there you might still want some helpful tips to get you started. [Read more] | Read more »
The Petcube Camera is a device that lets you use live video to check in on your pet, talk to them, and play with them using a laser pointer - all while you're away. And the Petcube app is coming to the Apple Watch, so you'll be able to hang out with... | Read more »
Now You Can Manage Your Line2 Calls With...
You'll be able to get your Line2 cloud phone service on the Apple Watch very soon. The watch app can send and receive messages using hands-free voice dictation, or by selecting from a list of provided responses. [Read more] | Read more »

## Price Scanner via MacPrices.net

Intel Compute Stick: A New Mini-Computing For...
The Intel Compute Stick, a new pocket-sized computer based on a quad-core Intel Atom processor running Windows 8.1 with Bing, is available now through Intel Authorized Dealers across much of the... Read more
Heal to Launch First One-Touch House Call Doc...
Santa Monica, California based Heal, a pioneer in on-demand personal health care services — will offer the first one-touch, on-demand house call doctor app for the Apple Watch. Heal’s Watch app,... Read more
Mac Notebooks: Avoiding MagSafe Power Adapter...
Apple Support says proper usage, care, and maintenance of Your Mac notebook’s MagSafe power adapter can substantially increase the the adapter’s service life. Of course, MagSafe itself is an Apple... Read more
12″ Retina MacBook In Shootout With Air And P...
BareFeats’ rob-ART morgan has posted another comparison of the 12″ MacBook with other Mac laptops, noting that the general goodness of all Mac laptops can make which one to purchase a tough decision... Read more
FileMaker Go for iPad and iPhone: Over 1.5 Mi...
FileMaker has announced that its FileMaker Go for iPad and iPhone app has surpassed 1.5 million downloads from the iTunes App Store. The milestone confirms the continued popularity of the FileMaker... Read more
Sale! 13-inch 2.7GHz Retina MacBook Pro for \$...
Best Buy has the new 2015 13″ 2.7GHz/128GB Retina MacBook Pro on sale for \$1099 – \$200 off MSRP. Choose free shipping or free local store pickup (if available). Price for online orders only, in-... Read more
Minimalist MacBook Confirms Death of Steve Jo...
ReadWrite’s Adriana Lee has posted a eulogy for the “Digital Hub” concept Steve Jobs first proposed back in 2001, declaring the new 12-inch MacBook with its single, over-subscribed USB-C port to be... Read more
13-inch 2.7GHz Retina MacBook Pro for \$1234 w...
Adorama has the 13″ 2.7GHz/128GB Retina MacBook Pro in stock for \$1234.99 (\$65 off MSRP) including free shipping plus a free LG external DVD/CD optical drive. Adorama charges sales tax in NY & NJ... Read more
13-inch 2.5GHz MacBook Pro available for \$999...
Adorama has the 13-inch 2.5GHz MacBook Pro on sale for \$999 including free shipping plus NY & NJ sales tax only. Their price is \$100 off MSRP. Read more
Save up to \$600 with Apple refurbished Mac Pr...
The Apple Store is offering Apple Certified Refurbished Mac Pros for up to \$600 off the cost of new models. An Apple one-year warranty is included with each Mac Pro, and shipping is free. The... Read more

## Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Support Technician IV - Jack Henry a...
Job Description Jack Henry & Associates is seeking an Apple Support Technician. This position while acting independently, ensures the proper day-to-day control of Read more
*Apple* Client Systems Solution Specialist -...
…drive revenue and profit in assigned sales segment and/or region specific to the Apple brand and product sets. This person will work directly with CDW Account Managers Read more
*Apple* Software Support - Casper (Can work...
…experience . Full knowledge of Mac OS X and prior . Mac OSX / Server . Apple Remote Desktop . Process Documentation . Ability to prioritize multiple tasks in a fast pace Read more
*Apple* Software Support - Xerox Corporation...
…Imaging experience Full knowledge of Mac OS X and prior Mac OSX / Server Apple Remote Desktop Process Documentation Ability to prioritize multiple tasks in a fast pace Read more