Sie sind auf Seite 1von 5

23

ASSIGNMENT 6
TO COMPUTE ROOT OF AN EQUATION BY USING SECANT METHOD

STATEMENT:

In numerical analysis, the secant method is a root-finding algorithm that uses a succession
of roots of secant lines to better approximate a root of a function f. The secant method can
be thought of as a finite difference approximation of Newton's method.
The secant method is defined by the recurrence relation:

𝑓(𝑥𝑛 )
𝑥𝑛+1 = 𝑥𝑛 − (𝑥𝑛 − 𝑥0 )
𝑓(𝑥𝑛 )−𝑓(𝑥0 )

As can be seen from the recurrence relation, the secant method requires two initial values,
x0 and x1, which should ideally be chosen to lie close to the root.

ALGORITHM:

INPUT: A function 3x−sinx−1


24

OUTPUT: An isolated root of the equation.

PROCESS:

Step 1: Create a function “float f (float x)”

Step 1.1: Return (3x−sinx−1)

[End of the function “float f (float x)”]

Step 2: Create the function “void main()”

[‘a’ and ‘b’ are two float type variables and initially a=0.0 and b=1.0]

Step 2.1: While (f(a)*f(b) > 0) then repeat Step 2.2 and Step 2.3

Step 2.2: Set aa−1.0

Step 2.3: Set bb+1.0

[End of Step 2.1 ‘While’ loop]

Step 2.4: Display the two integer values ‘a’ and ‘b’ in which the root lies i.e.

print “a,b”.

Step 2.5: Take the value of Error in a float type variable ‘err’.

Step 2.6: If (f(a) >0 ) then

Step 2.7: Set aa+b

Step 2.8: Set ba−b

Step 2.9: Set aa−b

[End of ‘If’]

Step 2.10: Do Step 2.11 to Step to Step 2.18

Step2.11: Set y  x

Step 2.12: Set xb−((f(b)/(f(b)−f(a)))×(b−a))

Step 2.13: If(f(x)<0) then


25

Step 2.14: Set ax

[End of ‘If’]

Step 2.15: Else

Step 2.16: Set bx

[End of ‘Else’]

Step 2.17: Print “a,b,f(a),f(b),x,f(x)”

Step 2.18: While(fabs(x-y)>err) then go to Step 2.10 repeatedly

Step 2.19: Display the value of the root correct up to 4 decimal places i.e. print “x”

[End of the function “void main()”]

Step 3: Stop

PROGRAM CODE:

#include <stdio.h>

#include <conio.h>

#include <math.h>

float f(float x)
{
return ((3*x)-sin(x)-1);
}

void main( )
{

float a= -1.0, b=0.0, x=0.0, y=0.0, err;

while(f(a)*f(b)>0)

{
a=a-1.0;

b=b+1.0;
26

}
printf("\n\tRoot lies between: %f and %f",a,b);

printf("\n\tEnter Err: ");

scanf("%f",&err);

printf("\n\n\t a\t\t b\t\t x");

printf("\n\t----------------------------------------\n");

if(f(a) > 0)
{
a=a+b;

b=a-b;

a=a-b;
}
do

y=x;

x=b-((f(b)/(f(b)-f(a)))*(b-a));
if(f(x) < 0)

a=x;

else

b=x;

printf("\n\t%f\t%f\t%f",a,b,x);

} while(fabs(x-y) > err);

printf("\n\t----------------------------------------\n");

printf("\n\tAnswer: %.4f (correct upto 4 decimal places)",x);

getch( );

OUTPUT:
27

DISCUSSION:

The secant method does not require that the root remain bracketed like the bisection
method does, and hence it does not always converge. The false position method (or regula
falsi) uses the same formula as the secant method. However, it does not apply the formula
on and , like the secant method, but on and on the last iterate such that
and have a different sign. This means that the false position method always
converges. The recurrence formula of the secant method can be derived from the formula
for Newton's method. If we compare Newton's method with the secant method, we see that
Newton's method converges faster.

Das könnte Ihnen auch gefallen