CBNST Practical Programs
CBNST Practical Programs
Problem Statement 3: Find the approximate root for the equation x3-
3x+1 for interval(2,3) upto five decimal places using bisection Method.
#include<stdio.h>
#include<math.h>
double eqn(double x)
{
double f;
return((x*x*x)-(3*x)+1);
}
int main()
{
float x1= 1 ,x2=2;
double z,x3;
float range = 0.000001;
int i = 1;
printf("***BISECTION METHOD *** \n");
do{
printf("iteration%d",i);
printf(" x1 =%f x2 = %f",x1,x2);
x3 = (x1+x2)/2;
printf(" x3=%f",x3);
z = eqn(x3); printf("\
n");
if(z>0)
x2 =x3;
else
x1=x3;
i++;
}
while(fabs(x1-x2)>=range);
printf("\n Final root after %d iterations is %f",i-1,x3);
return 0 ;
NAME- Kartik Singh ROLL.NO.-31 BCA SecB
} Output :
***BISECTION METHOD ***
iteration1 x1 =1.000000 x2 = 2.000000 x3=1.500000
iteration2 x1 =1.500000 x2 = 2.000000 x3=1.750000
iteration3 x1 =1.500000 x2 = 1.750000 x3=1.625000
iteration4 x1 =1.500000 x2 = 1.625000 x3=1.562500
iteration5 x1 =1.500000 x2 = 1.562500 x3=1.531250
iteration6 x1 =1.531250 x2 = 1.562500 x3=1.546875
iteration7 x1 =1.531250 x2 = 1.546875 x3=1.539063
iteration8 x1 =1.531250 x2 = 1.539063 x3=1.535156
iteration9 x1 =1.531250 x2 = 1.535156 x3=1.533203
iteration10 x1 =1.531250 x2 = 1.533203 x3=1.532227
iteration11 x1 =1.531250 x2 = 1.532227 x3=1.531738
iteration12 x1 =1.531738 x2 = 1.532227 x3=1.531982
iteration13 x1 =1.531982 x2 = 1.532227 x3=1.532104
iteration14 x1 =1.531982 x2 = 1.532104 x3=1.532043
iteration15 x1 =1.532043 x2 = 1.532104 x3=1.532074
iteration16 x1 =1.532074 x2 = 1.532104 x3=1.532089
iteration17 x1 =1.532074 x2 = 1.532089 x3=1.532082
iteration18 x1 =1.532082 x2 = 1.532089 x3=1.532085
iteration19 x1 =1.532085 x2 = 1.532089 x3=1.532087
iteration20 x1 =1.532087 x2 = 1.532089 x3=1.532088
Final root after 20 iterations is 1.532088
NAME- Kartik Singh ROLL.NO.-31 BCA SecB
Problem Statement 4: Find the approximate root for the equation x3-
3x+1 for interval(1,2) upto three decimal places using Regula falsi
Method.
#include<stdio.h>
#include<math.h>
float eqn(float x)
{
return ((x*x*x)-(3*x)+1);
}
float formula(float x1,float x2,float fx1,float fx2)
{
return (((x1*fx2)-(x2*fx1))/(fx2-fx1));
}
int main()
{
printf("****** Regula - Falsi Method *****");
float x1=1,x2=2,x3,fx1,fx2,fx3,range=0.0005;
int i=1;
do
{
fx1=eqn(x1);
fx2=eqn(x2);
x3=formula(x1,x2,fx1,fx2);
fx3=eqn(x3);
printf("\n Iteration - %d x1 = %f x2 = %f x3 = %f ",i,x1,x2,x3);
printf("\nValue of f(x1) = %f Value of f(x2) = %f ",fx1,fx2);
NAME- Kartik Singh ROLL.NO.-31 BCA SecB
if(fx3>0)
{
x2=x3;
}
else{
x1=x3;
} i+
+;
}while(fabs(fx3)>range);
printf("\n Final approximate root is : %f",x3);
return 0;
}
NAME- Kartik Singh ROLL.NO.-31 BCA SecB
Problem Statement 5: Find the approximate root for the equation x3-
3x+1 for interval(1,2) upto three decimal places using Secant Method.
#include<stdio.h>
#include<math.h>
float eqn(float x)
{
return ((x*x*x)-(3*x)+1);
}
float formula(float x1,float x2,float fx1,float fx2)
{
return (((x1*fx2)-(x2*fx1))/(fx2-fx1));
}
int main()
{
printf("****** Secant Method *****");
float x1=1,x2=2,x3,fx1,fx2,fx3,range=0.0005;
int i=1;
do
{
fx1=eqn(x1);
fx2=eqn(x2);
x3=formula(x1,x2,fx1,fx2);
printf("\n Iteration - %d x1 = %f x2 = %f x3 = %f ",i,x1,x2,x3);
printf("\n Value of f(x1) = %f Value of f(x2) = %f ",fx1,fx2);
x1=x2;
NAME- Kartik Singh ROLL.NO.-31 BCA SecB
x2=x3;
i++;
}while(fabs(x1-x2)>range);
printf("\n Final approximate root is : %f",x3);
return 0;
}
NAME- Kartik Singh ROLL.NO.-31 BCA SecB
Problem Statement 6: Find the approximate root for the equation x3-
3x+1 upto three decimal places using Newton Raphson Method.
#include<stdio.h>
#include<math.h>
float eqn(float x)
{
return ((x*x*x)-(3*x)+1);
}
float geqn(float x)
{
return ((3*x*x)-3);
}
int main()
{
printf("****** Newton Raphson Method *****");
float x1=2,x2,fx1,fx2;
int i=1;
do{
fx1=eqn(x1);
x2=x1-fx1/geqn(x1);
printf("\nIteration - %d x1 - %f x2 -%f ",i,x1,x2);
printf("\nvalue of f(x1)= %f f(x2)= %f ",fx1,fx2);
x1=x2;
fx2=eqn(x2);
NAME- Kartik Singh ROLL.NO.-31 BCA SecB
i++;
}while(fabs(fx2)>0.0005);
printf("\nThe final approximate root is : %f \n",x2);
return 0;
}
NAME- Kartik Singh ROLL.NO.-31 BCA SecB