# -*- coding: utf-8 -*-
"""
Created on Wed Apr  2 08:20:22 2025

@author: 2017
"""

import numpy as np

# Numerisk integrasjon

def f(x):              #funksjonen
    return 4/(1+x**2)  
  #np.exp(x)
  #np.sin(x)
  #x**4  

a=0     #nedre grense
b= 1    #øvre grense
#np.pi  

n=40    #antall intervaller

delta = (b-a)/n  #intervallbredde

V = 0
x = a
for i in range(n):
     V = V + f(x)
     x = x + delta
V = V*delta    

H = V + (f(b) - f(a))*delta

T = (V+H)/2

M = 0
x = a + delta/2
for i in range(n):
     M = M + f(x)
     x = x + delta
M = M*delta 

S = (T+2*M)/3 

print('venstre: ', V)
print('høyre:   ', H)
print('trapes:  ', T)
print('midtpunkt', M)
print('Simpson: ', S)
print('pi       ',np.pi)
