Thread: Problem Solving Multiple Roots For Quartic Equation

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    3

    Problem Solving Multiple Roots For Quartic Equation

    Hello

    I am having trouble understanding how this loops would work.
    Give the function (x*x*x*x) - (10*x*x*x) + (35*x*x) - (50*x) + 24
    Write a program that will use bisection method to find the roots of this function.
    Define lower limit and upper limit (e.g. -1.05 and 6.05)
    Starting at the lower limit step along the X axis at intervals of 0.1 for H calculating the function values f(x) and f(x +h), then f(x+h) and f(x+2h) until the upper limit is exceeded. If sign of the function value changes this indicates a root between ranges. Apply bi sectional method to this range until root has been found with an epsilon of 0.000001, then continue on until upper limit has been exceeded and all 4 roots found. If the function is within 0.000001 of zero then root has been found therefore no need for bi sectional method.

    Code:
    #include "stdafx.h"
    #include "math.h"
    #define H 0.1
    #define epsilon 0.000001
    double F(double x);
    
    
    int main(void)
    {
    	double root, midpt, midvalue, rtvalue, rtpoint, ltvalue, ltpoint, a, b, c;
    	int loop_count;
    
    
    	printf("\nSet Lower and Upper Limit Ranges");
    	scanf_s("%lf %lf%", &a, &B)/>/>;
    		
    /*Interval Loop to find changes in sign*/
    	do {
    	c = a + H;
    	ltvalue = F(a);
    	rtvalue = F(B)/>/>;
    		if (ltvalue > 0 && rtvalue > 0 || ltvalue < 0 && rtvalue < 0);
    	a = H ++;
    		else
    			do {
    			midpt = (ltvalue + rtvalue)/2;
    			rtpoint = F(rtvalue);
    			midvalue = F(midpt);
    			loop_count ++;
    			if (rtvpoint * midvalue >= 0)
    			rtvalue = midpt;
    			else ltvalue = midpt;
    				}			 
    			while ((rtvalue - ltvalue) > epsilon);
    			root = (rtvalue+ltvalue)/2;
    
    
    printf("\nRoot is: %.8f\n", root);
    
    
    /*Continue on with intervals until next root indicator then apply bisectional method???*/
    
    
    printf("\nNumber of Loops: %d\n", loop_count);
    
    
    return 0;
    }
    
    
    /*Function of quartic equation*/
    
    
    double F(double x){
    return (x*x*x*x) - (10*x*x*x) + (35*x*x) - (50*x) + 24;
    }
    So we have a function and a range. Program requires to work along the X axis at 0.1 increments until it reaches a point where the value changes from positive to negative or negative to positive. Then apply bisection method within that range to a given accuracy then print that root. Then continue on X axis until the next change of sign is found. I am not sure how these loops are to be constructed.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    This is a great opportunity to use functions. Not just your F(x) function, but also a bisection function. It will also make solving this easier, as it will help you keep two distinct problems (the "coarse" search by steps of 0.1 and the "fine" search to an epsilon of .000001) separate.

    But first, you must know how to solve this yourself, with paper and pencil, using the exact method you want the computer to use. If you don't know how to do it, you can't tell (program) the computer to do it. As you work through it by hand, make note of every little step you do, as they will become the basis of your algorithm. Read up on the bisection method for some help if need be.

    For example (yes, I know you did some of these already)

    1. Make sure you know the roots of your polynomial ahead of time so you can verify your program works.
    2. Write your F(x) function and make sure it returns proper values for a handful of x values (verify by hand/calculator).
    3. Write a loop that iterates x from lower_limit to upper_limit by increments of step_size (a for loop is best for something like this). Print each value of x to verify it's working correctly (note, you don't have to verify every number, just the start, end and that it changes by the right amount).
    4. Take that for loop, and calculate F(x) and F(x + h). If you notice a sign change between F(x) and F(x + h), print something like "sign change". Once you finish you bisection algorithm, you will call it here and pass in x and x + h.

    That should be plenty to get you going, it's certainly enough help on the coarse search part. For the bisection method, I would recommend a while loop, but I'll leave the rest up to you for now. Give all that a shot, and post back if you have more trouble.

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    2.5 Check that the function actually has roots and if not skip the bisection method altogether

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    To be pedantic, all polynomials have roots, it's a question of whether they're real. And yes, this is a good step in general, but since this only has to work for a single polynomial, which is order 4 and for which all 4 roots are real, he can skip it.

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Yes, I should have use the phrase "real roots".

    I wouldn't skip it because that F() function looks more like a test case to me...

    EDIT: A better test would be "Are there any roots within the user-specified range?"

    http://en.wikipedia.org/wiki/Sturm%27s_theorem
    Last edited by SirPrattlepod; 09-10-2013 at 09:39 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Not to argue the point, but I think that first paragraph is the actual assignment, i.e. that F(x) is not a test case. The OP would have to verify that for us.

    However, the "coarse" searching by steps of 0.1, looking for a sign change, is a sort of rudimentary Sturm's theorem, ensuring at least one real root in that range. The bisection will only happen if there's a real root. So it's already implemented insofar as the assignment requires, and there is no danger of getting stuck trying to find imaginary roots.

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by anduril462 View Post
    Not to argue the point, but I think that first paragraph is the actual assignment, i.e. that F(x) is not a test case. The OP would have to verify that for us.

    However, the "coarse" searching by steps of 0.1, looking for a sign change, is a sort of rudimentary Sturm's theorem, ensuring at least one real root in that range. The bisection will only happen if there's a real root. So it's already implemented insofar as the assignment requires, and there is no danger of getting stuck trying to find imaginary roots.
    You might be right... will have to wait for the OP to clarify.

    At any rate I'd probably at least add a comment, to whatever the implementation ends up being, outlining potential improvements or how to make the implementation more general.

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    That F(x) is a test case. E.g. Quartic equation of a.X^4 - b.X^3 + c.X^2 - d.X + e

    A is always 1 and the values b,c,d,e are sourced from a text file, however I plan on implementing that after I get the main problem solved.

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    All good sorted it all out. Thanks for the help guys. I had a fair few things wrong, but my initial code was going down the right track which I thought I had completely wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quadratic equation solving
    By narendrav in forum C Programming
    Replies: 6
    Last Post: 05-18-2011, 08:49 AM
  2. program for real roots of quadratic equation
    By jackson6612 in forum C++ Programming
    Replies: 24
    Last Post: 04-03-2011, 04:40 AM
  3. Replies: 10
    Last Post: 03-13-2008, 11:04 AM
  4. Equation solving
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-24-2002, 02:13 PM
  5. equation solving
    By ajlott_coder in forum C Programming
    Replies: 6
    Last Post: 01-26-2002, 02:34 AM