@anonymous/

OrneryDismalMeasurements

Python 2.7

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# python3
import numpy as np

def trapez(f, a, b , k ):
  h=1.0/ k
  ii=range(1,k)
  return (h/2.0)*(f(a)+f(b)+2*sum([f(a+i*h) for i in ii]))
  

def mittelpunkt(f, a, b , k ):
  h=1.0/ k
  ii=range(1,k+1)
  return h*(sum([f(a+(i-0.5)*h) for i in ii]))
  

def simpson(f, a, b , k ):
  h=1.0/ k
  ii1=range(1,k)
  ii2=range(1,k+1)
  return h/6.0*(f(a)+f(b)+2*sum([f(a+i*h) for i in ii1])+4*sum([f(a+(i-0.5)*h) for i in ii2]))
  

def romberg(f,a,b,k):
  def T(f,a,b,l,k):
    if(l==0):
      return trapez(f,a,b,2**k)
    return 1.0/(4**l-1)*(4**l*T(f,a,b,l-1,k+1)-T(f,a,b,l-1,k))
  return T(f,a,b,k,0)
  

def fun(x):
  return 1.0/(1+x**2)
  
  
print("\n f=1/1+x^2, a=0, b=1, k=1")
print ("genaue Lösung: %2.12f" % np.arctan(1))
print ("Trapezregel : %2.12f" % trapez(fun, 0, 1, 1))
print ("Mittelpunktregel: %2.12f" % mittelpunkt(fun, 0, 1, 1))
print ("Simpsonregel %2.12f" % simpson(fun, 0, 1, 1)) 
  
print("\n f=1/1+x^2, a=0, b=1, k=2")
print ("genaue Lösung: %2.12f" % np.arctan(1))
print ("Trapezregel : %2.12f" % trapez(fun, 0, 1, 2))
print ("Mittelpunktregel: %2.12f" % mittelpunkt(fun, 0, 1, 2))
print ("Simpsonregel %2.12f" % simpson(fun, 0, 1, 2))

print("\n f=1/1+x^2, a=0, b=1, k=4")
print ("genaue Lösung: %2.12f" % np.arctan(1))
print ("Trapezregel : %2.12f" % trapez(fun, 0, 1, 4))
print ("Mittelpunktregel: %2.12f" % mittelpunkt(fun, 0, 1, 4))
print ("Simpsonregel %2.12f" % simpson(fun, 0, 1, 4))

print("Rombergverfahren %2.12f" % romberg(fun, 0, 1, 4))