(* 
      This notebook illustrates the determination of a zero of a function,  f[x],  
      using different methods *) 

ClearAll[f, x, xa, xn, xnp1, xnm1, df, i, a, b, 
      c]

f[x_] := Sin[5 x] - 0.2;
df[x_] = D[f[xa], xa] /. xa -> x;

Plot[f[x], {x, 0, 5}, PlotStyle -> Hue[0.7]]

df[x]

xn = 2.3;

      i = 1;

      While[(i < 20) && (Abs[f[xn]] > 
              (10^(-15))), 
xnp1 = 
          xn  -  f[xn]/df[xn]; 
Print[i, " ", 
          xnp1, " ", f[xnp1]]; 
xn = 
          xnp1; 
i++;
]

xn = 2.3; 
      xnm1 = 2.4;

      i = 1;

      While[(i < 20) && (Abs[f[xn]] > 
              (10^(-15))), 
xnp1 = 
          xn  -  (f[xn] (xn - xnm1))/(f[xn] - 
                  f[xnm1]); 
Print[i, " ", 
          xnp1, " ", f[xnp1]]; 
xnm1 = xn; 
        xn = xnp1; 
i++;
]

a = 2.0; b = 3.0;

      c = 0.5 (a + b);

      i = 1;

      While[(i < 50) && (Abs[f[c]] > 
              (10^(-15))), 
c = 
          0.5 (a + b); 
If[f[c] f[a] > 0,  a = c, 
          b = c]; 
Print[i, " ", c, " ", 
          f[c]]; 
i++;
]

a = 2.0; 
      b = 3.0;

      c = b - f[b] (b - a)/(f[b] - f[a]);

      i = 1;

      While[(i < 40) && (Abs[f[c]] > 
              (10^(-15))), 
c = 
          b - f[b] (b - a)/(f[b] - f[a]); 
If[
          f[c] f[a] > 0,  a = c, b = c]; 
Print[
          i, " ", c, " ", 
          f[c]]; 
i++;
]