Bi Section Method
Bi Section Method
METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return ((x*x*x)-(4*x)-9);
}
void main()
{
int itr=0,maxitr;
float x,a,b,x1,aerr;
printf("enter the value a,b,aloowed error maximum iteration ");
scanf ("%f %f %f %d",&a,&b,&aerr,&maxitr);
x=(a+b)/2;
do
{
if ((f(a)*f(x))<0)
b=x;
else
a=x;
itr=itr+1;
x1=(a+b)/2;
printf("iteration no. %d,x=%6.4f\n",itr+1,x1);
if (fabs(x1-x)<aerr)
{
printf("after %d iteration root=%6.4f\n",itr+1,x1);
}
x=x1;
}
while(itr<= maxitr);
printf(" solution does not converges ,iteration is not sufficient ");
getch();
}
EULERS METHOD
#include<stdio.h>
float df(float x,float y)
{
return x+y;
}
main ()
{
float x0,y0,h,x,x1,y1;
puts("enter the values of x0,y0,h,x");
scanf("%f %f %f %f",&x0,&y0,&h,&x);
x1=x0;y1=y0;
while(1)
{
if(x1>x) return 1;
y1+=h*df(x1,y1);
x1+=h;
printf("whenx=%3.1f""y=%4.2f\n",x1,y1);
}}
TRAPEZOIDAL RULE
#include<stdio.h>
float y(float x)
{
return 1/(1+x*x);
}
main()
{
float x0,xn,h,s;
int i,n;
puts("enter x0,xn,no of subintervals");
scanf("%f %f %d",&x0,&xn,&n);
h=(xn-x0)/n;
s=y(x0)+y(xn);
for (i=1;i<=n-1;i++)
s+=2*y(x0+i*h);
printf("value of integral is %6.4f\n",(h/2)*s);
}
RUNGE-KUTTA METHOD
#include<stdio.h>
float f(float x,float y)
{
return x+y*y;
}
main()
{
float x0,y0,h,xn,x,y,k1,k2,k3,k4,k;
printf("enter the value of x0,y0,h,xn\n");
scanf("%f %f %f %f",&x0,&y0,&h,&xn);
x=x0;
y=y0;
while (1)
{
if (x==xn) break;
k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/2);
k4=h*f(x+h,y+k3);
k=(k1+(k2+k3)*2+k4)/6;
x+=h;
y+=k;
printf("when x=%8.4f""y=%8.4f\n",x,y);
}}
SIMPSONS RULE
#include<stdio.h>
float y(float x)
{
return 1/(1+x*x);
}
main()
{
float x0,xn,h,s;
int i,n;
puts("enter x0,xn,no of subintervals");
scanf("%f %f %d",&x0,&xn,&n);
h=(xn-x0)/n;
s=y(x0)+y(xn)+4*y(x0+h);
for (i=3;i<=n-1;i+=2)
s+=4*y(x0+i*h)+2*y(x0+(i-xit1)*h);
printf("value of integral is %6.4f\n",(h/3)*s);
}
MILLNES METHOD
#include<stdio.h>
#include<math.h>
float x[5],y[5],h;
float f(int i)
{
return x[i]-y[i]*y[i];
}
void corect()
{
y[4]=y[2]+(h/3)*(f(2)+4*f(3)+f(4));
printf("%8.4f %8.4f \n",y[4],f(4));
}
main()
{
float xr,aerr,yc;
int i;
puts("enter the value of x0,xr,h,allowed error");
scanf("%f %f %f %f",&x[0],&xr,&h,&aerr);
puts("enter the value of y[i],i=0,3");
for(i=0;i<=3;i++)
scanf("%f",&y[i]);
for(i=1;i<=3;i++)
x[i]=x[0]+i*h;
puts(" x predicated" " corrected");
puts(" x y f""y f");
while (1)
{
if(x[3]==xr) return 1;
x[4]=x[3]+h;
y[4]=y[0]+(4*h/3)*(2*(f(1)+f(3))-f(2));
printf("%6.2f %8.4f %8.4f\n",x[4],y[4],f(4));
corect();
while(1)
{
yc = y[4];
corect();
if(fabs(yc-y[4])<=aerr) break;
}
for(i=0;i<=3;i++)
{
x[i]=x[i+1];
y[i]=y[i+1];
}}}