0% found this document useful (0 votes)
14 views3 pages

Secant Method

The document describes the secant method for finding the root of a function. It provides code to implement the secant method to find the roots of three example functions: f(x) = x^3 - 5x + 1, f(x) = e^-x - x, and f(x) = cos(x) - x. For each function, it plots the function, initializes values for x0 and x1, iteratively calculates x2 using the secant method formula, and prints the results until convergence within a specified tolerance is reached, outputting the root.

Uploaded by

Zokova Gray
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)
14 views3 pages

Secant Method

The document describes the secant method for finding the root of a function. It provides code to implement the secant method to find the roots of three example functions: f(x) = x^3 - 5x + 1, f(x) = e^-x - x, and f(x) = cos(x) - x. For each function, it plots the function, initializes values for x0 and x1, iteratively calculates x2 using the secant method formula, and prints the results until convergence within a specified tolerance is reached, outputting the root.

Uploaded by

Zokova Gray
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/ 3

Secant Method of Finding Root

f[x_] := x3 - 5 x + 1;
Plot[f[x], {x, 0, 11}]
FindRoot[f[x] ⩵ 0, {x, 0.}]
tol = 10-3;
n = 10.0
x0 = 0;
x1 = 1;
Iff[x0] * f[x1] > 0,

Print"There is no root in this interval", Exit[];

f[x0] * (x1 - x0)


Dox2 = x0 - // N;
(f[x1] - f[x0])
Print[PaddedForm[i, 2], PaddedForm[x0, {10, 6}],
PaddedForm[x1, {10, 6}], PaddedForm[x2, {10, 6}],
PaddedForm[f[x2], {10, 6}]];
IfAbs[x2 - x1] < tol, Print"The root is:", x2, Exit[],

{x0 = x1, x1 = x2}, {i, 1, n};

Print"Maximum iteration failed"

1200

1000

800

600

400

200

2 4 6 8 10

{x → 0.20164}

10.

1 0.000000 1.000000 0.250000 -0.234375

2 1.000000 0.250000 0.186441 0.074277

3 0.250000 0.186441 0.201736 -0.000471

4 0.186441 0.201736 0.201640 -8.642293 × 10-7

The root is:0.20164

Quit[];
2

f[x_] := Exp[- x] - x;
Plot[f[x], {x, 0, 11}]
FindRoot[f[x] ⩵ 0, {x, 0.}]
tol = 10-3;
n = 10.0
x0 = 0;
x1 = 1;
Iff[x0] * f[x1] > 0,

Print"There is no root in this interval", Exit[];

f[x0] * (x1 - x0)


Dox2 = x0 - // N;
(f[x1] - f[x0])
Print[PaddedForm[i, 2], PaddedForm[x0, {10, 6}],
PaddedForm[x1, {10, 6}], PaddedForm[x2, {10, 6}],
PaddedForm[f[x2], {10, 6}]];
IfAbs[x2 - x1] < tol, Print"The root is:", x2, Exit[],

{x0 = x1, x1 = x2}, {i, 1, n};

Print"Maximum iteration failed"

2 4 6 8 10

-2

-4

-6

-8

-10

{x → 0.567143}

10.

1 0.000000 1.000000 0.612700 -0.070814

2 1.000000 0.612700 0.563838 0.005182

3 0.612700 0.563838 0.567170 -0.000042

4 0.563838 0.567170 0.567143 -2.538017 × 10-8

The root is:0.567143

Quit[];
3

f[x_] := Cos[x] - x;
Plot[f[x], {x, 0, 11}]
FindRoot[f[x] ⩵ 0, {x, 0.}]
tol = 10-3;
n = 10.0
x0 = 0;
x1 = 1;
Iff[x0] * f[x1] > 0,

Print"There is no root in this interval", Exit[];

f[x0] * (x1 - x0)


Dox2 = x0 - // N;
(f[x1] - f[x0])
Print[PaddedForm[i, 2], PaddedForm[x0, {10, 6}],
PaddedForm[x1, {10, 6}], PaddedForm[x2, {10, 6}],
PaddedForm[f[x2], {10, 6}]];
IfAbs[x2 - x1] < tol, Print"The root is:", x2, Exit[],

{x0 = x1, x1 = x2}, {i, 1, n};

Print"Maximum iteration failed"

2 4 6 8 10

-2

-4

-6

-8

-10

{x → 0.739085}

10.

1 0.000000 1.000000 0.685073 0.089299

2 1.000000 0.685073 0.736299 0.004660

3 0.685073 0.736299 0.739119 -0.000057

4 0.736299 0.739119 0.739085 3.529262 × 10-8

The root is:0.739085

You might also like