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

Secant 2

The document outlines a Mathematica code for finding the approximate root of a user-defined function using the Secant method. It prompts the user for the function, interval limits, tolerance level, and maximum iterations, then iteratively calculates and prints the approximate root. The example provided uses the function x^2 Cos[x] + Sin[x] with specified parameters, showing the results of several iterations.

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)
14 views1 page

Secant 2

The document outlines a Mathematica code for finding the approximate root of a user-defined function using the Secant method. It prompts the user for the function, interval limits, tolerance level, and maximum iterations, then iteratively calculates and prints the approximate root. The example provided uses the function x^2 Cos[x] + Sin[x] with specified parameters, showing the results of several iterations.

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[17]:= (*Priyanshi Bansal || 22BA064

Secant-2*)

f[x_] = Input["Enter a function:"];


x0 = Input["Enter the lower limit of the interval:"];
x1 = Input["Enter the upper limit of the interval:"];
ϵ = Input["Enter the prescribed tolerance level:"];
k = Input["Enter the maximum number of iterations:"];

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 - xi ]] < ϵ, Break[]]
]
f(x): x2 Cos[x] + Sin[x], x0 : 1 x1 : 3 and iterations: 10
Out[23]=
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


Out[24]=

$Aborted

You might also like