Simpson's rule in Python

前端 未结 6 2164
悲&欢浪女
悲&欢浪女 2021-01-06 00:43

For a numerical methods class, I need to write a program to evaluate a definite integral with Simpson\'s composite rule. I already got this far (see below), but my answer is

6条回答
  •  长发绾君心
    2021-01-06 01:03

    There is my code (i think that is the most easy method). I done this in jupyter notebook. The easiest and most accurate code for Simpson method is 1/3.

    Explanation

    For standard method (a=0, h=4, b=12) and f=100-(x^2)/2

    We got: n= 3.0, y0 = 100.0, y1 = 92.0, y2 = 68.0, y3 = 28.0,

    So simpson method = h/3*(y0+4*y1+2*y2+y3) = 842,7 (this is not true). Using 1/3 rule we got:

    h = h/2= 4/2= 2 and then:

    n= 3.0, y0 = 100.0, y1 = 98.0, y2 = 92.0, y3 = 82.0, y4 = 68.0, y5 = 50.0, y6 = 28.0,

    Now we calculate the integral for each step (n=3 = 3 steps):

    Integral of the first step: h/3*(y0+4*y1+y2) = 389.3333333333333

    Integral of the second step: h/3*(y2+4*y3+y4) = 325.3333333333333

    Integral of the third step: h/3*(y4+4*y5+y6) = 197.33333333333331

    Sum all, and we get 912.0 AND THIS IS TRUE

    x=0
    b=12
    h=4
    x=float(x)
    h=float(h)
    b=float(b)
    a=float(x)
    def fun(x): 
        return 100-(x**2)/2
    h=h/2
    l=0  #just numeration
    print('n=',(b-x)/(h*2))
    n=int((b-a)/h+1)
    y = []   #tablica/lista wszystkich y / list of all "y"
    yf = []
    for i in range(n):
        f=fun(x)
        print('y%s' %(l),'=',f)
        y.append(f)
        l+=1
        x+=h
    print(y,'\n')
    n1=int(((b-a)/h)/2)  
    l=1
    for i in range(n1):
        nf=(h/3*(y[+0]+4*y[+1]+y[+2]))
        y=y[2:]  # with every step, we deleting 2 first "y" and we move 2 spaces to the right, i.e. y2+4*y3+y4
        print('Całka dla kroku/Integral for a step',l,'=',nf)
        yf.append(nf)
        l+=1
    print('\nWynik całki/Result of the integral =', sum(yf) )
    

    At the beginning I added simple data entry:

    d=None
    while(True):
        print("Enter your own data or enter the word "test" for ready data.\n")
        x=input ('Enter the beginning of the interval (a): ') 
        if x == 'test':
            x=0
            h=4  #krok (Δx)
            b=12 #granica np. 0>12  
            #w=(20*x)-(x**2)  lub   (1+x**3)**(1/2)
            break
        h=input ('Enter the size of the integration step (h): ')
        if h == 'test':
            x=0
            h=4 
            b=12 
            break
        b=input ('Enter the end of the range (b): ')
        if b == 'test':
            x=0
            h=4  
            b=12 
            break 
        d=input ('Give the function pattern: ')
        if d == 'test':
            x=0
            h=4  
            b=12
            break
        elif d != -9999.9:
            break
    
    x=float(x)
    h=float(h)
    b=float(b)
    a=float(x)
    
    if d == None or d == 'test':
        def fun(x): 
            return 100-(x**2)/2 #(20*x)-(x**2)
    else:
        def fun(x): 
            w = eval(d)
            return  w
    h=h/2
    l=0  #just numeration
    print('n=',(b-x)/(h*2))
    n=int((b-a)/h+1)
    y = []   #tablica/lista wszystkich y / list of all "y"
    yf = []
    for i in range(n):
        f=fun(x)
        print('y%s' %(l),'=',f)
        y.append(f)
        l+=1
        x+=h
    print(y,'\n')
    n1=int(((b-a)/h)/2)  
    l=1
    for i in range(n1):
        nf=(h/3*(y[+0]+4*y[+1]+y[+2]))
        y=y[2:]
        print('Całka dla kroku/Integral for a step',l,'=',nf)
        yf.append(nf)
        l+=1
    print('\nWynik całki/Result of the integral =', sum(yf) )
    

提交回复
热议问题