What is Simpson's method and how to implement it in the Pascal language

To calculate the value of the integral, althoughApproximate, there is an excellent method, named after its creator, the Simpson method. It is also called the parabolic method, because it uses the construction of a parabola. This figure is constructed as close as possible to the function. Actually, since it is impossible to construct a parabola whose points exactly coincide with the points of the function, the integral is approximately. The formula for finding it with the boundaries a and b looks like this: 1 / h * (y0+ 4y1+ 2y2+ 4y3+ ... + 4yn-1+ yn). Here we just need to calculate each y from 0 to n, where n is determined by ourselves - the more, the better, because the more y-s, the closer to the true value we get. As for h, this step is calculated by the following formula: (b-a) / (n-1).

simpso example

In theory, everything is quite simple, but it would have to beto realize all this in practice. For many programmers, there is no better way to solve a problem such as the Simpson-Pascal or Delphi method. In this environment, you can very simply not only calculate the integral, but also construct a function graph and even a trapezium built for it. So, we'll figure out how to quickly implement the Simpson method and, if desired, even explain how here and what is organized, for everyone interested.

But before that, remember how the integral looks. This is a figure that is bounded by lines starting on the x-axis, that is, a and b.

simpson method

So, to begin with, you need to create afunction for an integrable function (sorry for the tautology), in which you just need to write f: = and then what we will find the integral for. It is extremely important not to make a mistake in entering the function in Pascal. But this is a separate topic for conversation. The resulting code will look something like this:

function f (x: real): real;

And the main text of the function


f: = 25 * ln (x) + sin (10); {that's where you need to write the contents of your function}


Next, we write a function for the implementation of the Simpson method. The beginning will be something like this:

function simpsonmetod (a, b: real; n: integer): real;

Next, declare the variables:


s: real; {Intermediate amounts (further understand)}

h: real; {Step}

my: integer; {Just a counter}

mno: integer; {Regular multipliers}

And now, actually, the program itself:


h: = (b-a) / (n-1); {We calculate the step by the standard formula. Sometimes a step is written in the task, in which case this formula does not apply}

s: = f (b) + f (a); {Set the initial step value}

mno: = 4; {Remember the formula - 1 / h * (y0+ 4y1… here this 4 is written here, the second multiplier will be 2, but more on this}

Now the basic formula is:

for my: = 1 to n-2 do begin

s: = s + mno * f (a + h * mu); {To the sum we add the next factor multiplied by 4 * yn or 2 * yn }

if (mno = 4) then mno: = 2 else mno: = 4; {Here the multiplier also changes - if it is now 4, it changes to 2 and vice versa}


simpsonmetod: = s * h / 3; {Next, the resulting sum is multiplied by h / 3 according to the formula}


That's all - we do all the actions according to the formula. If you have not yet figured out how to apply the Simpson method to the main program, the example will help you.

So after writing all the functions we write


n: = 3; {Set n}

q: = simpsonmetod (a, b, n); {Since Simpson's method is to calculate the integral from a to b, there will be several calculation steps, so we organize the cycle}


q2: = q; {The previous step is remembered}

n: = n + 2;

q: = simpsonmetod (a, b, n); {And the next value is calculated}

until (abs (q-q2) <0.001); {The accuracy of the job is written, so until the required accuracy is achieved, you must repeat the same actions}

simpson pascal method

That's how he is - Simpson's method. In fact, nothing complicated, everything is written very quickly! Now open your Turbo Pascal and start writing the program.