Author: David Merkley
Language: Python
Task 1
Routine Name: RelativeError and AbsoluteError
Description/Purpose: Gives a relative or an absolute error. I have added these to my python package.
Input: x exact, and x approximate
Output: Gives a relative or absolute error.
Implementation/Code:
def relerr(xexact, xapprox):
output = abs(xexact - xapprox) / abs(xexact)
return output
def abserr(xexact, xapprox):
output = abs(xexact - xapprox)
return output
Task 2
Routine Name: StringGraph
Description/Purpose: This gives some graphs that can be graphed and compared using the matplotlib package.
Input: Functions, starting point, and an ending point
Output: Graphs of chosen functions.
Implementation/Code:
from matplotlib import pyplot as plt
import numpy as np
start = 0
end = 20
t = []
x = 0
t.append(x)
dx = (end - start) / 201
expression = '(400 * np.exp(0.8*x)) / (500 + 3 * np.exp(0.8*x))'
expression2 = '(500 * np.exp(0.9*x)) / (500 + 3 * np.exp(0.9*x))'
j = []
p = []
p.append(eval(expression))
i = 0
for i in range(200):
j.append(eval(expression2))
p.append(eval(expression))
x = x + dx
t.append(x)
plt.xlabel('time values')
plt.ylabel("population density values")
plt.plot(t, p, j)
plt.legend(["Population 1", "Population 2"])
plt.show()
Output:
Task 3
Routine Name: FixedPointIteration
Description/Purpose: This code gives an approximation of a root using a fixed point iteration. I have added this to my python package.
Input: A function f(x), the derivative of f(x), starting point x0, a tolerance, and a max iteration.
Output: Approximation of the root.
Implementation/Code:
from math import exp
def fixedPointIter(fx, tol=1e-10, x0=0, epsi=1, maxitter=1e10):
x1 = x0 - epsi * fx(x0)
counter = 0
while counter in range(int(maxitter)):
if abs(x1 - x0) < tol:
return x0
x0 = x1
x1 = x1 - epsi * fx(x1)
counter = counter + 1
def fx(x):
return (x + 10)**2 / 20
def f(x):
return x * exp(3 * x**2) - 7 * x
x = fixedPointIter(fx)
print(fx(x))
Output:
9.99990241352224e-11
Task 4
Routine Name: FixedPointIteration
Description/Purpose: Finds an approximate root using Fixed Point Iteration. The purpose here is that the commented out code will give an overflow error and will not calculate the root. When we change the epsilon, it will calculate the root though.
Input: A function f(x), the derivative of f(x), f’(x), starting point x0, a tolerance, and a max iteration.
Output: Approximation of the root.
Implementation/Code:
from math import exp
def fixedPointIter(fx, tol=1e-10, x0=0, epsi=1, maxitter=1e10):
x1 = x0 - epsi * fx(x0)
counter = 0
while counter in range(int(maxitter)):
if abs(x1 - x0) < tol:
return x0
x0 = x1
x1 = x1 - epsi * fx(x1)
counter = counter + 1
def fx(x):
return (x + 10)**2 / 20
def f(x):
return x * exp(3 * x**2) - 7 * x
x = fixedPointIter(fx)
print(fx(x))
# print(fixedPointIter(f, x0=1))
print(fixedPointIter(f, x0=1, epsi=1e-3))
Output:
Overflow Error
0.8053798620480475
Task 5
Routine Name: Bisection
Description/Purpose: Finds the approximate root using the Bisection method. This was added to my python package.
Input: A function f(x), the derivative of f(x), f’(x), starting point x0, a tolerance, and a max iteration.
Output: Approximation of a root.
Implementation/Code:
from math import exp
def f(x):
return x * exp(3 * x**2) - 7 * x
def bisection(f, a, b, maxiter=200):
fa = f(a)
fb = f(b)
if fa > 0 and fb < 0 or fa < 0 and fb > 0:
counter = 0
while counter in range(int(maxiter)):
c = (a + b) / 2
fc = f(c)
if fa * fc < 0:
b = c
else:
fa = fc
a = c
counter = counter + 1
c = (a + b) / 2
return c
else:
print("Values were not opposite signs")
x = bisection(f, .25, 2)
print(x, f(x))
Output:
0.8053798584219567 -8.881784197001252e-16
Task 6
Root-finding problems will generally happen with a fixed point; however, some problems will have no solution, one solution, a finite amount, or infinitely many solutions. There are many different ways to solve root-finding problems. Bisection method, or the fixed point iteration are 2 different ways to solve them that we used in this assignment. We just need to find which method is best in different situations.
[http://www.mathcs.emory.edu/~haber/math315/chap6.pdf] [http://www.cs.cornell.edu/courses/cs3220/2008su/slides/rootfinding.pdf]