SHARE
TWEET

Untitled

a guest Mar 24th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. double P(double x)
  6. {
  7.     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);
  8. }
  9.  
  10. double Pderivative (double x)
  11. {
  12.     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);
  13. }
  14.  
  15. int main()
  16. {
  17.     //NR
  18.     double x0 = 0.4;
  19.     double xN;
  20.  
  21.     //Bisection
  22.     double xL = 0.4;
  23.     double xR = 0.7;
  24.     double xM;
  25.  
  26.     double Tol = 0.00000001;
  27.     double Error;
  28.  
  29.     //printf ("Enter x0: ");
  30.     //scanf("%d%", &x0);
  31.     //printf ("Enter xL: ");
  32.     //scanf("%d%", &xL);
  33.     //printf ("Enter xR: ");
  34.     //scanf ("%d", &xR);
  35.     //printf ("Enter tolerance: ");
  36.     //scanf("%d", &Tol);
  37.    
  38.     printf("%.10lf\n", x0);
  39.     printf("%.10lf\n", xR);
  40.     printf("%.10lf\n", xL);
  41.     printf("%.10lf\n", Tol);
  42.  
  43.  
  44.     int n = 0;
  45.     int b = 0;
  46.  
  47.     do{
  48.         if ((((x0-xL)*Pderivative(x0)-P(x0))*((x0-xR)*Pderivative(x0)-P(x0)))<0)
  49.         {
  50.             //NEWTON
  51.             xN = x0 -(P(x0)/Pderivative(x0));
  52.             Error=fabs((xN-x0)/xN);
  53.  
  54.             n++;
  55.  
  56.         }else{
  57.             //BISECTION
  58.             xM = (xL+xR)/2;
  59.             if((P(xL)*P(xM))>0.0)
  60.             {
  61.                 xL=xM;
  62.             }
  63.             else{
  64.                 xR=xM;
  65.             }
  66.             Error=fabs((xR-xL)/xM);
  67.  
  68.             b++;
  69.  
  70.         }
  71.  
  72.     } while (Error>Tol);
  73.  
  74.     printf("%.10lf\n",xN);
  75.     printf("%.10lf\n",xM);
  76.     printf("%d NR iterations\n",n);
  77.     printf("%d Bisection iterations\n",b);
  78.  
  79.     return 0;
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top