You are on page 1of 3

In[48]:= x0 = Input@"Enter initial guess : "D;

Nmax = Input@"Enter maximum number of iterations : "D;


eps = Input@"Enter a value of convergence parameter : "D;
Print@"x0=", x0D;
Print@"Nmax=", NmaxD;
Print@"epsilon=", epsD;
f@xD := Cos@xD;
Print@"fHxL := ", f@xDD;
Print@"f'HxL:= ", f '@xDD;
For@i = 1, i <= Nmax, i ++, x1 = N@x0 - Hf@xD . x ® x0L  Hf '@xD . x ® x0LD;
If @Abs@x1 - x0D < eps, Return@x1D, x0p = x0; x0 = x1D;
Print@"In ", i, "th Number of iterations the approximation to root is : ", x1D;
Print@" Estimated error is : ", Abs@x1 - x0pDDD;
Print@"the final approximation of root is : ", x1D;
Print@" Estimated error is : ", Abs@x1 - x0DD; Plot@f@xD, 8x, - 1, 3<D
x0=1

Nmax=5

epsilon=0.00001

fHxL := Cos@xD

f'HxL:= - Sin@xD

In 1th Number of iterations the approximation to root is : 1.64209

Estimated error is : 0.642093

In 2th Number of iterations the approximation to root is : 1.57068

Estimated error is : 0.0714173

In 3th Number of iterations the approximation to root is : 1.5708

Estimated error is : 0.00012105


Out[54]= Return@1.5708D

In[55]:= x0 = 1;
Nmax = 5;
eps = 0.0001;
f@xD := Cos@xD;
For@ i = 1, i <= Nmax, i ++,
x1 = N@x0 - Hf@xD . x ® x0L  Hf '@xD . x ® x0LD;
If @Abs@x1 - x0D < eps, Return@x1D, x0p = x0; x0 = x1D;
Print@"In ", i, "th Number of iterations the approximation to root is : ", x1D;
Print@" Estimated error is : ", Abs@x1 - x0pDDD;
Print@"the final approximation of root is : ", x1D;
Print@" Estimated error is : ", Abs@x1 - x0DD;
Plot@f@xD, 8x, - 1, 3<D
2 Untitled-3

In 1th Number of iterations the approximation to root is : 1.64209

Estimated error is : 0.642093

In 2th Number of iterations the approximation to root is : 1.57068

Estimated error is : 0.0714173

In 3th Number of iterations the approximation to root is : 1.5708

Estimated error is : 0.00012105


Out[59]= Return@1.5708D

1.0

0.5

Out[60]=
-1 1 2 3

-0.5

-1.0

In[61]:= newton@f_, x0_, Nmax_, eps_D :=


Module@8y0, x, x1, x0p<, y0 = x0; Print@"fHxL := ", f@xDD;
Print@"f'HxL:= ", f '@xDD; For@ i = 1, i <= Nmax, i ++, x1 = N@x0 - f@x0D  f '@x0DD;
If@Abs@x1 - x0D < eps, Return@x1D, x0p = x0; x0 = x1D; Print@"In ", i,
"th Number of iterations the approximation to root is : ", x1D;
Print@" Estimated error is : ", Abs@x1 - x0pDDD;
Print@"the final approximation of root is : ", x1D;
Print@" Estimated error is : ", Abs@x1 - x0DD; Plot@f@xD, 8x, - 1, 3<DD

In[62]:= f@xD := Cos@xD;


newton@f, 1, 5, 0.000001D
fHxL := Cos@x$6598D

f'HxL:= - Sin@x$6598D

Set::setraw : Cannot assign to raw object 1. ‡

In 1th Number of iterations the approximation to root is : 1.64209

Estimated error is : 0.642093

Set::setraw : Cannot assign to raw object 1. ‡

In 2th Number of iterations the approximation to root is : 1.64209

Estimated error is : 0.642093

Set::setraw : Cannot assign to raw object 1. ‡

General::stop : Further output of Set::setraw will be suppressed during this calculation. ‡


Untitled-3 3

In 3th Number of iterations the approximation to root is : 1.64209

Estimated error is : 0.642093

In 4th Number of iterations the approximation to root is : 1.64209

Estimated error is : 0.642093

In 5th Number of iterations the approximation to root is : 1.64209

Estimated error is : 0.642093

the final approximation of root is : 1.64209

Estimated error is : 0.642093


1.0

0.5

Out[63]=
-1 1 2 3

-0.5

-1.0

You might also like