Search code examples
pythonfunctionmathencodingnumeric

Fixed-point method function doesn't show output


I did a function for calculating fixed-point method in numerical but when I run the program there is no output. I just cant see where the problem is and I tried adding a max iteration but still it didn't work or when I tried filling the fxI_values it gave me an Index Error: list index out of range value for

xl=1
error=0.01

def fixedPoint(xL,error):
    xI=xL
    x0=xI
    
    xI_values=[]
    fxI_values=[]
    calc_error=[]

    while True:

        xI_values.append(xI)
        absError = abs(xI-x0)

       
        xI=fixedPoint_func(xI)
        
        calc_error.append(absError)

        # Check for convergence
        if absError <= error:
            break
        
 # Prepare data for tabulation
    table_data5 = []
    for j in range(len(xI_values)):
        iteration_number = j + 1
        table_data5.append([iteration_number, xI_values[j], fxI_values[j], calc_error[j]])

    # Print table
    headers = ["Iteration", "Xi", "g(Xi)", "Error"]
    print("Fixed Point Method :")
    print()
    print(tabulate(table_data5, headers=headers, floatfmt=".4f"))

    # Print a separator line
    print("-" * 100)
    print()  # Print an empty line for spacing

    return xI_values[-1]

# Fixed Point Function
def fixedPoint_func(x):
    return 3/x

# Call the fixedPoint method
last_FxN = fixedPoint(xL,error)

Solution

  • This code is a working example.

    from tabulate import tabulate
    from math import cos
    
    def fixedPoint(xL,error):
        xI=xL
        x0=xI
        
        xI_values=[]
        fxI_values=[]
        calc_error=[]
    
        while len(xI_values) < 20:
    
            xI_values.append(xI)
    
            x0 = xI
            xI=fixedPoint_func(xI)
            absError = abs(xI-x0)
            fxI_values.append(xI)
            calc_error.append(absError)
    
            # Check for convergence
            if absError <= error:
                break
            
     # Prepare data for tabulation
        table_data5 = []
        for j in range(len(xI_values)):
            iteration_number = j + 1
            table_data5.append([iteration_number, xI_values[j], fxI_values[j], calc_error[j]])
    
        # Print table
        headers = ["Iteration", "Xi", "g(Xi)", "Error"]
        print("Fixed Point Method :")
        print()
        print(tabulate(table_data5, headers=headers, floatfmt=".4f"))
    
        # Print a separator line
        print("-" * 100)
        print()  # Print an empty line for spacing
    
        return xI_values[-1]
    
    # Fixed Point Function
    def fixedPoint_func(x):
    #    return 3/x
        return cos(x)
    
    xL = 1
    error = 0.01
    # Call the fixedPoint method
    last_FxN = fixedPoint(xL,error)
    print(last_FxN)
    

    I've made a few changes

    1. Add import statement for tabulate function
    2. Add an import for cos function, I'll explain why below
    3. Move the initilisation of xL and error to near the end, not important
    4. Change the while loop to while len(xI_values) < 20: to ensure it always terminates
    5. Store the current value of xI in x0
    6. Add the line fxI_values.append(xI) to store the result of the function. This is the missing line causing the error
    7. Change the fixed point function to one known to converge

    The initial error was caused by the line

    table_data5.append([iteration_number, xI_values[j], fxI_values[j], calc_error[j]])
    

    The problem being that fxI_values is an empty list, so trying to find the value at position j fails. Adding a line in the loop

    fxI_values.append(xI)
    

    fixes this, but you need to watch the order of when things are stored.

    When I fixed this and then ran the code it then got stuck in an endless loop. This is because the function x' = 3/x, never converges. Starting with x=1, it goes 3, 1, 3, 1, 3 ... changing the while statement so it always halts after 20 iterations prevents the endless loop.

    I've also changed the function that will converge.

    # Fixed Point Function
    def fixedPoint_func(x):
        return cos(x)
    

    This might not be what you want, if you want to see how many iterations it takes to get back to the starting point, you will need to remove the x0 = xI line.