0% found this document useful (0 votes)
158 views11 pages

Newton Rapson Method

Newton Rapson Method Program to find out real root of equation f(x) = x3-2x-1 by Newton Rapson method. Enter the allowed error for solution:0.001 the solution converges in iteration: 2274 the solution is: 1.512695.

Uploaded by

Vikas Varshney
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views11 pages

Newton Rapson Method

Newton Rapson Method Program to find out real root of equation f(x) = x3-2x-1 by Newton Rapson method. Enter the allowed error for solution:0.001 the solution converges in iteration: 2274 the solution is: 1.512695.

Uploaded by

Vikas Varshney
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Newton Rapson Method


Program to find out real root of equation f(x) = x3-2x-1.

Formula Used:X(n+1)=xn+h=xn-(f(xn)/f(xn)) Where, h=-f(x0)/f(x0)

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)

{ if (df(x0)==0.0) { printf("method fails!!!\n"); break; } else {count++; h=f(x0)/df(x0); x1=x0-h; x0=x1; }

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:-

Enter 1st approximation x1:1

Enter 1st approximation x2:2

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

Find the value of integral f(x) = 1/(1+x2).

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(); }

Output:Enter the values of x0,xn,n: 0 6 6 The value of integral 1.4108.

4. Simpson 1/3rd rule.


Evaluate the integral(limit 0 to 4) ex using Simpson 1/3rd rule.

Formula Used:-

Integration f(x)dx lim(x0 to x0+nh)=h/3[(y0+yn)+4(y1+y2+y3+..+yn1)+2(y2+y4+...+yn-2)]

Program://simpson 1/3rd rule #include<stdio.h>

#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(); }

Output:enter x0 ,xn ,no. of subintervals:

x0=0 xn=4 no of subintervals=4

value of integral is : 53.87.

5.

Simpson 3/8 rule.

Evaluate the integral(limit 0 to 4) 1/(1+x^2) using simpson 3/8 rule.

Formula Used:-

Integration f(x)dx lim(x0 to x0+nh)=3h/8[(y0+yn)+3(y1+y2+y3+..+yn1)+2(y3+y6+...+yn-3)]

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);

for(i=3;i<=n-3;i+3) s+=2*y(x0+i*h); s-=3*y(x0+i*h); printf("value of integral is %f",(3h/8)*s); getch(); }

Output:Enter the values of x0,xn,n x0=0 xn=1 n=6

value is=0.785396

You might also like