Author: David Merkley
Language: Python
Software Manual The code that is needed to complete the assignment is all listed below.
Task 1
Routine Name: RootFinding
Description/Purpose: This was just to complete the Task 1. Just has a lot of different code coming together to find roots for a function. We will need the function and the derivative, also with it being symmetrical, any positive root is also a negative root. We just search through small intervals and then we will find an interval that has a root in it. I used my code from my software manual.
Implementation/Code:
def f(x):
return np.exp(-x**2) * np.sin(4 * x**2 - 1) + 0.051
def derivf(x):
return 8 * x * np.exp(-x**2) * np.cos(4 * x**2 - 1) - 2 * x * np.exp(-x**2) * np.sin(4 * x**2 - 1)
def bracketRoot(f, h, n):
fx2 = f(0)
for i in range(1, n):
fx1 = fx2
fx2 = f(i * h)
if (fx1 * fx2 < 0):
return [(i-1) * h, i * h]
bracketRoot(f, .1, 100)
fixedPointIter(f, x0=.45, epsi=1e-3)
secantMethod(f, .4, .5)
newtonMethod(f, derivf, .45)
bisection(f, .4, .5)
Output:
[0.4, 0.5]
0.4836106985428367
0.4836106985428367
0.4836106985428367
0.4836106985428367
Task 2
Routine Name: NewtonRoot
Description/Purpose: This code tried to find a zero, but for both -5 and 6 there was a division by zero error occuring. The zero was occuring because x was a number that got to close to inf or -inf. I used my code from my software manual.
Implementation/Code:
newtonMethod(f, derivf, 6)
newtonMethod(f, derivf, -5)
Output:
Divide by zero error
Task 3
Routine Name: HybridMethod
Description/Purpose: The bisection did not work for [-5, 6]. This is because both of the values are positive at this range. But we do know that we can use [.4, .5] because that is in the range of [-5, 6]. I used my code from my software manual.
Implementation/Code:
hybridMethod(f, derivf, .4, .5)
Output:
0.4836106985428367
Task 4
Routine Name: HybridSecant
Description/Purpose: The bisection did not work for [-5, 6]. This is because both of the values are positive at this range. But we do know that we can use [.4, .5] because that is in the range of [-5, 6]. I used my code from my software manual.
Implementation/Code:
hybridsecant(f, derivf, .4, .5)
Output:
0.4836106985428367
Task 5
Routine Name: Parallel
Description/Purpose: This code searches through the roots to find multiple places where there are roots. Also since it is even all of these roots can be on the negative end as well. When plugging in all of those into the root finding method I get an output below.
Implementation/Code:
def bracketRoot(f, h, n):
variable = -5
while variable <= 6:
fx2 = f(variable)
for j in range(1, n):
fx1 = fx2
fx2 = f(j * h)
if (fx1 * fx2 < 0):
print([(j-1) * h, j * h])
variable += 0.1
Output:
[0.4, 0.5]
Hybrid: 0.4836106985
[1.0, 1.1]
Hybrid: 1.035767106
[1.3, 1.4000000000000001]
Hybrid: 1.321586183
Task 6
The article I looked at talked about isolating roots in one dimension. Told me to make sure that I can find out as much about the function as I can beforehand. Things like limits, diverges anywhere, smooth graph, does it have a derivative, and could it be even multiplicity. These things can help when trying to find the roots. With polynomials, you can also be certain that you found all of the roots, but non-polynomials are much more difficult to know that you found all of the roots.