Program To Solve The Polynomial Equation by Using Bisection Method
Program To Solve The Polynomial Equation by Using Bisection Method
EN-NO:100413116010
ID-NO:210-ITM-109
#include<stdio.h>
#include<conio.h>
#include<math.h>
# define f(x) pow((double)3.0,(double)x)-4*x -9
void main()
{
float x0,x1,x2,E=0.003;
clrscr();
printf("Enter
scanf("%f",&x0);
printf("Enter
scanf("%f",&x1);
do
{
x2=(x0+x1)/2;
if(f(x0)*f(x2) < 0)
x1=x2;
else
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
x0=x2;
}
while( fabs((double) x0-x1) > E);
printf("\n Approximate root is= %.4f\n",x2);
getch();
}
PATEL SNEHAL .M
OUTPUT:
Enter the first interval x0=2
Enter the second interval x1=3
Approximate root is=2.0020
EN-NO:100413116010
ID-NO:210-ITM-109
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define F(x) 3*(x) - 1 - cos(x)
void main()
{
float x0,x1,x2,f1,f2,f0;
int count=0;
clrscr();
do
{
printf("\nEnter the value of x0: ");
scanf("%f",&x0);
}while(F(x0) > 0);
do
{
printf("\nEnter the value of x1: ");
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
scanf("%f",&x1);
}while(F(x1) < 0);
printf("\n______________________________________
____________________\n");
printf("\n
f1\t
f2");
x0\t
x1\t x2\t
f0\t
printf("\n______________________________________
____________________\n");
do
{
f0=F(x0);
f1=F(x1);
x2=x0-((f0*(x1-x0))/(f1-f0));
f2=F(x2);
printf("\n%f %f %f %f %f
%f",x0,x1,x2,f0,f1,f2);
if(f0*f2<0)
{
x1=x2;
}
else
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
{
x0 = x2;
}
}while(fabs(f2)>ESP);
printf("\n______________________________________
____________________\n");
printf("\n\nApp.root = %f",x2);
getch();
}
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
OUTPUT:
Function:3x-1-cosx
Enter the value of x0:-1
Enter the value of x1:1
______________________________________
x0
x1
x2
______________________________________
-1.000000
1.000000
0.513434
0.513434
1.000000
0.603320
0.603320
1.000000
0.606954
0.606954
1.000000
0.607096
______________________________________
f0
f1
f2
______________________________________
-4.540302
1.459698
-0.330761
-0.330761
1.459698
-0.013497
-0.013497
1.459698
-0.000527
-0.000527
1.459698
-0.000021
______________________________________
App.root=0.607096
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
scanf("%f",&n);
printf("enter the size of xn:");
scanf("%f",&xn);
printf("enter the size of x0:");
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
scanf("%f",&x0);
h=(xn-x0)/n;
sum=(f(x0)+f(xn))/2;
for (i=1;i<=(n-1);i++)
{
sum=sum +f(x0+(i*h));
}
I=h*sum;
printf("\n ans is =%f\n",I);
getch();
}
PATEL SNEHAL .M
OUTPUT:
enter
EN-NO:100413116010
ID-NO:210-ITM-109
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],sum=0,h,temp;
int i,n,j,k=0;
float fact(int);
clrscr();
printf("\nhow many record you will be
enter:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n\nenter the value of x%d: ",i);
scanf("%f",&x[i]);
printf("\n\nenter the value of f(x%d): ",i);
scanf("%f",&y[i]);
}
h=x[1]-x[0];
n=n-1;
sum = sum + y[0];
for(i=1;i<n;i++)
{
if(k==0)
{
sum = sum + 4 * y[i];
k=1;
}
else
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
{
sum = sum + 2 * y[i];
k=0;
}
}
sum = sum + y[i];
sum = sum * (h/3);
printf("\n\n I = %f
getch();
}
",sum);
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
OUTPUT:
how many record
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
I = 0.693250.
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
}
k=1;
}
else
{
sum = sum + 2 * y[i];
k=0;
l=0;
}
}
sum = sum + y[i];
sum = sum * (3*h/8);
printf("\n\n I = %f
getch();
}
",sum);
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
OUTPUT:
how many record
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
enter the value
I = 1.149975
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
}
}
else if(e<count)
{
while(e!=count)
{
i=i/10;
e++;
}
}
nc=e;
do
{
printf("\n\nChoose the arithmetic
operation to be
performed\n\n\t1.+\t2.\n\t3.*\t4./\n");
choice=getch();
sum=0;
switch(choice)
{
case '+':
add=i+j;
i2=floating(add);
e1=floating(add);
printf("\n\n\t %fE%d\n\t+
%fE%d",i,e,j,e);
printf("\n\t=
%fE%d",i2,e1+e);
break;
case '-':
sub=i-j;
i2=floating(sub);
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
e1=floating(sub);
printf("\n\n\t %fE%d\n\t%fE%d",i,e,j,e);
printf("\n\t=
%fE%d",i2,e1+e);
break;
case '*':
pro=i1*j1;
i2=floating(pro);
e1=floating(pro);
printf("\n\n\t %fE%d\n\t*
%fE%d",i,e,j,e);
printf("\n\t= %fE%d",i2,e1);
break;
case '/':
div=i1/j1;
i2=floating(div);
e1=floating(div);
printf("\n\n\t %fE%d\n\t/
%fE%d",i,e,j,e);
printf("\n\t= %fE%d",i2,e1);
break;
default:
printf("\n Please Enter
Correct choice");
break;
}
printf("\n\n Do you want to perform
another operation on the same numbers
(y/n):");
cho=getch();
}
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
while(cho=='y' || cho=='Y');
printf("\n\n\n\nDo you want to
continue with other numbers
(y/n): ");
ch=getch();
printf("\n\n");
}
while(ch=='Y'|| ch=='y');
getch();
}
float floating(float i)
{
int e=0;
if(i>=0.1 && i<1)
{
}
else if(i<=-0.1 && i>-1)
{
}
else if(i>=1)
{
while(i>1)
{
i=i/10;
e=e++;
}
}
else if(i>=0 && i<0.1)
{
while(i<0.1)
{
i=i*10;
e=e--;
}
}
PATEL SNEHAL .M
else if(i<=-1)
{
while(i<-1)
{
i=i/10;
e=e++;
}
}
else if(i<=0 && i>-0.1)
{
while(i>-0.1)
{
i=i*10;
e=e++;
}
}
sum++;
if(sum==1)
return i;
else
return e;
}
EN-NO:100413116010
ID-NO:210-ITM-109
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
OUTPUT :
->Enter 1st number:65430.00
The floating point representation is
0.654300E5
->Enter 2nd number:225.5
The floating point representation is
0.225500E3
Choose the arithmetic operation to be performed
1.+
2.3.*
4./
0.654300E5
+ 0.002255E5
= 0.656555E5
Do you want to perform another operation on the
same numbers (y/n):
Choose the arithmetic operation to be performed
1.+
2.3.*
4./
=
0.654300E5
0.002255E5
0.652045E5
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
0.654300E5
0.002255E5
0.290155E3
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
return 0;
}
x0=x1;
}
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
OUTPUT:
Enter xo, allowed error, maximum iterations
2.000001 10
Iteration no.
1 x=2.813170
Iteration no.
2 x=2.741109
Iteration no.
3 x=2.740646
Iteration no.
4 x=2.740646
After 4 iterations , root = 2.740646
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
x1+= h;
printf("when x= %3.1f y=%4.2f\n
",x1,y1);
}
}
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
OUTPUT:
Enter the value of x0, y0, h, x
01
.11
When
When
When
When
When
When
When
When
When
When
x=0.1
x=0.2
x=0.3
x=0.4
x=0.5
x=0.6
x=0.7
x=0.8
x=0.9
x=1.0
y=1.10
y=1.22
y=1.36
y=1.53
y=1.72
y=1.94
y=2.20
y=2.49
y=2.82
y=3.19
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
#include<stdio.h>
#include <math.h>
#include<conio.h>
#define F(x,y) 1 + (y)*(y)
void main()
{
double y0,x0,y1,n,h,f,k1,k2,k3,k4;
clrscr();
printf("\nEnter the value of x0: ");
scanf("%lf",&x0);
printf("\nEnter the value of y0: ");
scanf("%lf",&y0);
printf("\nEnter the value of h: ");
scanf("%lf",&h);
printf("\nEnter the value of last point: ");
scanf("%lf",&n);
for(; x0<n; x0=x0+h)
{
f=F(x0,y0);
k1 = h * f;
f = F(x0+h/2,y0+k1/2);
k2 = h * f;
f = F(x0+h/2,y0+k2/2);
k3 = h * f;
f = F(x0+h/2,y0+k2/2);
k4 = h * f;
y1 = y0 + ( k1 + 2*k2 + 2*k3 + k4)/6;
printf("\n\n k1 = %.4lf ",k1);
printf("\n\n k2 = %.4lf ",k2);
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
OUTPUT:
Enter
Enter
Enter
Enter
the
the
the
the
k1
k2
k3
k4
value
value
value
value
of
of
of
of
x0: 0
y0: 0
h: 0.2
last point: 0.4
= 0.2000
= 0.2020
= 0.2020
= 0.2020
y(0.2000) = 0.202
k1 = 0.2081
k2 = 0.2187
k3 = 0.2193
k4 = 0.2193
y(0.4000) = 0.419
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
PATEL SNEHAL .M
}
getch();
}
float f(float x,float y)
{
return (y-x);
}
EN-NO:100413116010
ID-NO:210-ITM-109
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
OUTPUT:
Solution by Runge kutta 2nd order Method
Enter initial Boundry condition x,y : 0 2
Enter Value of X at which Y is required : .1
Enter Interval ,h : .1
No. X m1 m2 Y
1 0.000000
2 0.100000
0.200000
0.210500
0.210000
0.221550
2.205000
2.421025
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float mat[4][4],temp,temp1,x,y,z;
int i,n,j;
clrscr();
printf("\nEnter size of matrix: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n\nenter the value of %d
eqvation",i+1);
for(j=0; j<n; j++)
{
printf("\nenter the value of
coefficient %d: ",j+1);
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
scanf("%f",&mat[i][j]);
}
printf("\nenter the value of constent:
");
scanf("%f",&mat[i][j]);
}
printf("\n Your Matrix \n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
printf(" %g ",mat[i][j]);
}
printf("\n\n");
}
temp=mat[1][0]/mat[0][0];
temp1=mat[2][0]/mat[0][0];
for(i=0,j=0;j<n+1;j++)
{
mat[i+1][j]=mat[i+1][j]-(mat[i][j]*temp);
mat[i+2][j]=mat[i+2][j]-(mat[i][j]*temp1);
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
temp=mat[2][1]/mat[1][1];
for(i=1,j=0;j<n+1;j++)
{
mat[i+1][j]=mat[i+1][j]-(mat[i][j]*temp);
}
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
printf(" %.3f ",mat[i][j]);
}
printf("\n\n");
}
z = mat[2][3]/mat[2][2];
y = (mat[1][3] - mat[1][2]*z)/mat[1][1];
x = (mat[0][3] - mat[0][2]*z mat[0][1]*y)/mat[0][0];
printf("\n\nx = %.3f",x);
printf("\n\ny = %.3f",y);
PATEL SNEHAL .M
printf("\n\nz = %.3f",z);
getch();
}
EN-NO:100413116010
ID-NO:210-ITM-109
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
OUTPUT:
Enter size of matrix: 3
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
Your Matrix
2
10
18
16
2.000
1.000
1.000
10.000
0.000
0.500
1.500
3.000
0.000
0.000
-2.000
-10.000
x = 7.000
y = -9.000
z = 5.000
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define X1(x2,x3) ((17 - 20*(x2) + 2*(x3))/20)
#define X2(x1,x3) ((-18 - 3*(x1) + (x3))/20)
#define X3(x1,x2) ((25 - 2*(x1) + 3*(x2))/20)
void main()
{
double x1=0,x2=0,x3=0,y1,y2,y3;
int i=0;
clrscr();
printf("\n
x1\t\t
x2\t\t
x3\n");
printf("\n______________________________________
____\n");
printf("\n%f\t%f\t%f",x1,x2,x3);
do
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
y1=X1(x2,x3);
y2=X2(y1,x3);
y3=X3(y1,y2);
if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP &&
fabs(y3-x3)<ESP )
{
printf("\n\nx1 = %.3lf",y1);
printf("\n\nx2 = %.3lf",y2);
printf("\n\nx3 = %.3lf",y3);
i = 1;
}
else
{
x1 = y1;
x2 = y2;
x3 = y3;
printf("\n%f\t%f\t%f",x1,x2,x3);
}
}while(i != 1);
getch();
}
PATEL SNEHAL .M
EN-NO:100413116010
ID-NO:210-ITM-109
OUTPUT:
x1
x2
x3
__________________________________________
0.000000
0.000000
0.000000
0.850000
-1.027500
1.010875
1.978588
-1.146244
0.880205
2.084265
-1.168629
0.866279
2.105257
-1.172475
0.863603
2.108835
-1.1731450
0.863145
2.109460
-1.173262
0.863065
2.109568
-1.173282
0.863051
x1 = 2.110
y1 = -1.173
z1 = 0.863