0% found this document useful (0 votes)
47 views7 pages

CBNST (Lab)

The document discusses implementing different numerical methods like bisection method, regular falsi method to find the root of equations. It provides the problem statements, objectives, formulas used and sample C code with inputs and outputs for each method.

Uploaded by

smdn284
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views7 pages

CBNST (Lab)

The document discusses implementing different numerical methods like bisection method, regular falsi method to find the root of equations. It provides the problem statements, objectives, formulas used and sample C code with inputs and outputs for each method.

Uploaded by

smdn284
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name: Aryan dhanai Course: BCA Roll No.

: 33(B)

Problem Statement : Write a C program to calculate the root of the equation


ax^2+bx+c by calculating discriminant D.
Objective : To implement the mathematical functions.
Formula Used : D = b^2 -4ac
Case 1: D>0, Equal and Unquie
R1=-b+sqrt(b*b-4ac
R2=-b-sqrt(b*b-4ac)
Case 2: D==0, Roots are equal R1=R2=-b/2a.
Case 3: D<0, Roots are complex.

Source Code:

#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,D,R1,R2;
printf("Enter the value of a,b and c");
scanf("%d%d%d",&a,&b,&c);
D=b*b-4*a*c;
if(D==0)
{
R1=-b/(2*a);
printf("Roots are equal R1=R2= %d",R1);
}
else if(D>0)
{
R1=-b+sqrt(b*b-4*a*c);
R2=-b-sqrt(b*b-4*a*c);
printf("Roots are equal and unique \nR1= %d \nR2= %d",R1,R2);
}
else
printf("Roots are complex");
}

Output:

Enter the value of a,b and c


1
4
2
Roots are equal and unique
R1= -1
R2= -6

--------------------------------------------------------------
Name: Aryan dhanai Course: BCA Roll No.: 33(B)
Problem Statement : Write a C programe to find the Absolute error,Relative error and
Percentage error.
Objective : To implement the mathematical functions.
Formula Used : Absolute Error(Ea) = |true value-absolute value|
Relative Error(Er) = Ea/true value
Percentage Error(Ep) = Er*100

Source Code:

#include<stdio.h>
#include<math.h>
int main()
{
float tv,av,Ea,Er,Ep;
printf("Enter the True and absolute value");
scanf("%f%f",&tv,&av);
Ea=fabs(tv-av);
printf(" Absolute Error = %f",Ea);
Er=Ea/tv;
printf("\n Relative Error = %f",Er);
Ep=Er*100;
printf("\n Percentage Error = %f",Ep);
}

Output:

Enter the True and absolute value


3.14
3.14159265359
Absolute Error = 0.001593
Relative Error = 0.000507
Percentage Error = 0.050721

--------------------------------------------------------------

Name: Aryan dhanai Course: BCA Roll No.: 33(B)


Problem Statement : Write a C programe to find the root of the equation using
Bisection method.
Objective : To implement the Bisection Method.
Formula Used : xi=(ai+bi)/2 , |ai-bi|<=0.001

Source Code:

#include<Math.h>
#include<stdio.h>
float fun(float x)
{
return(x*x*x-4*x-9);
}
int main()
{
int i=0,pos=0;
float ai,bi,xi,fxi;
while(1)
{
if(fun(i)==0)
{
printf("Exact root found at %d",i);
break;
}
if(fun(i)*fun(i+1)<0)
{
pos++;
break;
}
i++;
}
if(pos)
{
ai=i;
bi=i+1;
printf("Range is a=%0.f and b=%0.f",ai,bi);
}
else
{
printf("Range not found");
}
if(pos)
{
printf("\n Calculation Table\n");
xi=(float)(ai+bi)/2;
fxi=fun(xi);
printf("\n i\t ai\t bi\t xi\t fxi\n");
for(i=1;;i++)
{
printf("%d\t %.3f\t %.3f\t %.3f\t %.3f\n",i,ai,bi,xi,fun(xi));
if((float)fabs(xi-ai)<=0.0001f || (float)fabs(xi-bi)<=0.0001f)
break;
if(fxi<0)
ai=xi;
else
bi=xi;
xi=(float)(ai+bi)/2;
fxi=fun(xi);
}
}
printf("Root of the equation is =%.3f",ai);
}

Output:

Range is a=2 and b=3


Calculation Table

i ai bi xi fxi
1 2.000 3.000 2.500 -3.375
2 2.500 3.000 2.750 0.797
3 2.500 2.750 2.625 -1.412
4 2.625 2.750 2.688 -0.339
5 2.688 2.750 2.719 0.221
6 2.688 2.719 2.703 -0.061
7 2.703 2.719 2.711 0.079
8 2.703 2.711 2.707 0.009
9 2.703 2.707 2.705 -0.026
10 2.705 2.707 2.706 -0.009
11 2.706 2.707 2.707 0.000
12 2.706 2.707 2.706 -0.004
13 2.706 2.707 2.706 -0.002
14 2.706 2.707 2.706 -0.001
Root of the equation is =2.706

--------------------------------------------------------------

Name:Aryan dhanai Course: BCA Roll No.: 33(B)

Problem Statement : Write a C programe to find the root of the equation using
Regular Falsi method.
Objective : To implement the Regular Falsi Method.
Formula Used : xi=(ai+bi)/2 , |ai-bi|<=0.001

Source Code
#include<stdio.h>
#include<math.h>
float func(float x)
{
return(x*x*x-x-18);
}
int main()
{
int i=0,pos=0;
float x0,x1,x2,fx0,fx1,fx2;
while(1)
{
if(func(i)==0)
{
printf("\nExact Root: %d",i);
break;
}
if(func(i)*func(i+1)<0)
{
pos++;
break;
}
i++;
}
if(pos)
{
x0=i;
x1=i+1;
}
else
{
printf("\nSorry Equation do not have Positive Root\n");
}
if(pos)
{
printf("\nCalculation Table\n");
printf("Root lies between:%.0f,%.0f",x0,x1);
fx1=func(x1);
fx0=func(x0);
x2=(float)(x1-(fx1*(x1-x0)/(fx1-fx0)));
fx2=func(x2);
printf("\n i\t\tx0\t\tx1\t\tx2\t\tfx2");
for(i=1;;i++)
{
printf("\n %d\t\t%.4f\t\t%.4f\t\t%.4f\t\t%.4f",i,x0,x1,x2,fx2);
if((float)fabs(x0-x2)<0.0001f||(float)fabs(x2-x1)<0.0001f)
break;
if(fx2<0)
{
x0=x2;
fx0=fx2;
}
else
{
x1=x2;
fx1=fx2;
}
x2=(float)(x1-(fx1*(x1-x0)/(fx1-fx0)));
fx2=func(x2);
}
}
printf("\nRoot of the Equation is %.4f",x2);
}

Output:

Calculation Table
Root lies between:2,3
i x0 x1 x2 fx2
1 2.0000 3.0000 2.6667 -1.7037
2 2.6667 3.0000 2.7404 -0.1609
3 2.7404 3.0000 2.7472 -0.0145
4 2.7472 3.0000 2.7478 -0.0013
5 2.7478 3.0000 2.7478 -0.0001
Root of the Equation is 2.7478

----------------------------------------------------------------

Name: Aryan dhanai Course: BCA Roll No.: 33(B)

Problem Statement : Write a C programe to find the root of the equation using
Regular Falsi method.
Objective : To implement the Regular Falsi Method.
Formula Used : xi=(ai+bi)/2 , |ai-bi|<=0.001

Source Code:

#include <stdio.h>
float fun(float x)
{
return(x*x*x-18);
}
int main()
{
int i=0,pos=0;
float ai,bi,xi,fxi;
while(1)
{
if(fun(i)==0)
{
printf("Exact root found at %d",i);
break;
}
if(fun(i)*fun(i+1)<0)
{
pos++;
break;
}
i++;
}
if(pos)
{
ai=i;
bi=i+1;
printf("Range is a=%0.f and b=%0.f",ai,bi);
}
else
{
printf("Range not found");
}
}

Output:

Range is a=2 and b=3

You might also like