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

    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Determining the real roots of a polnomial within a given range


    I am a beginner in coding especially in java and I have tried plenty of times figuring how to find the real roots of a polynomial within a given range. The program is supposed to find all the real roots of the given polynomial the user provided. For example, the program should run as follows: Enter the degree: 3 Enter 4 coefficients: -6 11 -6 1 Enter the left and right endpoints: -10 10 Root found at: 1.00000 Root found at: 2.00000 Root found at: 3.00000. Attached below is the format of my program.

    [COLOR=Red]import java.util.Scanner;
    class Roots{
    public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    double resolution=0.01;
    double tolerance=0.0000001;
    double threshold=0.001;
    double roots;
    System.out.print("Enter the degree: ");
    int degree =sc.nextInt();
    System.out.print("Enter "+(degree+1)+" coefficients: ");
    double[] C=new double[degree+1];
    for(int i=0; i<C.length;i++){
    C[i]=sc.nextDouble();
    }
    System.out.print("Enter the left and right endpoints: ");
    double a=sc.nextInt();
    double b=sc.nextInt();
    if(poly(C,a)*poly(C,b)<0){
    roots=findRoot(C,a,b,tolerance);
    }
    }
    }
    static double poly(double[] C, double x){
    int n=C.length-1;
    int K;
    double sum=0.0;
    for(int i=0;i<n;i++){
    sum+=C[i]*(Math.pow((x-i),n));
    }
    return sum;
    }
    static double[] diff(double[] C){
    int n=C.length-1;
    int K;
    double[]D=new double[n];
    for(int i=0;i<n;i++){
    D[i]=C[i]*(n-1);
    }
    return D;
    }
    static double findRoot(double[] C, double a, double b, double tolerance){
    //loops here
    }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    67
    Rep Power
    0
    That's java, this is javascript. Might want to move this post...

    Comments on this post

    • web_loone08 agrees : I have already mentioned it to the moderator. So they should be moving it, anytime.
    http://kawaii-nation.com (^._.^)ノ
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,119
    Rep Power
    4308
    Next time please wait for a moderator to move your thread before making a new one.

    New thread: http://forums.devshed.com/java-help-...en-958473.html

    Comments on this post

    • requinix agrees : that one is actually a few hours *older*
    Spreading knowledge, one newbie at a time.

    Learn CSS. | PHP includes | HTML Validator | CSS validator

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  6. #4
  7. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,119
    Rep Power
    4308
    Oh, oops. Thanks requinix. I had just looked at the time "9:32 PM" and missed that it said "yesterday" instead of "today".

    In that case, jojohaha, please don't post the same question in multiple forums. Pick the one that you feel is best. (If it turns out another is better, a moderator can move your thread for you.)
    Spreading knowledge, one newbie at a time.

    Learn CSS. | PHP includes | HTML Validator | CSS validator

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo