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

AI Thread Summary
The discussion focuses on implementing the Bisection Method in C to find a root of the polynomial function x^4+x^3-12x^2-2x+10. The original code contains errors, particularly in how the polynomial is evaluated, with suggestions to use Horner's method for efficiency. Participants emphasize the importance of ensuring that the initial values x1 and x2 bracket a root, as the method requires the function to have opposite signs at these points. Additionally, there are recommendations to modularize the polynomial evaluation into a separate function for better code organization. The conversation concludes with a consensus on improving the code structure and efficiency.
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:

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

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 :-)
 
Back
Top