Python Relaxation Method
Problem #2
Our current implementation of the relaxation method is quite crude in that we must specify the number of iterations that it performs, and then simply look at the output to see if we have converged to a fix-point. It would instead be better if we could have our algorithm check its own numbers to see if they are converging to a single value, and then terminate itself if it has converged.
We can measure how close our most recent guess is to a fixed-point by looking at our most-recent three guesses x_{n-2}, x_{n-1}, x_{n} , and seeing if x_{n-1} and x_{n} are closer to one another than are x_{n-1} and $x_{n-2}. Skipping a formal derivation, the following formula gives an upper-bound estimate on how close $x_{n}$ is to a true fixed-point:
begin{equation}
toleranceA =absolute{(x_n – x_{n-1})^2}/({2x_{n-1} – x_{n-2} – x_{n})}
end{equation}
That is, if your previous three guesses were 1.0, then 1.63, and then 1.80, plugging these into the preceding formula produces an error bound of $epsilon = 0.06. This means that the guess 1.80 is within 0.06 of the true fixed-point. To prevent division-by-zero errors, if your denominator is equal to 0.0, replace it with the value 1e-14
.
Armed with this formula, we can now write a much better algorithm, which can operate based on a tolerance rather than a strict number of iterations.
Write a second version of the relaxation-method. This function should accept four arguments:
a python function, which accepts a number as an input, and returns a float as an output
an initial guess for the fixed-point, x_{0}, a floating-point number
a tolerance value, a positive-valued floating-point number
a max number of iterations that your algorithm is permitted to run
Your algorithm should produce guesses until toleranceA is smaller than the specified tolerance value, or until the number of guesses produced (including the initial guess) matches/exceeds the max number of iterations. Like the last function, it should return a list of all the guesses. You will need to have three guesses before you can assess the tolerance.
Here is my code. It returns one more value on the list every time. Please help me debug it. Thank you!
def relaxation_method2(func, xo, tol, max_it):
“””
Performs the relaxation method to find a fixed-point for func
,
given the initial guess xo
. The relaxation process continues until
the error bound is less than tol
or until max_it
iterations are reached.
Parameters
----------
func : Callable[[float], float]
The function whose fixed point is being found.
xo : float
The initial "guess" value.
tol : float
A positive value that sets the maximum permissible error
in the final fixed-point estimate.
max_it : int
The maximum number of relaxation-guesses.
Returns
-------
List[float]
A list of the initial guess, and all of the subsequent guesses
generated by the relaxation method.
"""
# Initial guesses list
guesses = [xo]
# Generate the first two guesses
if max_it > 1:
guess1 = func(xo)
guesses.append(guess1)
if max_it > 2:
guess2 = func(guess1)
guesses.append(guess2)
# Iterate until max_it or tolerance is met
for _ in range(max_it - 3):
guess3 = func(guesses[-1])
# Calculate the error bound
den = 2 * guesses[-1] - guess3 - guesses[-2]
if den == 0:
den = 1e-14
num = (guesses[-2] - guesses[-1]) ** 2
epsilon_n = abs(num / den)
# Append the new guess
guesses.append(guess3)
# Check if the tolerance condition is met
if epsilon_n < tol:
break
return guesses
It returns one more value on the list every time. Please help me debug it.
avengers5463 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.