#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Fault that cannot be detected


    The following is a program on solving an equation by bisection method.
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<math.h>
    int i=0,count=0,flag=0;
    float x1,x2,x3=0;
    float fx1=0,fx2=0,fx3=0,t=0;
    float func(float f)
    {
    return(log10(f)-cos(f));
    }
    int check()
    {
    printf("\n\t INITIAL x1= ");
    scanf("%f",&x1);
    printf("\n\t INITIAL x2= ");
    scanf("%f",&x2);
    fx1=fx2=fx3=0.0;
    if((func(x1)*func(x2))>0)
    {
    printf("\n\t INITIAL VALUES ARE NOT PERFECT.");
    return(1);
    }
    return(0);
    }
    void main()
    {
    clrscr();
    printf("\n\n\t\t PROGRAM FOR BISECTION METHOD");
    printf("\n\n\t EQUATION:::::LOG(X)-COS(X)");
    while(1)
    {
    if (check()==0)
    {
    flag=1;
    break;
    }
    check();
    }
    printf("\n ***********************************************************");
    printf("\n ITERATION X1 FX1 X2 FX2 X3 FX3 ");
    printf("\n ***********************************************************");
    if (flag==1)
    {
    while ((fabs(x2-x1))>=0.0001)
    {
    count++;
    fx1=fx2=fx3=0;
    fx1=func(x1);
    fx2=func(x2);
    printf ("\n %d %.3f %.3f %.3f %3f %.3f %.3f",count,x1,fx1,x2,fx2,x3,fx3);
    t= fx1*fx3;
    if (t>0)
    {
    x1=x3;
    }
    if (t<0)
    {
    x2=x3;
    }
    }
    printf("\n\t ROOT OF EQUATION IS %f::::",x3);
    }
    getch();
    }
    I have found this on the Net. An output is also given which I am not getting. Can someone tell me what is going wrong.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    5
    Not quite sure, but where is fx3 used once it's initialized to zero? I think that's the problem. fx3 is permanently zero.

    Comments on this post

    • Lux Perpetua agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    You should really try to understand this program that you've downloaded from somewhere on the web. As you are finding out now if you didn't already know, stuff found on the internet can be pretty unreliable. Astrodude's comment is valid, and that's not even the most serious bug in the code, which is that this program doesn't actually implement bisection (or anything useful, actually). On top of that, this program follows a number of bad coding practices ("void main()", questionable use of global variables, superfluous use of conio.h in what should be a pure console program, among others).

    By the way, next time you post code on this forum, make sure to put it in [code][/code] tags. As you can see, the formatting of the code in your post has not been preserved.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    Ah yes, yet another here is some found code I don't understand poster.
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Unsurprisingly, the attribution has been removed from the code. Surprisingly, so have the two lines of the algorithm that give it the name "bisection."

    Comments on this post

    • salem agrees : LOL - one edit too far, unbelievable.

IMN logo majestic logo threadwatch logo seochat tools logo