0% found this document useful (0 votes)
110 views6 pages

Title: Program For Bisection Method Algorithm

The document describes algorithms for three root finding methods: the bisection method, the regula falsi method, and the Newton-Raphson method. For each method, it provides the step-by-step algorithm and includes C code examples to implement the method. The algorithms take initial guesses and an error tolerance as input and iteratively find a root of the function by updating the guesses until the error is below the tolerance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views6 pages

Title: Program For Bisection Method Algorithm

The document describes algorithms for three root finding methods: the bisection method, the regula falsi method, and the Newton-Raphson method. For each method, it provides the step-by-step algorithm and includes C code examples to implement the method. The algorithms take initial guesses and an error tolerance as input and iteratively find a root of the function by updating the guesses until the error is below the tolerance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB 2:

TITLE: PROGRAM FOR BISECTION METHOD


ALGORITHM:
Start

1.Read x0, x1, e


*Here x1 and x2 are initial guesses
e is the absolute error i.e. the desired degree of accuracy*

2.Compute: f1 = f(x0) and f2 = f(x1)

3.If (f1*f2) > 0, then display initial guesses are wrong and goto (11).
Otherwise continue.

4.x2= (x0+ x1)/2

5.If ( [ (x0 – x1)/x ] < e ), then display x and goto (11).


* Here [ ] refers to the modulus sign. *

6.Else, f = f(x)

7.If ((f*f1) > 0), then x1 = x and f1 = f.

8.Else, x1 = x and f2 = f.

9.Goto (5).

C CODE:
#include <stdio.h>
#include <stdlib.h>
#include "math.h"

//#define f(x) (x*x*x - 5*x -7 )

float f(float x);

int main()
{
float x0,x1,x2;
int k = 0;
do{
printf("\nEnter the value for x0 and x1 ... ");
scanf("%f%f",&x0,&x1);
//printf("f(x0) = %f , f(x1) = %f, f(x0)*f(x1) = %f ",f(x0),f(x1),f(x1)*f(x0));
}while(f(x0)*f(x1) > 0);
do {
k++; // p = x2;
//x2 = (x0*f(x1)-x1*f(x0))/(f(x1) - f(x0));
x2 = (x1+x0)/2;
if(f(x2) == 0)
break;
else if(f(x2)< 0)
x0 = x2;
else if(f(x2)> 0)
x1 = x2;

//printf("\n\n x2 = %f f(x2) = %f\n\n",x2,fabs(f(x2)));

}while(fabs(f(x2))> .0001);
printf("\n root is %f and no. of iteration is %d",x2,k);

return 0;
}

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

INPUT:
Enter the values of x0 and x1 : 0 1
OUTPUT:
root is 1.324707 and no of iterations is 17
TITLE:PROGRAM FOR REGULA FALSI METHOD
ALGORITHM:
Start
1.Read values of x0, x1 and e
*Here x0 and x1 are the two initial guesses
e is the degree of accuracy or the absolute error i.e. the stopping criteria*

2.Computer function values f(x0) and f(x1)

3.Check whether the product of f(x0) and f(x1) is negative or not.


If it is positive take another initial guesses.
If it is negative then goto step 5.

4.Determine:
x = [x0*f(x1) – x1*f(x0)] / (f(x1) – f(x0))

5.Check whether the product of f(x1) and f(x) is negative or not.


If it is negative, then assign x0 = x;
If it is positive, assign x1 = x;

6.Check whether the value of f(x) is greater than 0.00001 or not.


If yes, goto step 5.
If no, goto step 8.
*Here the value 0.00001 is the desired degree of accuracy, and hence the
stopping criteria.*

7.Display the root as x.

8.Stop

CODE:

#include <stdio.h>
#include <stdlib.h>
#include "math.h"

//#define f(x) (x*x*x - 5*x -7 )

float f(float x);


int main()
{
float x0,x1,x2;
int k = 0;
do{
printf("\nEnter the value for x0 and x1 ... ");
scanf("%f%f",&x0,&x1);
//printf("f(x0) = %f , f(x1) = %f, f(x0)*f(x1) = %f ",f(x0),f(x1),f(x1)*f(x0));
}while(f(x0)*f(x1) > 0);
do {
k++; // p = x2;
//x2 = (x0*f(x1)-x1*f(x0))/(f(x1) - f(x0));
x2 = (x0*f(x1) - x1*f(x0))/ (f(x1) - f(x0));

if(f(x2) == 0)
break;
else if(f(x2)< 0)
x0 = x2;
else if(f(x2)> 0)
x1 = x2;

//printf("\n\n x2 = %f f(x2) = %f\n\n",x2,fabs(f(x2)));

}while(fabs(f(x2))> .0001);
printf("\n root is %f and no. of iteration is %d",x2,k);

return 0;
}

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

INPUT:
Enter the values of x0 and x1 0 2

OUTPUT:
Root is 1.324702 and no of iterations is 1
TITLE:PROGRAM FOR NEWTON RAPHSON METHOD
ALGORITHM:
Start

1.Read x, e, n, d
*x is the initial guess
e is the absolute error i.e the desired degree of accuracy
n is for operating loop
d is for checking slope*

2.Do for i =1 to n in step of 2

3.f = f(x)

4.f1 = f'(x)

5.If ( [f1] < d), then display too small slope and goto 11.
*[ ] is used as modulus sign*

6.x1 = x – f/f1

7.If ( [(x1 – x)/x1] < e ), the display the root as x1 and goto 11.
*[ ] is used as modulus sign*

8.x = x1 and end loop

9.Display method does not converge due to oscillation.

10.Stop

CODE:
#include <stdio.h>
#include <math.h>

#define f(x) x*x*x -x - 1


#define df(x) 3*x*x -1

#define e 0.0001

int main()
{
float x1,x0,f0,f1,df0;
printf("Enter the value of x0 ");
scanf("%f",&x0);
int i = 0;
do
{
f0 = f(x0);
df0 = df(x0);
x1 = x0 - f0/df0;
f1 = f(x1);
x0 = x1;
i++;
printf("\nNo of iterations %d",i);
printf("\nroot = %f",x1);
}while(fabs(f1)>e);

return 0;
}

INPUT:
0 and 2
OUTPUT:
Root is 1.324719 and no of iterations is 20

You might also like