Skip to content

Commit fa3cc4c

Browse files
committed
trapezoidal and simpsons
1 parent e096312 commit fa3cc4c

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Some numerical methods in Python

curves.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# trapezoidal
2+
def trapezoidal(f,a,b,n):
3+
fs = list(map(lambda x: f(x), np.linspace(a,b,n+1)));
4+
return ( sum(fs)-(fs[0]+fs[-1])*0.5)*(b-a)/n
5+
6+
# simpson
7+
def simpson(f,a,b,n):
8+
r = np.linspace(a,b,n+1)
9+
total = 0
10+
for i in range(len(r)-1):
11+
total += (((b-a)/n)/6)*(f(r[i])+4*f((r[i]+r[i+1])/2)+f(r[i+1]))
12+
return total
13+
14+
def f(x):
15+
return 2/(x**2+1)
16+
17+
print(trapezoidal(f,-1.0,1.0,10.0))
18+
print(simpson(f,-1.0,1.0,10.0))
19+
20+
p = 4 # run through powers and check numbers are the same
21+
for i in range(1,20): # checking it experimentally
22+
print((simpson(np.cos,0.0,1.0,i)-np.sin(1.0))*(i**p))

precise.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# multiple precision
2+
3+
from mpmath import *
4+
mp.dps = 500
5+
newton(f,df,mpf(1))
6+
7+
random.uniform(-1,1)
8+
9+
from numpy import *

0 commit comments

Comments
 (0)