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

To calculate the value of the integral, although approximate, there is a wonderful method named after its creator - the Simpson method. It is also called the parabola method because it uses the construction of a parabola. This figure is built 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 found approximately. The formula for finding it with borders a and b looks like this: 1/h(y0+4y1+2y2 +4y3+…+4yn-1+y ). Here we just need to calculate each y from 0 to n, where we determine n ourselves - the more, the better, because the more y-s there are, the closer to the true value we get. As for h, this is a step and is calculated using the following formula: (b-a)/(n-1).

In theory, everything is quite simple, but it would be necessary to implement all this in practice. For many programmers, there is no better way to solve a problem like the Simpson method - Pascal or Delphi. In this environment, it is very easy not only to calculate the integral, but also to build a graph for itfunctions and even a trapezoid built to it. So, let's see how you can quickly implement the Simpson method and, if you wish, even explain yourself how it is here and what is organized, to everyone who is interested.

But before that, let's remember what the integral looks like. This is a figure that is limited by lines starting on the x-axis, that is, a and b.

So, first you need to create a function in the program for the integrable function (sorry for the tautology), in which you just need to write f:=and what we will find the integral for. Here it is extremely important not to make a mistake in entering the function in Pascal. But this is a separate topic for discussion. The resulting code will look something like this:

function f(x:real):real;

And the main text of the function

begin

f:=25ln(x)+sin(10); {here you need to write the contents of your function}

end;

Next, let's write a function to implement Simpson's method. The beginning will be something like this:

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

Next, declare variables:

var

s:real; { Subtotals (you'll understand later) }

h:real; { Step }

my:integer; { Just a counter }

mno:integer; { Regular multipliers }

And now, actually, the program itself:

begin

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

s:=f(b)+ f(a); { Initial step value is set }

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

Now the same basic formula:

for my:=1 to n-2 do begin

s:=s+mnof(a+hmu); { Add another multiplier to the sum, multiplied by 4y or 2 y}

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

end;

simpsonmethod:=sh/3; { Next, multiply the sum obtained as a result of the loop by h/3 according to the formula}

end.

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 in the main program, the example will help you with this.

So after writing all the functions, we write

Begin

n:=3; { Set n }

q:=simpsonmethod(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 a loop }

repeat

q2:=q; { Remember previous step }

n:=n+2;

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

until (abs(q-q2)<0.001);{ The accuracy is written in the task, so until the required accuracy is reached, you need to repeat the same steps }