My code is always returning one extra guess than needed how do I make sure it is always returning the correct amount

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.

New contributor

avengers5463 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật