0% found this document useful (0 votes)
10 views2 pages

Root Finding with Function Plotting

numerical method secant

Uploaded by

hetankumar9
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)
10 views2 pages

Root Finding with Function Plotting

numerical method secant

Uploaded by

hetankumar9
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

In[49]:= f[x_] = Input"Enter the function : ";

Print"Graph of the function in the interval [-4,4]";

Plotf[x], x, - 4, 4, PlotStyle → Thick

x0 = Input"Enter first initial approximation : ";


x1 = Input"Enter second initial approximation : ";

Iff[x0] * f[x1] > 0,


Print"The given interval does not contain the root";
Exit[];
;

ε = Input"Enter the prescribed tolerance error : ";


k = Input"Enter the maximum number of iterations : ";

Print"Graph of the function in the interval [", x0, ", ", x1, "]";
Plotf[x], x, x0, x1, PlotStyle → Thick

Fori = 1, i ≤ k, i ++,
x2 = x0 * f[x1] - x1 * f[x0]  f[x1] - f[x0];
Print"Iteration Number is ", i, " root = ", Nx2, 9;

IfAbs[f[x2]] < ε, Break[];

x0 = x1;
x1 = x2;

Ifi ⩵ k + 1 && Abs[f[x2]] > ε,

Print"Maximum no. of iterations has been exceeded",


Print"Desired accuracy has been achieved"
;

Print"The root of the equation ", f[x],


" = 0 after ", i - 1, " iterations is : ", Nx2, 9;
Print"The value of the function is : ", Nf[x2], 9;

Graph of the function in the interval [-4,4]


2

Out[51]=

60

40

20

-4 -2 2 4

-20

-40

Graph of the function in the interval [1, 1.5]


Out[58]=

1.0

0.5

1.1 1.2 1.3 1.4 1.5

-0.5

Iteration Number is 1 root = 1.3233


Iteration Number is 2 root = 1.368
Iteration Number is 3 root = 1.37372
Iteration Number is 4 root = 1.37347
Desired accuracy has been achieved
The root of the equation 2x - x3 = 0 after 3 iterations is : 1.37347
The value of the function is : 4.93875 × 10-6

You might also like