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

Let g(x) = write a program (using Matlab/Octave or C/C++) that implements fixed

ID: 3168032 • Letter: L

Question

Let g(x) = write a program (using Matlab/Octave or C/C++) that implements fixed point iteration as discussed in class (i.e., do not use any built-in fixed point iteration functions, if they are available). If you like, your code may assume that the fixed point iteration will converge quadratically. Run your program using p0 1.5 and 10-6 What was the final p( n) value (include what value of n it was)? What was the final eC n 1) approximation (including what value of n it was)? Use 9 significant figures for your answers. Hint: Since e(n 1) - abs( p(n) p(n 1)); can only be calculated for n >= 2, you can use an if statement in your loop so that you only approximate the error when n - 2 and otherwise just set the error approximate to, for example, 10e.

Explanation / Answer

clc;
clear all;
f=@(x)(x/2)+(1/x);
x0=1.5;
del=1e-6;% tolerance
err=0.1;
n=0;
while(err>del)
    y2=f(x0);
    err(n+1)=abs(x0-y2);
    x0=y2;
    n=n+1;
    x(n+1)=x0;
end
n

disp('num_iter          x_value                erorr')
disp('_______________________________________________________________________________')
for i=1:n
fprintf('%d %20f %20f ',i ,x(i),err(i))
end

n =

     4

num_iter          x_value                erorr
_______________________________________________________________________________
1                  0.000000                  0.083333
2                  1.416667                  0.002451
3                  1.414216                  0.000002
4                  1.414214                  0.000000