Sie sind auf Seite 1von 8

BISECTION.

CPP

#include<stdio.h>
#include<conio.h>
#include<math.h>
/*function*/
float f(float x)
{
float f;
/*f=x*x*x-cos(x)*cos(x);*/
f=pow(2.7182828, x)-3*cos(x)+1;
return(f);
}
/*Main Program */
main()
{
float a, b, e, x0, N;
int k;
clrscr();
printf("\n Enter accuracy");
scanf("%f", &e);
do
{
printf("\n Enter interval a,b");
scanf("%f%f",&a,&b);
}while(f(a)*f(b)>0.0);
k=0;
do
{
x0=(a+b)/2.0;
printf("\nI=%d a=%f b=%f x0=%f f(x0)=%f |b-a|=%f" , k+1, a, b, x0, f(x0), fabs(b-a));
if(f(a)*f(x0)< 0.0)
{b=x0;}
else
{a=x0;}
k=k+1;
}while(fabs(b-a)>e);
printf("\n Root of the equation is %f" , x0);
printf("\n Actual no. of iterations required to achieve desired accuracy,%d",k);
getch();
return 0;
}
EULER.CPP

#include<stdio.h>
#include<math.h>
#define f(x,y) (x-y)/(x+y)
void main()
{
int i,n;
float x0,y0,h,xn,x,y;
printf("\n enter the value of x0,y0,h,xn:- \n");
scanf("%f%f%f%f",&x0,&y0,&h,&xn);
n=(xn-x0)/h+1;
for(i=1;i<=n;i++)
{
y=y0+h*f(x0,y);
x=x0+h;
printf("\n X=%f Y=%f",x0,y0);
if (x<=xn)
{
x0=x;
y0=y;
}
}
return;
}

Lagranges.CPP

#include<stdio.h>
#include<math.h>
void main()
{
int i,n,j;
float X,x[50],y[50],sum,Y;
printf("\n enter the value of n:");
scanf("%d",&n);
printf("\n enter the value of x[i],y[i]");
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&y[i]);
printf("\n enter the value of X for Y is to find : \n");
scanf("%f",&X);
Y=0.0;
for(i=1;i<=n;i++)
{
sum=y[i];
for(j=1;j<=n;j++)
{
if(i!=j)
sum=sum*(X-x[j])/(x[i]-x[j]);
}
Y=Y+sum;
}
printf("\n \n Hence X=%f value of Y=%f",X,Y);
return;
}

NEWTONRA.CPP

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x) x *exp(x)-1
#define DF(x) x*exp(x)+exp(x)
void main()
{
int i,n;
float a,b,x0,err,f,df,k,f1,f2;
printf("\n enter the value of a,b,n,err:\n");
scanf("%f%f%d%f",&a,&b,&n,&err);
f1=F(a);
f2=F(b);
if(f1*f2>0)
printf("\n there is no root lies between %f and %f",a,b);
else
{
x0=(a+b)/2;
for(i=1;i<=n;i++)
{
f=F(x0);
df=DF(x0);
x=x0-(f/df);
printf("\n x(%d)=%f",i,x);
getch();
k=F(x);
if(k<0)
k=-k;
if(k<=err)
{
printf("\n\n root x=%f F(x)=%f",x,F(x));
getch();
return;
}
x0=x;
}
printf("\n solution does not converge");
return;
}}

NONAMEOO.CPP

#include<stdio.h>
#include<conio.h>
#include<math.h>
/*function/
float f(float x)
{
float f:
/*f=x*x*x-cos(x)*cos(x);*/
f=pow(2.7182828,x)-3*cos(x)+1;
return(f);
}
/*Main Program */
main()
{
Float a,b,e,x0,N;
int k;
clrscr();
printf("\n Enter accuracy");
scanf("%f" , &e);
do
{
printf("\n Enter interval a,b");
scanf("%f%f",&a,&b);
}while(f(a)*f(b)>0.0);
k=0;
do
{
x0=(a+b)/2.0;
printf("\nI=%d a=%f b=%f x0=%f f(x0)=%f |b-
a|=%f" , k+1,a,b,x0,f(x0),fabs(b-a));
if(f(a)*f(x0)< ,0)
{b=x0}
else
{a=x0;}
k=k+1;
}while(fabs(b-a)>e);
printf("\n Root of the equation is %f" ,x0);
printf("\n Actual no. of iterations required to achieve desired accuracy,%d",k);
getch();
}
REGULAFA.CPP

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x) pow(x,3)-x-1
void main()
{
int i,n;
float a,b,err,x,k,fa,fb;
printf("\n enter the value of a,b,n,err:- \n");
scanf("%f%f%d%f",&a,&b,&err);
fa=F(a);
fb=F(b);
if(fa*fb>0)
printf("\n there is no root between a and b");
else
{
for(i=1;i<=n;i++)
{
fa=F(a);
fb=F(b);
x=(a*fb-b*fa)/(fb-fa);
printf("\n x(%d)=%f",i,x);
getch();
k=F(x);
if(k<0)
k=-k;
if(k<=err)
{
printf("\n iterations=%d root=%ff(x)=%f",i,x,F(x));
return;
}
if(F(x)*fb>0)
b=x;
else
a=x;
}
printf("\n iterations are not sufficient :");
return;
}
getch();
return;
}

SIMPSON.CPP

#include<stdio.h>
#include<math.h>
#define F(x) 1.0/(1+pow(x,2))
void main()
{
int i,n;
float a,b,sum,simp,h,x,x1,x2;
printf("\n Enter the value of a,b,n:\n");
scanf("%f%f%d",&a,&b,&n);
h=(b-a)/n;
x=a+h;
sum=F(a)+F(b)+4*F(x);
for(i=3;i<=n-1;i=i+2)
{
x1=(a+(i*h));
x2=(a+(i-1)*h);
sum=sum+4*F(x1)+2*F(x2);
}
simp=(h/3)*sum;
printf("\n \n value of integral=%f",simp);
return;
}

SIMSIONT.CPP

#include<stdio.h>
#include<math.h>
#include<conio.h>
void main ()
{
int i,j,n;
float h,sum1=0.0,sum2=0.0,sum3=0.0,x[15],y[15];
printf("\n Enter size: \t");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
printf("Enter x[%d] : \t",i);
scanf("%f",&x[i]);
printf("Enter y[%d] : \t",i);
scanf("%f",&y[i]);
}
h=(x[n]-x[0])/n;
printf("h is =%f",h);
sum1=y[0]+y[n];
for(i=1;i<=n-1;i=i+3)
{
sum3=sum3+(3*(y[i]+y[i+1]));
}
for(i=3;i<=n-1;i=i+3)
{
sum2=sum2+(2*y[i]);
}
sum1=(3*h)/8*(sum1+sum2+sum3);
printf("\n sum is %f",sum1);
getch();
}

TRAPIZON.CPP

#include<stdio.h>
#define F(x) 1/(1+x)
void main()
{
int i,n;
float a,b,sum,trap,h,x;
printf("\n enter the value of a,b,n:\n");
scanf("%f%f%d",&a,&b,&n);
h=(b-a)/n;
sum=0.0;
for(i=1;i<=n-1;i++)
{
x=(a+(i*h));
sum=sum+F(x);
}
trap=h*(F(a)+(2.0)*sum+F(b))/(2.0);
printf("\n \n value of integral = %f",trap);
return;
}

YUGESH_E.CPP

#include<stdio.h>
#include<math.h>
#define F(x) 1.0/(1+pow(x,2))
void main()
{
int i,n;
float a,b,sum,simp,h,x,x1,x2;
printf("\n Enter the value of a,b,n:\n");
scanf("%f%f%d",&a,&b,&n);
h=(b-a)/n;
x=a+h;
sum=F(a)+F(b)+4*F(x);
for(i=3;i<=n-1;i=i+2)
{
x1=(a+(i*h));
x2=(a+(i-1)*n);
sum=sum+4*F(x1)+2*F(x2);
}
simp=(h/3)*sum;
printf("\n \n value of integral=%f",simp);
return;
}

Das könnte Ihnen auch gefallen