Practical -2
Program for Newton Raphson Method with number of iteration as stop-
ping criteria
In[1]:= NewtonRaphson[x0_, n_, f_] := Module[{xk, xk1}, xk = N[x0]; k = 0;
Output = {{k, x0, f[x0]}};
While[k < n,
fPrimexk = f '[xk];
If[fPrimexk 0, Print["The derivative of function at", k,
"th iteration is zero,we can not proceed furtherwith the iteration scheme"];
Break[]];
xk1 = xk - f[xk] / fPrimexk;
xk = xk1; k ++;
Output = Append[Output, {k, xk, f[xk]}];];
Print[
NumberForm[TableForm[Output, TableHeadings {None, {"k", "x k ", "f[xk ]"}}], 10]];
Print["Root after ", n, "iterations xk = ", NumberForm[xk, 10]];
Print["Function value at approximated root,f[x k ] = ", NumberForm[f[xk], 10]];];
f[x_] := x ^ 3 - 5 x + 1;
NewtonRaphson[0.5, 5, f]
k xk f[xk ]
0 0.5 -1.375
1 0.1764705882 0.1231426827
2 0.2015680743 0.0003492763989
3 0.2016396751 3.100484314 × 10-9
4 0.2016396757 1.110223025 × 10-16
5 0.2016396757 1.110223025 × 10-16
Root after 5iterations xk = 0.2016396757
Function value at approximated root,f[xk ] = 1.110223025 × 10-16
In[4]:=
f[x_] := x ^ 2 - 2;
NewtonRaphson[1, 4, f]
k xk f[xk ]
0 1 -1
1 1.5 0.25
2 1.416666667 0.006944444444
3 1.414215686 6.007304883 × 10-6
4 1.414213562 4.510614104 × 10-12
Root after 4iterations xk = 1.414213562
Function value at approximated root,f[xk ] = 4.510614104 × 10-12
Program for Newton Raphson Method with error tolerance as stopping criteria
2 P-2 [Link]
In[6]:= NewtonRaphsonError[x0_, error_, f_] := Module[{xk, xk1}, xk = N[x0];
k = 0;
Output = {{k, x0, f[x0]}};
approxerror = 10 000 000;
While[approxerror > error,
fPrimexk = f '[xk];
If[fPrimexk 0, Print["The derivative of function at", k,
"th iteration is zero,we can not proceed furtherwith the iteration scheme"];
Break[]];
xk1 = xk - f[xk] / fPrimexk;
approxerror = Abs[xk1 - xk];
xk = xk1; k ++;
Output = Append[Output, {k, xk, f[xk], approxerror}];];
Print[NumberForm[TableForm[Output,
TableHeadings {None, {"k", "xk ", "f[xk ]", "Approx Error"}}], 10]];
Print["Number of iteration required to acheive desired accuracy = ", k];
Print["Root after ", n, "iterations xk = ", NumberForm[xk, 10]];
Print["Function value at approximated root,f[x k ] = ", NumberForm[f[xk], 10]];];
f[x_] := x ^ 3 - 5 x + 1;
error = 10 ^ - 4;
NewtonRaphsonError[0.5, error, f]
k xk f[xk ] Approx Error
0 0.5 -1.375
1 0.1764705882 0.1231426827 0.3235294118
2 0.2015680743 0.0003492763989 0.0250974861
3 0.2016396751 3.100484314 × 10-9 0.00007160074946
Number of iteration required to acheive desired accuracy = 3
Root after niterations xk = 0.2016396751
Function value at approximated root,f[xk ] = 3.100484314 × 10-9
In[10]:= f[x_] := x ^ 2 - 2;
error = 10 ^ - 4;
NewtonRaphsonError[0.5, error, f]
k xk f[xk ] Approx Error
0 0.5 -1.75
1 2.25 3.0625 1.75
2 1.569444444 0.4631558642 0.6805555556
3 1.421890364 0.02177220671 0.1475540806
4 1.414234286 0.00005861552843 0.007656077875
5 1.414213563 4.294600231 × 10-10 0.00002072341514
Number of iteration required to acheive desired accuracy = 5
Root after niterations xk = 1.414213563
Function value at approximated root,f[xk ] = 4.294600231 × 10-10
P-2 [Link] 3
Program for Newton raphson method with maximum number of iteration and
error tolerance as stopping criteria
In[13]:= NewtonRaphsonErrorIter[x0_, n_, error_, f_] := Module[{xk, xk1}, xk = N[x0];
k = 0;
Output = {{k, x0, f[x0], None}};
approxerror = 10 000 000;
While[n > k && approxerror > error,
fPrimexk = f '[xk];
If[fPrimexk 0, Print["The derivative of function at", k,
"th iteration is zero,we can not proceed furtherwith the iteration scheme"];
Break[]];
xk1 = xk - f[xk] / fPrimexk;
approxerror = Abs[xk1 - xk];
xk = xk1; k ++;
Output = Append[Output, {k, xk, f[xk], approxerror}];];
Print[NumberForm[TableForm[Output,
TableHeadings {None, {"k", "xk ", "f[xk ]", "Approx Error"}}], 10]];
If[k < n, Print["The stated accuracy achieved in fewer iteration (less than", n, ")"],
Print["Maximum allowed", n,
" iteration are performed, staed accuracy is not achieved"]];
Print["Number of iteration required to acheive desired accuracy = ", k];
Print["Root after ", n, "iterations xk = ", NumberForm[xk, 10]];
Print["Function value at approximated root,f[x k ] = ", NumberForm[f[xk], 10]];];
f[x_] := x ^ 4 - x - 10;
error = 10 ^ - 4;
Plot[f[x], {x, 0, 4}]
NewtonRaphsonErrorIter[2, 5, error, f]
Out[16]=
250
200
150
100
50
1 2 3 4
4 P-2 [Link]
k xk f[xk ] Approx Error
0 2 4 None
1 1.870967742 0.3826745683 0.1290322581
2 1.855780702 0.004818128482 0.01518704024
3 1.855584561 7.94894218 × 10-7 0.0001961406858
4 1.855584529 1.953992523 × 10-14 3.236994561 × 10-8
The stated accuracy achieved in fewer iteration (less than5)
Number of iteration required to acheive desired accuracy = 4
Root after 5iterations xk = 1.855584529
Function value at approximated root,f[xk ] = 1.953992523 × 10-14
In[18]:= NewtonRaphsonErrorIter[1, 7, error, f]
k xk f[xk ] Approx Error
0 1 -10 None
1 4.333333333 338.2716049 3.333333333
2 3.29083438 103.989205 1.042498954
3 2.556206408 30.13944822 0.7346279719
4 2.098236403 7.284615179 0.4579700042
5 1.895608769 1.016430452 0.2026276341
6 1.85688202 0.03189669855 0.03872674972
7 1.855585943 0.00003473593956 0.001296076507
Maximum allowed7 iteration are performed, staed accuracy is not achieved
Number of iteration required to acheive desired accuracy = 7
Root after 7iterations xk = 1.855585943
Function value at approximated root,f[xk ] = 0.00003473593956