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

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

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).

simpson method example
simpson method example

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.

simpson method
simpson method

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 }

simpson pascal method
simpson pascal method

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

Popular topic

Editor's choice

  • Let's build iOs apps! Overview of programs, instructions, recommendations
    Let's build iOs apps! Overview of programs, instructions, recommendations

    Today more and more people are starting to dive into the world of IT. A fairly common industry is the creation of various kinds of mobile applications. Quite a lot of different games and programs have already been developed for Android. It is because of this that many developers have begun to move to the iOS platform. This is not a very crowded industry where you can find a lot of clients and make quite a lot of money

  • Moving WordPress to another hosting: features, procedure
    Moving WordPress to another hosting: features, procedure

    Today, every person who has his own website on the Internet may face the problem that he will need to transfer the site to another hosting. There can be many different reasons for this. Of course, the most common of them is dissatisfaction with the services provided. If you can't take it anymore, then you need to act. This is a rather long procedure. If you do everything step by step, you can safely transfer the site, and not redo everything several times

  • How to pass on the rights in "SAMP RP"?
    How to pass on the rights in "SAMP RP"?

    Every person who plays online games is trying to upgrade his character and achieve many interesting things. Users who prefer SAMP try to get a driver's license even from the first level. Experienced players may not take the exam, but simply go to the instructor and buy rights from him. Of course, it will be somewhat more expensive, but this way you can save your time. How to pass on the rights to "SAMP"? Let's review this

  • How to turn off the camera on a laptop? 3 easy ways
    How to turn off the camera on a laptop? 3 easy ways

    Today, hacking has begun to actively develop in the world of information technology. An experienced hacker can get into almost any computer and get the user's information, provided, of course, the laptop is connected to the network. Recently, users have been complaining that their webcam turns on by itself. This way hackers can see where you are and what you are doing. In the future, this information can be used in completely different ways, up to the fact that you become a star on YouTube

  • Shared AppStore Account Features
    Shared AppStore Account Features

    A shared App Store account has many features. It allows users to install games and applications, as well as watch movies in high definition. Shared accounts give official access to app and game downloads for Apple devices (iPhone, iPad, iPod Touch)