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

To calculate the value of the integral, although approximate, there is an excellent 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’s impossible to construct a parabola for which the points exactly coincide with the points of the function, the integral is also approximately. The formula for finding it with the boundaries a and b looks like this: 1 / h * (y 0 + 4y 1 + 2y 2 + 4y 3 + ... + 4y n-1 + y n ). Here we just need to calculate each y from 0 to n, where n we determine ourselves - the more, the better, because the more y-s, the closer to true we get the value. As for h, this is a step and is calculated by the following formula: (ba) / (n-1).

simpson method example

In theory, everything is quite simple, but it would be necessary to put all this into practice. For many programmers, there is no better way to solve such a problem as the Simpson - Pascal or Delphi method. In this environment, you can very simply not only calculate the integral, but also construct a graph of the function and even a trapezoid built to it. So, we will figure out how to quickly implement the Simpson method and, if you wish, to explain to yourself how everyone is organized and what is organized here.

But before that, 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.

simpson method

So, for starters, in the program you need to create a function for an 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 a 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: = 25 * ln (x) + sin (10); {here you need to write the contents of your function}

end;

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

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

Next, declare the variables:

var

s: real; {Subtotals (understand further)}

h: real; {Step}

my: integer; {Just a counter}

mno: integer; {Regular factors}

And now, in fact, the program itself:

begin

h: = (ba) / (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); {Set the initial value of the step}

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

Now that same basic formula:

for my: = 1 to n-2 do begin

s: = s + mno * f (a + h * mu); {Add to the sum another factor multiplied by 4 * y n or 2 * y n }

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

end;

simpsonmetod: = s * h / 3; {Further, we multiply the sum obtained as a result of the cycle by h / 3 according to the formula}

end.

That's all - we do all the actions according to the formula. If you still do not understand how to apply the Simpson method in the main program, an example will help you with this.

So after writing all the functions we write

Begin

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 organize a loop}

repeat

q2: = q; {Remember the previous step}

n: = n + 2;

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

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

simpson pascal method

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

Source: https://habr.com/ru/post/K7860/


All Articles