Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Write a MATLAB function, called fixed point_iteration that inputs a function,

ID: 2073737 • Letter: 2

Question

2. Write a MATLAB function, called fixed point_iteration that inputs a function, g, an initial guess To, an error tolerance, tol, and a maximum number of iterations, N, and outputs the fixed point of g, obtained using the fixed point iteration, starting with To. Your function should have an error defined by En1, and stop when the error is less than the tolerance, or if the number of iterations exceeds N - whichever happens first. Your function header should look something like: function [c,n,err] fixed-p oint iteration (g, x0,tol,N)

Explanation / Answer

The Matlab function is given below:

Matlab code:

% This function implements fixed point iteration method.
function [c,n,err] = fixed_point_iteration(g,x0,tol,N)
% Output
% c is the output of the variable after convergence
% n is the iteration number when exit
% err is the error at the time of termination
% Input   
% g is the input function which computes x'
% x0 is the initial guess
% tol is the error tolerance
% N is the maximum number of iterations

x(1) = x0; % Intial guess

for i = 1:N
x(i+1) = g(x(i));
err = abs(x(i+1)-x(i)); % Calculate error
if err < tol % Compare error with tollerence value
break % Exit loop
end
end
c = x(end); % Return x value
n = i; % Return the value of iteration

An example function is used to test the code. g(x) = 1+1/x;

This is used to solve the differential equatin x^2-x-1 = 0.

This can be defined in the command prompt by typing

g = @(x) 1+1/x;

The solutions was found to be 1.618.
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote