Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 44

PATEL SNEHAL .

EN-NO:100413116010
ID-NO:210-ITM-109

Program To Solve The Polynomial Equation By Using


BISECTION METHOD

#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

the first interval x0=");

scanf("%f",&x0);
printf("Enter

the second interval x1=");

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

Program To Solve The Polynomial Equation By Using


FALSE POSITION METHOD

#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

Program To Solve The Equaition By Using


TRAPEZOIDAL RULE

//Trapezoidal Rule for given Function//


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return (1/x);
}
void main()
{
float h,sum,I, i,n,x0,xn;
clrscr();
printf("enter

the number of intervals:");

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

the number of intervals:6

enter the size of xn:2


enter the size of x0:1
ans is =0.694877

EN-NO:100413116010
ID-NO:210-ITM-109

PATEL SNEHAL .M

EN-NO:100413116010
ID-NO:210-ITM-109

Program To Solve The Equaition By Using


SIMPSONS 1/3S RULE

#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.

you will be enter: 5


of x0: 0
of f(x0): 1
of x1: 0.25
of f(x1): 0.8
of x2: 0.5
of f(x2): 0.6667
of x3: 0.75
of f(x3): 0.5714
of x4: 1
of f(x4): 0.5

PATEL SNEHAL .M

EN-NO:100413116010
ID-NO:210-ITM-109

Program To Solve The Equaition By Using


SIMPSONS 3/8 RULE
#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,l=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 || l==0)
{
sum = sum + 3 * y[i];
if(k==1)
{
l=1;

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

you will be enter: 10


of x0: 0.1
of f(x0): 1.001
of x1: 0.2
of f(x1): 1.008
of x2: 0.3
of f(x2): 1.027
of x3: 0.4
of f(x3): 1.064
of x4: 0.5
of f(x4): 1.125
of x5: 0.6
of f(x5): 1.216
of x6: 0.7
of f(x6): 1.3434
of x7: 0.8
of f(x7): 1.512
of x8: 0.9
of f(x8): 1.729
of x9: 1.0
of f(x9): 2

PATEL SNEHAL .M

EN-NO:100413116010
ID-NO:210-ITM-109

Program To Solve The FLOATING POINT PROBLEMS


#include<stdio.h>
#include<conio.h>
float floating(float);
int sum=0,nc=0;
void main()
{
char choice,ch,cho;
do
{
int e=0,count=0,e1;
float i,j,i1,i2,j1,add,sub,pro,div;
clrscr();
printf("\n->Enter 1st number:");
scanf("%f",&i1);
i=floating(i1);
e=floating(i1);
printf("\n\tThe floating point
representation is %fE%d\n",i,e);
printf("\n->Enter 2nd number:");
scanf("%f",&j1);
sum=0;
j=floating(j1);
count=floating(j1);
printf("\n\tThe floating point
representation is %fE%d\n",j,count);
if(e>count)
{
while(e!=count)
{
j=j/10;
count++;

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

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.147545E8
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.290155E3

Do you want to perform another operation on the


same numbers (y/n):
Do you want to continue with other numbers
(y/n):

PATEL SNEHAL .M

EN-NO:100413116010
ID-NO:210-ITM-109

Program To Solve The Polynomial Equation By Using


NEWTON RAPHSON METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
Return x*log10(x)-1.2;
}
float df(float x)
{
Return log10(x) + 0.43429;
}
void main()
{
Int itr,maxitr;
float x0,x1,h,aerr;
clrscr();
printf("enter x0,allowed error," maximum
iterations\n);
scanf("%f%f%d",&x0,&aerr,&maxitr);
for(itr=1;itr<=maxitr;itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf("iteration no.
%3d,x=%9.6f\n,itr,x1);
if(fabs(h) < aerr)
{
Printf(After %3d iterations, root
is %8.6f\n,itr,x1);

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

Program To Solve The Equaition By Using


EULERS METHODS
# include <stdio.h>
# include <conio.h>
Float df(float x,float y)
{
return (x+y);
}
void main()
{
float x0,y0,h,x,x1,y1;
clrscr();
printf("enter the value of x0,y0,h,x\n");
scanf("%f%f%f %f ",&x0,&y0,&h,&x);
x1=x0;
y1=y0;
while(1)
{
if( x1 > x)
return;
y1+= h*df(x1,y1);

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

Program To Solve The Equaition By Using


RUNGE-KUTTA 4ND ORDER METHODS

#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

printf("\n\n k3 = %.4lf ",k3);


printf("\n\n k4 = %.4lf ",k4);
printf("\n\n y(%.4lf) = %.3lf
",x0+h,y1);
y0=y1;
}
getch();
}

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

Program To Solve The Equaition By Using


RUNGE-KUTTA 2ND ORDER METHODS
# include <stdio.h>
# include <conio.h>
void main()
{
int c=0;
float x,y0,xp,h,i,y1,m1,m2;
float f(float,float);
clrscr();
printf("Solution by Runge kutta 2nd order
Method\n");
printf("Enter initial Boundry condition x,y
: ");
scanf("%f%f",&x,&y0);
printf("Enter Value of X at which Y is
required : ");
scanf("%f",&xp);
printf("Enter Interval ,h : ");
scanf("%f",&h);
printf(" No. \t X\t m1 \t m2\t Y\n");
for(i=x;i<=xp;i=i+h)
{
c++;
m1=h*f(i,y0);
m2=h*f(i+h,y0+m1);
y1=y0+(.5*(m1+m2));
printf("%2d\t%5.6f\t%5.6f\t%5.6f\t
%5.6f\n",c,i,m1,m2,y1);
y0=y1;

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

Program To Solve The Equaition By Using


GUASS ELIMINATION

#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

enter the value of 1 equation


enter the value of coefficient 1: 2
enter the value of coefficient 2: 1
enter the value of coefficient 3: 1
enter the value of constant: 10

enter the value of 2 equation


enter the value of coefficient 1: 3
enter the value of coefficient 2: 2
enter the value of coefficient 3: 3
enter the value of constant: 18

enter the value of 3 equation


enter the value of coefficient 1: 1
enter the value of coefficient 2: 4
enter the value of coefficient 3: 9
enter the value of constant: 16

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

Program To Solve The Equaition By Using


SEIDAL METHOD

#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

You might also like