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

In summary, the conversation was about an assignment where the individual was having trouble finding a root of a given function within a certain precision. They asked for help and received advice on improving their code and implementing the bisection algorithm. They also discussed different ways to represent the polynomial in the code. Ultimately, the individual was able to fix their code and successfully complete the assignment.
  • #1
ktafg
2
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
  • #2
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:
  • #3
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.
 
  • #4
thank you very much for your help guys, my code is all sorted!
 
  • #5
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
 
  • #6
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.
 
  • #7
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.
 
  • #8
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 :-)
 

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

1. What is the purpose of the Bisection Method in finding the root of a polynomial equation?

The Bisection Method is used to numerically approximate the root of a given polynomial equation by repeatedly dividing the interval in which the root lies into halves and choosing the half that contains the root. This method is useful when the root cannot be found analytically.

2. How does the Bisection Method work?

The Bisection Method works by first identifying an interval in which the root lies. Then, the midpoint of this interval is calculated and checked to see if it is the root. If not, the interval is divided into two halves and the process is repeated until the root is found within a desired level of accuracy.

3. What are the advantages of using the Bisection Method over other root-finding methods?

The Bisection Method is relatively easy to implement and does not require any derivative calculations, making it suitable for use in situations where the derivative is difficult to obtain. It also guarantees convergence to the root as long as the function is continuous and changes sign within the initial interval.

4. Are there any limitations to using the Bisection Method?

The Bisection Method may be slow to converge compared to other methods, especially when the interval is large or the function is not well-behaved. It also requires the initial interval to be chosen carefully in order to ensure convergence to the root.

5. How can the Bisection Method be implemented in C to find the root of a polynomial equation?

In C, the Bisection Method can be implemented using a loop that calculates the midpoint of the interval, checks if it is the root, and then updates the interval based on the sign of the function at the midpoint. This process is repeated until the root is found within the desired level of accuracy. The code should also include error handling to prevent infinite loops in case the function does not change sign within the chosen interval.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
911
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
12K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top