def trapezodial(expr, interval, N): func = function(expr) dx, fs = getIntervalPoints(N, func, interval) integral = 0.0 for i in range(N): integral += fs[i] + fs[i + 1] integral *= dx / 2 return integral, maxErr(func, dx, interval)
def rectangular(expr, interval, N): func = function(expr) dx, fs = getIntervalPoints(N, func, interval) # Integral computation integral = 0.0 for i in range(N): integral += fs[i] integral *= dx return integral, maxErr(func, dx, interval)
def simpson(expr, interval, N): func = function(expr) # Simpson rule requires that interval is separated in 2*n equal intervals dx, fs = getIntervalPoints(2 * N, func, interval) dx *= 2 # dx must be (b-a)/6 not (b-a)/12 integral = 0.0 for i in range(N): integral += fs[2 * i] + 4 * fs[2 * i + 1] + fs[2 * i + 2] integral *= dx / 6 return integral, maxErr(func, dx, interval)
def newton_raphson(expr, point=0, tolerance=0.001): """Uses newton raphson method to solve the equation: expr = 0""" f = function(expr) derivative = f.diff() k = 0 while True: k += 1 newPoint = point - f(point) / derivative(point) if abs(newPoint - point) < tolerance: break else: point = newPoint return point, k # (solution, iterations)
def bisection(expr, lo, hi, tolerance=0.001): """Uses bisection method to solve the equation: expr = 0""" f = function(expr) k = 1 while abs(hi - lo) >= tolerance: medium = (hi + lo) / 2 if f(lo) * f(medium) <= 0: hi = medium else: lo = medium k += 1 return lo, hi, k # (lower bound, higher bound, iterations)
def false_position(expr, lo, hi, tolerance=0.001): """Uses false position method to solve the equation: expr = 0""" f = function(expr) c, k = 1, 1 while abs(f(c)) >= tolerance: c = (lo * f(hi) - hi * f(lo)) / (f(hi) - f(lo)) if (f(lo) * f(c) <= 0): hi = c else: lo = c k += 1 return c, k # (solution, iterations)
def fixed_point(expr, point=0, tolerance=0.001): """Uses fixed point method to solve the equation: expr = 0""" # TODO: implement Aitken acceleration f = function(expr + "+x") k, maxiter = 0, 1000 while True: k += 1 newPoint = f(point) if abs(newPoint - point) < tolerance: break elif k > maxiter: return Exception("Failed to convert in %d iterations" % maxiter) else: point = newPoint return point, k # (solution, iterations)
# Func is a first degree polynomial and its derivative is constant # Sympy can't handle devitives of a constant function. If func has # a positive slope then we should return func(b), else we should # return func(a) return max(func(a), func(b)) secondder = func.diff(2) """ To find the global maximum of func in interval we first solve df/dx = 0 Then we check if the second derivative is negative on these points, which is the condition for checking for local maximum. Of these local maxima and the values of func at a and b we want the one that is bigger """ solutionsExpr = solve(firstder) solutions = list(map(lambda x: x.evalf(), solutionsExpr)) maxOfFunc = max(func(a), func(b)) for point in solutions: if secondder(point) < 0: # we have a local maximum maxOfFunc = max(func(point), maxOfFunc) return maxOfFunc if __name__ == "__main__": expr = input("Enter a function: ") f = function(expr) print(funcMax(f, [-10, 10])) print(getIntervalPoints(10, function('x'), [0, 1]))