Title: Program For Bisection Method Algorithm
Title: Program For Bisection Method Algorithm
3.If (f1*f2) > 0, then display initial guesses are wrong and goto (11).
Otherwise continue.
6.Else, f = f(x)
8.Else, x1 = x and f2 = f.
9.Goto (5).
C CODE:
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
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;
}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*
4.Determine:
x = [x0*f(x1) – x1*f(x0)] / (f(x1) – f(x0))
8.Stop
CODE:
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
if(f(x2) == 0)
break;
else if(f(x2)< 0)
x0 = x2;
else if(f(x2)> 0)
x1 = 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*
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*
10.Stop
CODE:
#include <stdio.h>
#include <math.h>
#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