You are on page 1of 4

BISECTION METHOD

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(3*pow(x,3)+(5*x)-40);
}
void main()
{
float a,b,f1,f2,f3,x3;
printf("Enter the intervals: \n");
scanf("%f%f",&a,&b);
f1=f(a);
f2=f(b);
printf("\n a\t \t b\t\t x\n");
while(f1*f2<0)
{
x3=(a+b)/2;
printf("\n%f \t%f \t%f \t",a,b,x3);
f3=f(x3);
if(f3*f1<0)
{
b=x3;
}
else
{
b=x3;
}
if(fabs(a-b)<0.0001)
break;
}
printf("\n\n Root is=%f",x3);
}
NEWTON RAPHSON METHOD
#include<stdio.h>
#include<math.h>
float f1(float x)
{
return(pow(x,2)+(2*x)-2);
}
float f2(float x)
{
return((2*x)+2);
}
void main()
{
float a,m,h;
int c;
printf("Enter the initial values:");
scanf("%f",&a);
printf("\nx\t \tf(x)\t\tf'(x)\t\th\t\txn");
while(c)
{
h=f1(a)/f2(a);
m=a-h;
printf("\n %f \t %f \t %f \t %f \t%f",f1(a),f2(a),m,h,a);
c++;
if(fabs(a-m)<e)
{
printf("\n The root is: %f",m);
break;
}
else
{
a=m;
}
}
REGULAR FALSI METHOD
#include<iostream.h>
#include<math.h>
#define e 0.001
float f(float x)
{
return(pow(x,3)+3);
}
void main()
{
float a,b,h,f1,f2,x;
int i=0;
printf("Enter the initial values: \n");
scanf("%f%f",&a,&b);
printf("\n no \ta \tb \t n");
while(fabs(a-b)>e)
{
i++;
f1=f(a);
f2=f(b);
h=((b-a)/(f2*f1)*f1);
x=a+h;
printf("\n %d \t %f \t %f \t %f \t %f",i,a,b,x);
if(f(x)*f(a)<0)
{
a=x;
}
else
b=x;
}
printf("\nthe root is %f",x);
}
SIMPSON ONE THIRD RULE
#include<stdio.h>
float f(float x)
{
return(x/(x+1));
}
void main()
{
float a,b,h,x,s1=0,s2=0,s3=0,s=0;
int i=1,n;
printf("Enter the intervals:\n");
scanf("%d",&n);
s1=f(a)+f(b);
h=(b-a)/n;
while(i<n)
{
if(i%2==0)
s2+=f(a+(i*h));
else
s3+=f(a+(i*h));
i++;
}
s=(s1+2*s2+4*s3)*h/3;
printf("The integrationb is %f",s);
}
NEWTON BACKWARD INTERPOLATION
#include<stdio.h>
void main()
{
int i,j,n;
float x,x[10],y[10],f[10][10],h,u,s=0,p=1;
printf("Enter the range:\t");
scanf("%d",&n);
printf("Enter the value of x and y:\n");
for(i=0;i=n;i++)
{
printf("n=");
scanf("%f",&x[i]);
printf("y=");
scanf("%f",&y[i]);
}
printf("Enter value of x to final y \t");
scanf("%f",&x);
h=x[2]-x[1];
u=(x-x[n])/h;s=y[n];
for(i=1;i<=n;i++)
{
f[0][i]=y[i];
}
for(i=1;i<=n;i++)
{
for(j=n;j<1;j--)
{
f[i][n-j]=f[i-1][j]-f[i-1][j-1];
}
for(i=1;i<n;i++)
{
p=p*(u+p-1)/i;
s=s+p*f[i][0];
}
printf("%f",s);
}
NEWTON FORWARD INTERPOLATION
#include<stdio.h>
void main()
{
int i,j,n;
float x,x[10],y[10],f[10][10],h,u,s=0,p=1;
printf("Enter the range:\t");
scanf("%d",&n);
printf("Enter the value of x and y:\n");
for(i=0;i=n;i++)
{
printf("x=");
scanf("%f",&x[i]);
printf("y=");
scanf("%f",&y[i]);
}
printf("Enter value of x for which to find y:");
scanf("%f",&x);
h=x[1]-x[0];
u=(x-x[0])/h;s=y[0];
for(i=1;i<n;i++)
{
f[0][i]=y[i];
}
for(i=1;i<n;i++)
{
for(j=0;j<n;j++)
{
f[i][j]=f[i-1][j+1]-f[i-1][j];
}
for(i=1;i<n;i++)
{
p=p*(u-p+1)/i;
s=s+(p*f[i][0]);
}
printf("required y=%f",s);
}
TRAPEZOIDAL RULE
#include<stdio.h>
float f(float x)
{
return(x/(x+1));
}
void main(){
int i=1,m;
float a,b,h,s1=0,s2=0,s=0;
printf("Enter the lower limit:\t");
scanf("%f",&a);
printf("Enter the upper limit:\t");
scanf("%f",&b);
printf("Enter the no. of intervals:\t");
scanf("%f",&n);
h=(b-a)/n;
s1=f(a)+f(b);
while(i<n)
{
s2+=f(a+i*h);
i++;
}
s=(s1+2*s2)*h/2;
printf("\n The integration is :%f",s);
}

You might also like