Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- double f(double x)
- {
- return 2.3*x*sin(x)+10.3*x-1.5/x; //x*x*x-2;
- }
- double f1(double x)
- {
- return 2.3*sin(x)+2.3*x*cos(x)+10.3+1.5/(x*x);
- }
- void GetRootRange(double *L, double *U)
- {
- double y;
- for(y=-999999.0l;y<999999.0l;y=y+1.5)
- {
- if(f(y)*f(y+1.5)<0.0)
- {
- *L=y;*U=y+1.5;
- return;
- }
- }
- }
- double NR(double x0, double err)
- {
- double x;
- int k=0;
- while(1)
- {
- if (f1(x0)!=0.0)
- x = x0 - f(x0)/f1(x0);
- else
- {
- printf("\nDiscontinued at %lf",x0);
- exit(0);
- }
- printf("\n%d\t%10.5f\t%10.5f",k,x0,x);
- k++;
- if(fabs(x-x0)<err)
- return x;
- x0 = x;
- }
- }
- int main()
- {
- const double err = 1.0E-10;
- double x0,x1;
- //printf("\nEnter initial guess:");
- //scanf("%lf",&x0);
- GetRootRange(&x0,&x1);
- x0=(x0+x1)/2.0;
- printf("\nSoulution:%lf",NR(x0,err));
- return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement