Bisection Method in C to Find Root of x^4+x^3-12x^2-2x+10

Click For Summary

Discussion Overview

The discussion revolves around implementing the bisection method in C to find a root of the polynomial function x^4+x^3-12x^2-2x+10. Participants are sharing code snippets, troubleshooting issues, and discussing the efficiency of their implementations, focusing on precision and algorithm correctness.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses difficulty in implementing the bisection method and requests help with their code.
  • Another participant suggests that the variable 'd' should represent the width of the interval rather than a value of the function, indicating a potential flaw in the code logic.
  • There is a recommendation to use Horner's method for evaluating the polynomial instead of the pow() function, which is considered inefficient.
  • Concerns are raised about the validity of the bisection method if the function has the same sign at the initial points x1 and x2, with specific values provided as examples.
  • Several participants engage in correcting and refining the polynomial representation, discussing different forms of the polynomial and their efficiency.
  • One participant proposes an alternative approach using an array to store polynomial coefficients and a function to calculate the polynomial value, which could facilitate derivative calculations.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best implementation approach, as multiple methods and representations for evaluating the polynomial are discussed. There are corrections and refinements made, but no final agreement on a single method is established.

Contextual Notes

There are unresolved issues regarding the correct implementation of the bisection method, particularly concerning the initial conditions and the handling of polynomial evaluation. The discussion includes various representations of the polynomial, which may lead to confusion without clear definitions.

ktafg
Messages
2
Reaction score
0
Hi guys, I'm doing an assignment for working out a certain root of a given function, x^4+x^3-12x^2-2x+10, to a precision of 10-6. We are asked to give two limits of where we want to find a root between but I'm getting stuck. I was just wondering if any of you could look at my code and maybe point out where I'm going wrong, thanks.

Here's my code:

Code:
#include<stdio.h>
#include<math.h>

main()
{double x1,x2,x,series,series1,d;

printf("enter x1 and x2\n");
scanf("%lf%lf",&x1,&x2);

x=0;

 while(d>0.0001)
    {
      x=(x1+x2)/2;

      series=(pow(x,4))+(pow(x,3))-(12*(pow(x,2))-(2*x)+10);

     series1=(pow(x1,4))+(pow(x1,3))-(12*(pow(x1,2))-(2*x1)+10);

     d=fabs(series);

         if(series*series1 < 0)
             x2=x;
         else
             x1=x;
    }

printf("%lf",x);

return 0;
}
 
Physics news on Phys.org
d should be not a value of the function, but width of the interval. I have just skimmed your code so could be I missed some other problems related to your bisection algorithm implementation.

There is one thing that cries I AM INEFFICIENT:

ktafg said:
Code:
series=(pow(x,4))+(pow(x,3))-(12*(pow(x,2))-(2*x)+10);

Never ever use pow() function while calculating value of a polynomial. Use Horner scheme:

[tex]x^4+x^3-12x^2-2x+10 = x(x(x(x+1)-12)-2)+10[/tex]

which means replacing

Code:
series=(pow(x,4))+(pow(x,3))-(12*(pow(x,2))-(2*x)+10);

with

Code:
series=x*(x*(x*(x+1)-12)-2)+10;

Also note you don't need parentheses around (12*(pow(x,2)).

And moving calculation of polynomial to the function external to the main body will make it simpler to modify the program when you will be given another function (right now you have the same code in two different places, it can be in one place only).
 
Last edited:
You can't use bisection if the function has the same sign at the initial points x1 and x2. Your function, f(x)=x4+x3-12x2-2x+10 (see note), has a value of 10 at x=0 and a value of 4 at x=3. You should give some kind of error message if the user specifies 0 and 3 as the initial values of x1 and x2.Note: As Borek noted, this is better written as x*(x*(x*(x+1)-12)-2)+10 and should be a separate function.
 
thank you very much for your help guys, my code is all sorted!
 
beweare
x*(x*(x*x+1)-12)-2)+10 = x^4+x^2-12 x+8

...x^4+x^3- 12x^2 -2x + 10 =
(((1x +1)x - 12)x - 2)x + 10
 
Xitami said:
beweare
x*(x*(x*x+1)-12)-2)+10 = x^4+x^2-12 x+8

My mistake, DH posted correct formula. Correcting my post.

...x^4+x^3- 12x^2 -2x + 10 =
(((1x +1)x - 12)x - 2)x + 10

For some reason I prefer the x*(x*(x*(x... version.
 
Xitami said:
beweare
x*(x*(x*x+1)-12)-2)+10 = x^4+x^2-12 x+8
No, it doesn't. The left hand side isn't even well-formed.

Looking back, Borek did make that mistake. He also pointed out the name of the representation scheme. It was stated correctly as x*(x*(x*(x+1)-12)-2)+10 in post #3.


...x^4+x^3- 12x^2 -2x + 10 =
(((1x +1)x - 12)x - 2)x + 10
There's no reason for that 1*x. Just write x. (This happens a lot; a polynomial with a leading coefficient of one is a proper, or monic, polynomial.)
So ((((x+1)x-12)x-2)x+10
Or 10+(-2+(-12+(1+x)x)x)x
Or x(x(x(x+1)-12)-2)+10
They're all the same thing. I like the latter two better than the first, and the last one best. That first one looks a bit inside out to me. But that's just personal preference. All three have the same number of multiplications.
 
i prefer
Code:
double a[]={ 10, -2, -12, 1, 1};
const int ord=sizeof(a)/sizeof(a[0]) - 1;
then
Code:
double series(double x){
	int i; double s=a[ord]; 
	for( i=ord-1; i>=0; i-- )
		s = s*x +a[i];
	return s; }
we can calculate derivatives, zero of 3-rd is start of bisection
SFME :-)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
12K
  • · Replies 16 ·
Replies
16
Views
4K