You are on page 1of 3

ASG

Scientific Computer

import numpy as np
import math
def right_riemann(f, a, b, n):
h = (b - a) / (n - 1)
x = np.linspace(a, b, n)
y = 0
for i in x[1::]:
y += h * f(i)
return y

def trapezoid(f, a, b, n):


h = (b-a) / (n-1)
x = np.linspace(a, b, n)
n = 0
for i in x[1:n-1]:
n += f(i)
y = (f(x[0:1]) + (2*n) + f(x[-1::])) * (h/2)
return y

def simpson( a, b, n):


x = np.linspace(a, b, n)
h = (b-a) / (n-1)
f1 = (1/((x)-8)**5) + 1.5
i_simp = (h / 3) * (f1[0] + (4 * sum(f1[1:n-1:2]) + (2 * sum(f1[:n-
2:2]))) + f1[n-1])
err_simp = 2 - i_simp
return i_simp
import matplotlib.pyplot as plt
def func(x):
return (1/(x-8)**5) + 1.5
f = lambda x: (1/(x-8)**5) + 1.5
t = [2, 6, 8, 10, 14]
n = 4
j = 14
a = 0
for i in (t):
y = right_riemann(f, a, i, n)
y2 = trapezoid(f, a, i, n)
y3 = simpson(a, i, n)
print(f"( riemaan kanan) Kecepatan t={i} is: {y}")
print(f"(Trapezoid) Kecepatan t={i} is: {y2}")
print(f"(simpson's) kecepatan t={i} is: {y3}")
print("\n")
from scipy.integrate import cumtrapz
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use("seaborn-poster")

f = lambda x: (1/(x-8)**5) + 1.5


t = np.linspace(2,14,2)
v = right_riemann(f, 0, t, 4)
plt.figure(figsize = (8, 6))
plt.plot(t, v, 'b', )
plt.xlabel('t(s)')
plt.ylabel('v (m/s)')
plt.title('right riemman')
plt.show()
Source Code : asg.ipynb - Colaboratory (google.com)

You might also like