BISECTION.C
Euler’s Method
#include<stdio.h> #include #include<math.h> void main() { int i=1; float x1,x2,x3,fx1,fx2,fx3,temp; clrscr(); xyz:printf("Enter 2 values in the range of the root "); scanf("%f %f",&x1,&x2); fx1=pow(x1,3)-2*x1-5; fx2=pow(x2,3)-2*x2-5; printf("Sr.No x1 x2 x3 fx3\n"); if(fx1*fx2<0) { do { temp=x3; x3=(x1+x2)/2; fx3=pow(x3,3)-2*x3-5; printf("\n%5d %9f %9f %9f %9f",i,x1,x2,x3,fx3); if(fx1*fx3<0) x2=x3; else x1=x3; i++; getch(); }while(temp!=x3); printf("\n\nThe root of the equation by Bisection Method is %f",x3); } else { printf("Values not in the range, enter different values\n"); goto xyz; } getch(); }
#include<stdio.h> #include #include<math.h> void main() { float i,x,y,h,fx,n; clrscr(); printf("Enter the values of x,y,h,n "); scanf("%f %f %f %f",&x,&y,&h,&n); while(x
False Position Method
#include<stdio.h> #include #include<math.h> void main() { int i=1; float x0,x1,x2,fx1,fx2,fx0,temp; clrscr(); xyz:printf("\nEnter 2 values in the range of the root "); scanf("%f %f",&x0,&x1); fx0=pow(x0,3)-2*x0-5; fx1=pow(x1,3)-2*x1-5; //printf("Enter the number of iterations "); //scanf("%d",&n); if(fx0*fx1<0) { printf("Sr.No x0 x1 x2 fx2\n"); do { temp=x2; x2=x0-(((x1-x0)*fx0)/(fx1-fx0)); fx2=pow(x2,3)-2*x2-5; printf("\n%5d %9f %9f %9f %9f",i,x0,x1,x2,fx2); if(fx0*fx2>0) x0=x2; else x1=x2; i++; getch(); }while(temp!=x2); printf("\nThe root of the equation by False Position Method is %f",x2); } else { printf("Enter value in different range\n"); goto xyz; } getch(); }
Euler’s Modified Method #include<stdio.h> #include #include<math.h> void main() { float x0,y0,h,fx,x1,y1,y1n,y1n1,temp; clrscr(); printf("Enter the values of x0 & y0 "); scanf("%f %f",&x0,&y0); printf("Enter h: "); scanf("%f",&h); fx=x0+3*y0; printf("fx=%f\n",fx); y1=y0+h*fx; printf("Enter x1 "); scanf("%f",&x1); y1n=y1; printf("y1n=%f",y1n); y1n1=y0+(h/2)*(fx+(x1+3*y1n)); printf("\ty1n1=%f",y1n1); do { temp=y1n1; y1n1=y0+(h/2)*(fx+(x1+3*y1n1)); printf("\ny1n+1=%f",y1n1); }while(temp!=y1n1); printf("\n\ny(%f)=%f",x1,y1n1); getch(); }
Newton Rapsons Method #include<stdio.h> #include #include<math.h> void main() { int i=1; float x0,x1,fx0,fx1,f1x0,temp; clrscr(); printf("\nEnter initial values in the range of the root "); scanf("%f",&x0); fx0=pow(x0,3)-2*x0-5; f1x0=3*pow(x0,2)-2; printf("Sr.No x0 fx0 f1x0 x2\n"); do { temp=x1; x1=x0-(fx0/f1x0); printf("\n%5d %10f %10f %10f %10f",i,x0,fx0,f1x0,x1); x0=x1; fx0=pow(x0,3)-2*x0-5; f1x0=3*pow(x0,2)-2; i++; getch(); }while(temp!=x1); printf("\nThe root of the equation by Newton Raphson Method is %f",x1); getch(); }
Gauss Elimination #include<stdio.h>
#include #include<math.h> void main() { float x,y,z,x1,y1,z1,x2,y2,z2; clrscr(); printf("The 3 equations are:\n"); printf("10x+y+z=12\n"); printf("2x+10y+z=13\n"); printf("2x+2y+10z=14\n"); x=0; y=0; z=0; while(x2!=x1 && y2!=y1 && z2!=z1) { x2=x1; x1=(12-y-z)/10; x=x1; y2=y1; y1=(13-2*x-z)/10; y=y1; z2=z1; z1=(14-2*x-2*y)/10; z=z1; printf("\nx=%f y=%f z=%f",x1,y1,z1); } getch(); }
Ringe-Kutta Method #include<stdio.h> #include void main() { float x0,y0,x,y,x1,y1,K1,K2,h; clrscr(); printf("Enter the intial values of x & y "); scanf("%f %f",&x0,&y0); printf("Enter the value of h "); scanf("%f",&h); printf("Enter the final value of x "); scanf("%f",&x1); do { K1=h*(y0-x0); x=x0+h; y=y0+K1; K2=h*(y-x); y1=y0+((K1+K2)/2); printf("\ny(%f)=%f",x,y1); x0=x; y0=y1; }while(x0!=x1); getch(); }
Trapezoidal Method #include<stdio.h> #include #include<math.h> #define e 2.72 void main() { float y[10],I,J,K; int i,a,b,h,n; clrscr(); printf("Enter the values of a,b,n: "); scanf("%d %d %d",&a,&b,&n); h=(b-a)/n; printf("h=%d",h); for(i=a;i<=b;i=i+h) { y[i]=pow(e,i); printf("\ny[%d]=%f",i,y[i]); } J=0; for(i=a+h;i<=b-h;i=i+h) { J=J+y[i]; } K=(y[a]+y[b]+2*J); I=K*h/2;
printf("\nThe value of Numerical Integration by Trapezoidal rule is = %f",I); getch(); }
Simpsons 1/3 Rule
#include<stdio.h> #include #include<math.h> #define e 2.72 void main() { float y[10],I,J,K,L; int i,a,b,h,n; clrscr(); printf("Enter the values of a,b,n: "); scanf("%d %d %d",&a,&b,&n); h=(b-a)/n; printf("h=%d",h); for(i=a;i<=b;i=i+h) { y[i]=pow(e,i); printf("\ny[%d]=%f",i,y[i]); } J=0; for(i=a+h;i<=b-h;i=i+h+h) { J=J+y[i]; } L=0; for(i=a+h+h;i<=b-h-h;i=i+h+h) { L=L+y[i]; } K=(y[a]+y[b]+4*J+2*L); I=K*h/3; printf("\nThe value of Numerical Integration by Simpson's 1/3rd rule is = %f",I); getch(); }
Simpsons 3/8 Rule
#include<stdio.h> #include #include<math.h> #define e 2.72 void main() { float y[10],I,J,K,L; int i,a,b,h,n; clrscr(); printf("Enter the values of a,b,n: "); scanf("%d %d %d",&a,&b,&n); h=(b-a)/n; printf("h=%d",h); for(i=a;i<=b;i=i+h) { y[i]=1/pow(1+i,2); printf("\ny[%d]=%f",i,y[i]); } J=0; L=0; for(i=a+h;i<=b-h;i=i+h) { if(i%3==0) { L=L+y[i]; } else { J=J+y[i]; } } K=(y[a]+y[b]+3*J+2*L); I=K*3*h/8; printf("\nThe value of the Numerical Intergral by Simpson's 3/8th rule is = %f",I);getch(); }