Newton Rapson Method
Newton Rapson Method
Program:#include<stdio.h> #include<conio.h> #include<math.h> float f(float x) {return(x*x*x-2*x-1);} float df(float x) {return(3*x*x-2);} void main() {int count=0; float x0=0,x1=1,h=1,err; clrscr(); while(f(x0)*f(x1)>0.0) {x0++; x1=x0+1;} printf("the interval in which root lies is %f %f",x0,x1); printf("enter the allowed error"); scanf("%f",&err); while(fabs(h)>err)
printf("\nItertation %d the value of x1= %f",count,x1);} printf("\nThe root of equation is: %f",x1); getch(); }
Output:The interval in which root lies is 1.000000 2.000000 Enter the allowed error: 0.001
Itertation 1 the value of x1= 3.000000 Itertation 2 the value of x1= 2.200000 Itertation 3 the value of x1= 1.780831 Itertation 4 the value of x1= 1.636303 Itertation 5 the value of x1= 1.618305 Itertation 6 the value of x1= 1.618034 The root of equation is: 1.618034
2. Bisection Method
Program to find out real root of equation f(x) = ex-x3 by Bisection method.
Program:#include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> float f(float x) {return(exp(x)-x*3);} void main() {int i; float x1,x2,x3,err; clrscr(); printf("\nEnter 1st approximation x1:"); scanf("%f",&x1); printf("\nEnter 1st approximation x2:"); scanf("%f",&x2); if(f(x1)*f(x2)<0) printf("\nThe approximations are correct."); else {printf("\nThe approximations are Wrong!!"); getch(); exit(0);} printf("\nEnter the allowed error for solution:");
scanf("%f",&err); while(fabs(x2-x1)>err) {i++; x3=(x1+x2)/2; if(f(x1)*f(x3)<0) {x2=x3; printf("\nThe iteration %d x1=%f and x2=%f\n",i,x1,x2); printf("\n");} else x1=x3; printf("\nThe iteration %d x1=%f and x2=%f\n",i,x1,x2); printf("\n");} printf("\nThe solution converges in itertion %d \n",i); printf("\nThe solution is:%f",x3); getch(); }}
Output:-
The approximations are correct. Enter the allowed error for solution:0.001 The solution converges in iteration: 2274 The solution is: 1.512695
3.
Trapezoidal Method
Formula Used:-
Lim(x to x+nh) f(x)dx=(h/2)(y0+yn)+2(y1+y2)+....yn-1. Where n=no. of sub intervals. S=sum. X0=lower limit. Xn=upper limit. h=(xn-x0)/n.
Program:#include<stdio.h> #include<conio.h> #include<math.h> float y(float x) { return (1/(1+x*x)); } void main() { int i,n; float x0,xn,h,s;
clrscr(); printf("enter the values of x0,xn,n"); 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*n); printf("the value of integral %f",(h/2)*s); getch(); }
Formula Used:-
#include<conio.h> #include<math.h> float y(float x) { return exp(x); } void main() { int i,n; float x0,xn,h,s; printf("enter the values of x0,xn,n"); 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-1)*h); printf("the integral is equal to %f \n",(h/3)*s); getch(); }
5.
Formula Used:-
Program:#include<stdio.h> #include<conio.h> #include<math.h> float y(float x) { return (1/(1+x*x)); } void main() { int i,n; float x0,xn,h,s; printf("enter the values of x0,xn,n"); scanf("%f %f %d",&x0,&xn,&n); h=(xn-x0)/n; s=y(x0)+y(xn);
value is=0.785396