0% found this document useful (0 votes)
2 views1 page

Secant 1

The document describes a Secant Method implementation for finding roots of a function, where the user inputs the function, two initial values, the number of iterations, and the tolerance level. It provides an example with the function f(x) = x^2 Cos[x] + Sin[x] and shows the output of approximate roots over several iterations. The results indicate the convergence of the method towards a root within the specified tolerance.

Uploaded by

nikkinikunj2
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)
2 views1 page

Secant 1

The document describes a Secant Method implementation for finding roots of a function, where the user inputs the function, two initial values, the number of iterations, and the tolerance level. It provides an example with the function f(x) = x^2 Cos[x] + Sin[x] and shows the output of approximate roots over several iterations. The results indicate the convergence of the method towards a root within the specified tolerance.

Uploaded by

nikkinikunj2
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/ 1

In[9]:= (*Priyanshi Bansal || 22BA064

Secant Method-1*)

f[x_] = Input["Enter the function"]


x0 = Input["Enter the first value"]
x1 = Input["Enter the second value"]
k = Input["Enter the no. of iterations"]
ϵ = Input["Enter the tolerance level"]
Print["f(x): ", f[x], ", x0 : ", x0 , " x1 : ", x1 , " and iterations: ", k];
Plot[f[x], {x, x0 , x1 }]

For [i = 1, i ≤ k, i ++,
xi+1 = (xi-1 f[xi ] - xi f[xi-1 ]) / (f[xi ] - f[xi-1 ]);
Print["Iteration number: ", i, " Approximate Root= ", N[x i+1 ]];
If[Abs[f[xi+1 ]] < ϵ, Break[]]
]
Out[9]= x2 Cos[x] + Sin[x]
Out[10]=

1
Out[11]=

3
Out[12]=

10
Out[13]=

0.001

f(x): x2 Cos[x] + Sin[x], x0 : 1 x1 : 3 and iterations: 10


Out[15]=
2

1.5 2.0 2.5 3.0

-2

-4

-6

-8

Iteration number: 1 Approximate Root= 1.27225

Iteration number: 2 Approximate Root= 1.51478

Iteration number: 3 Approximate Root= 2.411

Iteration number: 4 Approximate Root= 1.72567

Iteration number: 5 Approximate Root= 1.81213

Iteration number: 6 Approximate Root= 1.85914

You might also like