Thread: Find the first 3 roots of a function HELP! - bisection method

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    41

    Find the first 3 roots of a function HELP! - bisection method

    i'm trying to write a program that would find the root of a function using 2 the start and end inputs from a user and using the bisection method.However , it only gives me a root at 0 with f(x) = 50 which is wrong.

    Now , the program is pretty lengthy so I only provided the parts needed however if u need the rest ask me.





    Code:
    /*----------------------------------------------------File: bisectionSearchSoln.c
    Description: Applies bisection root finding method.
    ------------------------------------------------------*/
    #include <stdio.h>
    #include <math.h>
    #include "gng1106plplot.h"
    #define N 100  // number of points
    #define TRUE 1
    #define FALSE 0
    #define EPSILON 1e-10
    // Function Prototypes
    void findInterval(double *, double *);
    int findRoot(double, double, double *);
    void plotFunc(double, double, int, double []);
    double func(double);
    void plot(int,  double[], double[], int, double[], double[]);
    double getMin(double *, int );
    double getMax(double *, int );
    /*--------------------------------------------------------
    Function: main()
    ---------------------------------------------------------*/
    int main()
    {
        double start, end;
        double root;
        int n;  // number of roots found
        // Get intervals from user
        findInterval(&start, &end);
        printf("Finding root for interval between %.4f and %.4f\n",
               start, end);
        if(findRoot(start, end, &root))
        {
            printf("Found root at: %.8f (f(x) = %.8f)\n",root, func(root));
            n = 1;
        }
        else
        {
             printf("Did not find root in interval.");
             n = 0;
        }
        plotFunc(start, end, n, &root);
        return 0;
    }
    /*----------------------------------------------------------
    Function: findRoot
    Parameters
        lower, upper:  lower and upper values of x for the interval
        root  - pointer to array for saving roots
    Returns:  TRUE if a root was found (store in root), and FALSE
              if no root exists in interval.
    Description: Find the root between the interval for the
                 function using the bisection
                 method.
    ---------------------------------------------------------------*/
    
    
    int findRoot(double lower, double upper, double *root)
    {
     double x0 , x1 ;
    
    
     x0=upper;
     x1= lower;
    int retVal = FALSE;
    if(func(x0)*func(x1) > 0)
    {
        do
      {
        retVal = TRUE;
    
    
        *root = (x0+x1)/2;
    
    
        if(func(*root) > 0 )
         x1 = *root;
        else x0 = *root;
    
    
    
    
    }while(fabs(*root)< EPSILON);
    
    
    return(TRUE);
    }
    }
    /*----------------------------------------------------------
    Function: func
    Parameters:
        x - x value function f(x)
    Returns: value y of function f(x)
    Description: Plots the value of the function:
    
    
    f(x) = 50 - x^2 |cos(sqrt(x)| , x must be positive, if x
                             negative return 0.
    -----------------------------------------------------------*/
    double func(double x)
    {
      double fx;
    
    
        if(x < 0)
          fx = 0;
    
    
      fx = 50 - x*x * fabs(cos(sqrt(x)));
    
    
      return fx;
    
    
    
    
    }
    Last edited by Kamal Joub; 11-12-2017 at 12:35 AM.

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    This is kind of a novice programmer's opinion, but I don't think you have too much code in there, but you have a lot of comments. As I have sought advice on how to use comments the advice that seems the best to me is that your line-of-code to comment ratio should be at least greater than 5 to 1. From what I've read, as a person gets better at programming, the code itself reads just as well as a native language. Also, some whitespace is good when you want to break up segments of code, but when you have whitespace everywhere it doesn't help too much.

    I guess I just say this because you are hesitant on posting too much code, but it is really a lot of whitespace and comments that is likely just skipped over as the reader reads.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    I didn't only post the rest of the code because its too long but because it's irrelevant to the problem at hand.Also , thanks for the advice but you could you please help with the bigger problem because the comments are kind of irrlevant if the program is not working properly

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    Maybe a more talented reader can decern your code better than myself, but I need to follow it from start to finish. This requires definitions of findInterval and plotFunc, and any other functions that might be used within these functions.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > However , it only gives me a root at 0 with f(x) = 50 which is wrong
    I suppose this is a lesson in testing as you go, rather than writing the whole thing and wondering why it doesn't work.

    So put some print statements in - or use a debugger.
    Code:
    int findRoot(double lower, double upper, double *root)
    {
     double x0 , x1 ;
     
     
     x0=upper;
     x1= lower;
    int retVal = FALSE;
    if(func(x0)*func(x1) > 0)
    {
        do
      {
        retVal = TRUE;
     printf("Iteration: x0=%f, x1=%f\n", x0, x1 );
     
        *root = (x0+x1)/2;
     
     
        if(func(*root) > 0 )
         x1 = *root;
        else x0 = *root;
     
     
     
     
    }while(fabs(*root)< EPSILON);
     
     
    return(TRUE);
    }
    }
    Oh, and fix your indentation.
    Indentation style - Wikipedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by jack jordan View Post
    [...] but you have a lot of comments. [...]
    "Lots of" comments providing detailed descriptions of functions is generally good. The amount of commenting in the code provided by the author is suitable IMO (I'm not sure if the contents of the comments is good, but the size of the comments documenting the functions is quite normal)

    Edit: Have a look at some source code that is documented using Doxygen at some point. The Amiga technical reference manuals were created, in part at least, in the same way using what they called AutoDoc. Detailed function documentation is a good thing.
    Last edited by Hodor; 11-12-2017 at 04:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bisection method
    By babibabi in forum C Programming
    Replies: 1
    Last Post: 08-18-2012, 10:11 AM
  2. Help in my bisection method..
    By anttonnee in forum C Programming
    Replies: 12
    Last Post: 05-10-2012, 06:08 PM
  3. bisection method
    By swty_todd in forum C Programming
    Replies: 4
    Last Post: 02-08-2009, 12:33 AM
  4. Newton method to find roots
    By ICool in forum C Programming
    Replies: 17
    Last Post: 11-18-2007, 11:46 AM
  5. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM

Tags for this Thread