Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- double P(double x)
- {
- return ((6435.0*pow(x,8)-12012.0*pow(x,6)+6930.0*pow(x,4)-1260.0*pow(x,2)+35.0)/128.0);
- }
- double Pderivative (double x)
- {
- return ((8.0*6435.0*pow(x,7)-6.0*12012.0*pow(x,5)+4.0*6930.0*pow(x,3)-2.0*1260.0*x)/128.0);
- }
- int main()
- {
- //NR
- double x0 = 0.4;
- double xN;
- //Bisection
- double xL = 0.4;
- double xR = 0.7;
- double xM;
- double Tol = 0.00000001;
- double Error;
- //printf ("Enter x0: ");
- //scanf("%d%", &x0);
- //printf ("Enter xL: ");
- //scanf("%d%", &xL);
- //printf ("Enter xR: ");
- //scanf ("%d", &xR);
- //printf ("Enter tolerance: ");
- //scanf("%d", &Tol);
- printf("%.10lf\n", x0);
- printf("%.10lf\n", xR);
- printf("%.10lf\n", xL);
- printf("%.10lf\n", Tol);
- int n = 0;
- int b = 0;
- do{
- if ((((x0-xL)*Pderivative(x0)-P(x0))*((x0-xR)*Pderivative(x0)-P(x0)))<0)
- {
- //NEWTON
- xN = x0 -(P(x0)/Pderivative(x0));
- Error=fabs((xN-x0)/xN);
- n++;
- }else{
- //BISECTION
- xM = (xL+xR)/2;
- if((P(xL)*P(xM))>0.0)
- {
- xL=xM;
- }
- else{
- xR=xM;
- }
- Error=fabs((xR-xL)/xM);
- b++;
- }
- } while (Error>Tol);
- printf("%.10lf\n",xN);
- printf("%.10lf\n",xM);
- printf("%d NR iterations\n",n);
- printf("%d Bisection iterations\n",b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement