The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
Found an interesting simple approximation for the integral of a function
06-15-2021, 09:16 PM (This post was last modified: 06-18-2021 03:13 AM by Namir.)
Post: #1
Found an interesting simple approximation for the integral of a function
Hi,

I found in the 9th edition of "Numerical Analysis" by Burden & Faires, a question (number 16) on page 202 about estimating an integral using:

Integral of f(x) from a to b = 9/4*h*f(x1) + 3/4*h*f(x2)

Where h = (b-a)/3, x1 = a + h, and x2 = b.

You can divide your interval (A, B) into a series of small (a, b) and reuse the above equation. Here is a Python implementation that uses the above equation is a series of small intervals:

Code:
from math import *

def fx(x):
  return 1/x
  
def Integral(fx,A,B,h):
  a = A
  b = a + h
  s = 0
  h2=h/3
  t=3/4*h2
  while a < B:
    s += t*(3*fx(a+h2)+fx(b))
    a = b
    b += h
  return s
  
def Exact(a,b):
  return log(b/a)
  
a = 1
b = 100
h = 0.01
integ =Integral(fx,a,b,h)
print("Area=",integ)
ex = Exact(a,b)
print("%error=", 100*(ex-integ)/ex)

The output is:

Code:
Area= 4.605170176738395
%error= 2.008546184997127e-07

Does anyone know about this rather unusual algorithm?

Namir
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Found an interesting simple approximation for the integral of a function - Namir - 06-15-2021 09:16 PM



User(s) browsing this thread: 2 Guest(s)