Thread: C Program Help

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    4

    C Program Help

    I have a project for my programming class that I am stuck on. It requires using arrays, pointers, structures, and unions to combine the Direct Search method and the Newton-Raphson method with precision of .001 in finding a root. So far I have the Direct Search portion done, but cannot figure out how to begin the Newton-Raphson portion. Please help!

    Here is my working code for the direct search portion that I have done so far.

    Code:
    #include <stdio.h> 
    #include <math.h> 
    #define PI 3.14159265
    double f(double x);
    double direct(double a, double b, int n);
    int main()
    {
        double  x0 = 0.0, xn = 3.0;
        int n = 12;
    printf( "The root using the Direct Search Method is %f\n", direct(x0, xn, n) );
        return 0;
    }
    double f(double x)
    {
        double y;
        y = pow(x,3) - 0.2589 * pow(x,2) + 0.02262 * x - 0.001122;
        return y;
    }
    double direct(double a, double b, int n)
    {
        int i;
        double dx, x = 0.0, f1, f2;
        dx = (b - a) / n;
        f1 = f(a);
        for (i = 1;i <= n; i++)
        {
            x = a + dx * i; f2 = f(x);
            if (f1 * f2 <= 0)
                break; f1 = f2;
        }
        return x - dx/2;
    }
    
    

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Can you describe the Newton-Raphson method step by step? Write it out as a list of steps and then you can translate your steps into code. If you have problems with the translation we can help you there, or if you get bugs in your program. You need to make an attempt first though.

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    4
    As far as the Newton-Raphson method goes, I have a code complete that uses it. It's attached below. As for making an attempt first, I have. I have working codes for each method individually. The reason I am posting on here is because I don't know how to combined them. I don't even know where to start otherwise I would have them combined in some way. I have no background aside from basic coding.

    Code:
    #include <stdio.h>
    #include <math.h>
    #define EPS0 1.0e-12
    double f(double x);
    double fp(double x);
    double newton_raphson(double x, double eps);
    
    int main()
    {
      double  xi = 0, eps = 1.0e-6;
      printf( "The root is %f\n", newton_raphson(xi, eps) );
      return 0;
    }
    
    double f(double x)
    {
      double y;
      y = 1.5*x / pow((1+x*x), 2) - 0.65 * atan(1/x) + 0.65*x / (1+x*x);
      return y;
    }
    
    double fp(double x)
    {
      double y;
      y = (2.8 - 3.2*x*x) / pow((1+x*x), 3);
      return y;
    }
    double newton_raphson(double x, double eps)
    {
      double x1, dx, fx, fpx;
    
      do {
      fx = f(x);
      fpx = fp(x);
     
      if (fabs(fpx) < EPS0)
      {
      printf( "Warning: slope --> 0 !!!\n");
      break;
      }
    
      x1 = x - fx/fpx;
      dx = x1 - x;
      x = x1;
     
      } while (fabs(dx) > eps || fabs(fx) > EPS0);
    
      return x1;
    }

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Honestly not sure what is meant by "combe them".

    My assumption would be they mean to run both methods on the input and then return whichever one gives the best accuracy...or perhaps they mean to combine the two algorithms themselves into 1 function? or something else?

  5. #5
    Registered User
    Join Date
    Nov 2014
    Posts
    4
    I think he wants them within one function. The wording for the requirements are confusing. It is all suppose to be contained in one code. Is there a way to use an answer from one method in the other method?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  2. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM

Tags for this Thread