Use MATLAB to code a script for the fixed-point iteration method

Use MATLAB to code a script for the fixed-point iteration method. Use examples to demonstrate the correctness of your code.

The Correct Answer and Explanation is:

The fixed-point iteration method is an iterative numerical technique used to approximate the solution of an equation of the form:x=g(x)x = g(x)x=g(x)

Where g(x)g(x)g(x) is a function derived from the original equation f(x)=0f(x) = 0f(x)=0. The method starts with an initial guess x0x_0x0​ and iteratively computes xn+1=g(xn)x_{n+1} = g(x_n)xn+1​=g(xn​) until the value converges to the solution.

MATLAB Script for Fixed-Point Iteration

Here’s a MATLAB script to implement the fixed-point iteration method:

matlabCopyEdit% Fixed-Point Iteration Method
% The function g(x) should be derived from the equation f(x) = 0

% Define the function g(x)
g = @(x) cos(x);  % Example: g(x) = cos(x)

% Define the tolerance and maximum number of iterations
tolerance = 1e-6;
max_iter = 100;

% Initial guess
x0 = 0.5;

% Fixed-Point Iteration
x = x0;  % Starting value
iter = 0;  % Iteration counter

fprintf('Iteration\tValue of x\n');
fprintf('%d\t%.6f\n', iter, x);

while iter < max_iter
    x_new = g(x);  % Compute the next approximation
    iter = iter + 1;  % Increment the iteration counter
    
    % Display the iteration and current value
    fprintf('%d\t%.6f\n', iter, x_new);
    
    % Check for convergence
    if abs(x_new - x) < tolerance
        fprintf('Convergence achieved after %d iterations\n', iter);
        break;
    end
    
    % Update x for next iteration
    x = x_new;
end

if iter == max_iter
    fprintf('Maximum iterations reached without convergence\n');
end

Explanation:

  1. Function Definition:
    • The function g(x) is specified as cos(x) in this example. You can modify this to reflect the specific function you are working with.
  2. Initial Guess and Parameters:
    • The initial guess (x0) is set to 0.5. The tolerance defines how close the iterated value must be to the previous value to stop the iterations. A max_iter value ensures the loop terminates after a set number of iterations to avoid infinite loops.
  3. Iteration Loop:
    • The loop iteratively computes the next value xn+1x_{n+1}xn+1​ from g(xn)g(x_n)g(xn​) and checks for convergence by comparing the difference between successive values. If the difference is less than the tolerance, the process stops.
  4. Output:
    • The script outputs the iteration number and the current value of xxx at each step. It also prints a message if convergence is achieved or if the maximum number of iterations is reached without convergence.

Example:

For the equation x=cos⁡(x)x = \cos(x)x=cos(x), the fixed-point iteration should converge to the value approximately x=0.739085x = 0.739085x=0.739085. Running this MATLAB script will demonstrate how the value approaches the solution with each iteration.

Conclusion:

This script provides a straightforward implementation of the fixed-point iteration method. You can adapt it by modifying the function g(x) for different equations, and adjusting the tolerance and maximum iterations as needed for specific problems.

Scroll to Top