Python | Scipy integrate.romberg() method

With the help of scipy.integrate.romberg() method, we can get the romberg integration of a callable function from limit a to b by using scipy.integrate.romberg() method.

Syntax : scipy.integrate.romberg(func, a, b)
Return : Return the romberg integrated value of a callable function.

Example #1 :
In this example we can see that by using scipy.integrate.romberg() method, we are able to get the romberg integration of a callable function from limit a to b by using scipy.integrate.romberg() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import numpy and scipy.integrate
import numpy as np
from scipy import integrate
gfg = lambda x: np.exp(-x**2)
  
# using scipy.integrate.romberg()
geek = integrate.romberg(gfg, 0, 3, show = True)
  
print(geek)

chevron_right


Output :

Romberg integration of <function vectorize1..vfunc at 0x00000209C3641EA0> from [0, 3]

 Steps  StepSize   Results
     1  3.000000  1.500185
     2  1.500000  0.908191  0.710860
     4  0.750000  0.886180  0.878843  0.890042
     8  0.375000  0.886199  0.886206  0.886696  0.886643
    16  0.187500  0.886205  0.886207  0.886207  0.886200  0.886198
    32  0.093750  0.886207  0.886207  0.886207  0.886207  0.886207  0.886207
    64  0.046875  0.886207  0.886207  0.886207  0.886207  0.886207  0.886207  0.886207
   128  0.023438  0.886207  0.886207  0.886207  0.886207  0.886207  0.886207  0.886207  0.886207

The final result is 0.8862073482595311 after 129 function evaluations.

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# import numpy and scipy.integrate
import numpy as np
from scipy import integrate
gfg = lambda x: np.exp(-x**2) + 1 / np.sqrt(np.pi)
  
# using scipy.integrate.romberg()
geek = integrate.romberg(gfg, 1, 2, show = True)
  
print(geek)

chevron_right


Output :

Romberg integration of <function vectorize1..vfunc at 0x00000209E1605400> from [1, 2]

 Steps  StepSize   Results
     1  1.000000  0.757287
     2  0.500000  0.713438  0.698822
     4  0.250000  0.702909  0.699400  0.699438
     8  0.125000  0.700310  0.699444  0.699447  0.699447
    16  0.062500  0.699663  0.699447  0.699447  0.699447  0.699447
    32  0.031250  0.699501  0.699447  0.699447  0.699447  0.699447  0.699447

The final result is 0.6994468414978009 after 33 function evaluations.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.