In[49]:= f[x_] = Input"Enter the function : ";
Print"Graph of the function in the interval [-4,4]";
Plotf[x], x, - 4, 4, PlotStyle → Thick
x0 = Input"Enter first initial approximation : ";
x1 = Input"Enter second initial approximation : ";
Iff[x0] * f[x1] > 0,
Print"The given interval does not contain the root";
Exit[];
;
ε = Input"Enter the prescribed tolerance error : ";
k = Input"Enter the maximum number of iterations : ";
Print"Graph of the function in the interval [", x0, ", ", x1, "]";
Plotf[x], x, x0, x1, PlotStyle → Thick
Fori = 1, i ≤ k, i ++,
x2 = x0 * f[x1] - x1 * f[x0] f[x1] - f[x0];
Print"Iteration Number is ", i, " root = ", Nx2, 9;
IfAbs[f[x2]] < ε, Break[];
x0 = x1;
x1 = x2;
Ifi ⩵ k + 1 && Abs[f[x2]] > ε,
Print"Maximum no. of iterations has been exceeded",
Print"Desired accuracy has been achieved"
;
Print"The root of the equation ", f[x],
" = 0 after ", i - 1, " iterations is : ", Nx2, 9;
Print"The value of the function is : ", Nf[x2], 9;
Graph of the function in the interval [-4,4]
2
Out[51]=
60
40
20
-4 -2 2 4
-20
-40
Graph of the function in the interval [1, 1.5]
Out[58]=
1.0
0.5
1.1 1.2 1.3 1.4 1.5
-0.5
Iteration Number is 1 root = 1.3233
Iteration Number is 2 root = 1.368
Iteration Number is 3 root = 1.37372
Iteration Number is 4 root = 1.37347
Desired accuracy has been achieved
The root of the equation 2x - x3 = 0 after 3 iterations is : 1.37347
The value of the function is : 4.93875 × 10-6