ComputersProgramming

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

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 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 * (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 is determined by ourselves - the more, the better, because the more y-s are, the closer to the true value we get. As for h, this step is calculated by the following formula: (ba) / (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 such as the Simpson-Pascal or Delphi method. In this environment, it is very simple not only to calculate the integral, but also to 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.

So, to begin with, you need to create a function for the integrable function (sorry for the tautology) in the program, in which you just need to write f: = and what we will find the integral for. It is extremely important not to make a mistake in entering the function in the Pascal language. 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

Begin

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

End;

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:

Var

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

H: real; {Step}

My: integer; {Just a counter}

Mno: integer; {Regular multipliers}

And now, actually, the program itself:

Begin

H: = (ba) / (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 * (y 0 + 4y 1 ... this 4 is written here, the second multiplier is 2, but more on this}

Now the same basic formula:

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 * y n or 2 * y n }

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

End;

Simpsonmetod: = s * h / 3; {Then the resulting sum is multiplied 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 to the main program, the example will help you.

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 we organize the cycle}

Repeat

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}

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.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.