0% found this document useful (0 votes)
6 views5 pages

Numerical Prac4c(Secant Method)

The document describes the implementation of the Secant Method for finding roots of functions using a series of iterations based on user-defined parameters. It includes three questions with different functions: Cos[x], x^3 - 5x + 1, and Cos[x] - x * Exp[x], providing example outputs for each case. The results show the estimated roots and errors after a specified number of iterations, along with plots of the functions.

Uploaded by

duaashish2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Numerical Prac4c(Secant Method)

The document describes the implementation of the Secant Method for finding roots of functions using a series of iterations based on user-defined parameters. It includes three questions with different functions: Cos[x], x^3 - 5x + 1, and Cos[x] - x * Exp[x], providing example outputs for each case. The results show the estimated roots and errors after a specified number of iterations, along with plots of the functions.

Uploaded by

duaashish2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Secant Method In Different Form

Secant method: for given parameters


Ques. 1
x0 = Input["Enter first guess:"];
x1 = Input["Enter Second guess"];
Nmax = Input["Enter maximum number of iterations:"];
eps = Input["Enter the value of convergence parameter:"];
Print["x0=", x0];
Print["x1=", x1];
Print["Nmax", Nmax];
Print["epsilon=", eps];
f[x_] := Cos[x];
Print["f[x]:=", f[x]];
Fori = 1, 1 ⩽ Nmax, i ++,
x2 =
Nx1 - f[x] /. x -> x1 * x1 - x0  f[x] /. x -> x1 - f[x] /. x -> x0;
If[Abs[x1 - x2] < eps, Return[x2], x0 = x1; x1 = x2];
Print["In", i, "th Number of iterations the root is :", x2];
Print["estimated error is :", Abs[x1 - x0]];
Print["root is :", x1];
Print["estimated error is :", Abs[x2 - x1]];
Plot[f[x], {x, - 1, 3}]
x0=1

x1=2

Nmax20
1
epsilon=
1 000 000
f[x]:=Cos[x]

In1th Number of iterations the root is :1.5649

estimated error is :0.435096

In2th Number of iterations the root is :1.57098

estimated error is :0.0060742

In3th Number of iterations the root is :1.5708

estimated error is :0.000182249


Return[1.5708]

root is :1.5708

estimated error is :1.02185 × 10-9


2 NUMERICAL PRAC4c(SECANT METHOD).nb

1.0

0.5

-1 1 2 3

-0.5

-1.0

Ques. 2
x0 = Input["Enter first guess:"];
x1 = Input["Enter Second guess"];
Nmax = Input["Enter maximum number of iterations:"];
eps = Input["Enter the value of convergence parameter:"];
Print["x0=", x0];
Print["x1=", x1];
Print["Nmax", Nmax];
Print["epsilon=", eps];
f[x_] := x3 - 5 x + 1;
Print["f[x]:=", f[x]];
Fori = 1, 1 ⩽ Nmax, i ++,
x2 =
Nx1 - f[x] /. x -> x1 * x1 - x0  f[x] /. x -> x1 - f[x] /. x -> x0;
If[Abs[x1 - x2] < eps, Return[x2], x0 = x1; x1 = x2];
Print["In", i, "th Number of iterations the root is :", x2];
Print["estimated error is :", Abs[x1 - x0]];
Print["root is :", x1];
Print["estimated error is :", Abs[x2 - x1]];
Plot[f[x], {x, - 1, 3}]
NUMERICAL PRAC4c(SECANT METHOD).nb 3

x0=1

x1=2

Nmax20
1
epsilon=
1 000 000
f[x]:=1 - 5 x + x3

In1th Number of iterations the root is :2.5

estimated error is :0.5

In2th Number of iterations the root is :2.09756

estimated error is :0.402439

In3th Number of iterations the root is :2.12134

estimated error is :0.0237786

In4th Number of iterations the root is :2.12859

estimated error is :0.0072456

In5th Number of iterations the root is :2.12842

estimated error is :0.000166952


Return[2.12842]

root is :2.12842

estimated error is :8.77361 × 10-7

10

-1 1 2 3
4 NUMERICAL PRAC4c(SECANT METHOD).nb

Ques. 3
x0 = Input["Enter first guess:"];
x1 = Input["Enter Second guess"];
Nmax = Input["Enter maximum number of iterations:"];
eps = Input["Enter the value of convergence parameter:"];
Print["x0=", x0];
Print["x1=", x1];
Print["Nmax", Nmax];
Print["epsilon=", eps];
f[x_] := Cos[x] - x * Exp[x];
Print["f[x]:=", f[x]];
Fori = 1, 1 ⩽ Nmax, i ++,
x2 =
Nx1 - f[x] /. x -> x1 * x1 - x0  f[x] /. x -> x1 - f[x] /. x -> x0;
If[Abs[x1 - x2] < eps, Return[x2], x0 = x1; x1 = x2];
Print["In", i, "th Number of iterations the root is :", x2];
Print["estimated error is :", Abs[x1 - x0]];
Print["root is :", x1];
Print["estimated error is :", Abs[x2 - x1]];
Plot[f[x], {x, - 1, 3}]
x0=1

x1=2

Nmax20
1
epsilon=
1 000 000
f[x]:=-ⅇx x + Cos[x]

In1th Number of iterations the root is :0.832673

estimated error is :1.16733

In2th Number of iterations the root is :0.728779

estimated error is :0.103894

In3th Number of iterations the root is :0.562401

estimated error is :0.166377

In4th Number of iterations the root is :0.524782

estimated error is :0.0376189

In5th Number of iterations the root is :0.518014

estimated error is :0.00676874

In6th Number of iterations the root is :0.517759

estimated error is :0.0002547

In7th Number of iterations the root is :0.517757

estimated error is :1.50138 × 10-6


NUMERICAL PRAC4c(SECANT METHOD).nb 5

Return[0.517757]

root is :0.517757

estimated error is :3.22103 × 10-10

-1 1 2 3

-10

-20

-30

-40

-50

-60

You might also like