0% found this document useful (0 votes)
163 views11 pages

CBNST Practical Programs

The document contains 6 problem statements that involve finding the approximate root of the equation x3 - 3x + 1 using different numerical methods. Each problem statement includes the code to implement the method (bisection, regula falsi, secant, or Newton-Raphson) and the output showing the iterative process of finding the root to within the specified precision.

Uploaded by

sidharth joshi
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)
163 views11 pages

CBNST Practical Programs

The document contains 6 problem statements that involve finding the approximate root of the equation x3 - 3x + 1 using different numerical methods. Each problem statement includes the code to implement the method (bisection, regula falsi, secant, or Newton-Raphson) and the output showing the iterative process of finding the root to within the specified precision.

Uploaded by

sidharth joshi
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/ 11

NAME- Kartik Singh ROLL.NO.

-31 BCA SecB

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

Output :(Regula – falsi Method)


****** Regula - Falsi Method *****
Iteration - 1 x1 = 1.000000 x2 = 2.000000 x3 = 1.250000
Value of f(x1) = -1.000000 Value of f(x2) = 3.000000
Iteration - 2 x1 = 1.250000 x2 = 2.000000 x3 = 1.407407
Value of f(x1) = -0.796875 Value of f(x2) = 3.000000
Iteration - 3 x1 = 1.407407 x2 = 2.000000 x3 = 1.482367
Value of f(x1) = -0.434436 Value of f(x2) = 3.000000
Iteration - 4 x1 = 1.482367 x2 = 2.000000 x3 = 1.513157
Value of f(x1) = -0.189730 Value of f(x2) = 3.000000
Iteration - 5 x1 = 1.513157 x2 = 2.000000 x3 = 1.525012
Value of f(x1) = -0.074882 Value of f(x2) = 3.000000
Iteration - 6 x1 = 1.525012 x2 = 2.000000 x3 = 1.529463
Value of f(x1) = -0.028372 Value of f(x2) = 3.000000
Iteration - 7 x1 = 1.529463 x2 = 2.000000 x3 = 1.531117
Value of f(x1) = -0.010584 Value of f(x2) = 3.000000
Iteration - 8 x1 = 1.531117 x2 = 2.000000 x3 = 1.531729
Value of f(x1) = -0.003925 Value of f(x2) = 3.000000
Iteration - 9 x1 = 1.531729 x2 = 2.000000 x3 = 1.531956
Value of f(x1) = -0.001453 Value of f(x2) = 3.000000
Iteration - 10 x1 = 1.531956 x2 = 2.000000 x3 = 1.532040
Value of f(x1) = -0.000537 Value of f(x2) = 3.000000
Final approximate root is : 1.532040
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

Output : (Secant Method)

****** Secant Method *****


Iteration - 1 x1 = 1.000000 x2 = 2.000000 x3 = 1.250000
Value of f(x1) = -1.000000 Value of f(x2) = 3.000000
Iteration - 2 x1 = 2.000000 x2 = 1.250000 x3 = 1.407407
Value of f(x1) = 3.000000 Value of f(x2) = -0.796875
Iteration - 3 x1 = 1.250000 x2 = 1.407407 x3 = 1.596083
Value of f(x1) = -0.796875 Value of f(x2) = -0.434436
Iteration - 4 x1 = 1.407407 x2 = 1.596083 x3 = 1.522501
Value of f(x1) = -0.434436 Value of f(x2) = 0.277742
Iteration - 5 x1 = 1.596083 x2 = 1.522501 x3 = 1.531425
Value of f(x1) = 0.277742 Value of f(x2) = -0.038330
Iteration - 6 x1 = 1.522501 x2 = 1.531425 x3 = 1.532096
Value of f(x1) = -0.038330 Value of f(x2) = -0.002683
Iteration - 7 x1 = 1.531425 x2 = 1.532096 x3 = 1.532089
Value of f(x1) = -0.002683 Value of f(x2) = 0.000029
Final approximate root is : 1.532089
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

Output : (Newton Raphson Method)

****** Newton Raphson Method *****


Iteration - 1 x1 - 2.000000 x2 -1.666667
value of f(x1)= 3.000000 f(x2)= 0.000000
Iteration - 2 x1 - 1.666667 x2 -1.548611
value of f(x1)= 0.629629 f(x2)= 0.629629
Iteration - 3 x1 - 1.548611 x2 -1.532390
value of f(x1)= 0.068040 f(x2)= 0.068040
Iteration - 4 x1 - 1.532390 x2 -1.532089
value of f(x1)= 0.001218 f(x2)= 0.001218
The final approximate root is : 1.532089

You might also like