Results 1 to 2 of 2
Thread: Polynomial Graphing help
 12012011, 10:11 AM #1Member
 Join Date
 Dec 2011
 Posts
 1
 Rep Power
 0
Polynomial Graphing help
Hey everyone. I'm working on a graphing program for my class that is supposed to graph a polynomial based on user input. What I'm having trouble with is calling my constructor class into my test class correctly. I just know I'm not doing it correctly, but I'm stuck and need some help. If it helps, here's everything that my programs supposed to do
The PolyGrapher class
This class must contain methods with the following signatures:
public void plot(): When invoked, this method
Clears all existing graphs off the screen
prompts the user for the values of the coefficients for a polynomial
instantiates a polynomial with the data obtained in the previous step
prompts the user for the range [a, b] over which the polynomial is to be evaluated
invokes getGraphData() with the polynomial, range and number of points to plot and returns a data array with yMin, yMax and sf.
invokes graph() with the polynomial, data array with yMin, yMax and sf, and a color not used for the grid lines or axes.
public double[] getGraphData(Polynomial p, double a, double b, int nPoints) When invoked, this method determines the values nPoints, yMin, yMax, and sf
Evaluates the polynomial at the appropriate values for x compute maximum and minimum values of the function on [a, b].
Computes the scaling factor sf. sf = graphHeight / ( max  min ).
public void plotAndDifferentiate(): When invoked, this method
invokes plot()
invokes the current polynomial's differentiate method to get a new polynomial, the derivative of the current polynomial
invokes graph() with the new polynomial, same data, and a new color.
private double[] graph(Polynomial p, double[] data, Color c): When invoked, this method displays the line graph of the polynomial in an 800x600 image plotted from a through b on nPoints using the color c.
Evaluates the polynomial at the appropriate values for x
Displays the line graph of the polynomial in an 800x600 image.
Returns the scale factor and minimum value of the plot.
Other methods as desired to make your code more readable/useable.
The Polynomial class
This class models a polynomial and must contain methods with the following signatures:
A constructor: public Polynomial(double [] coefficients)
public double evaluate(double x)  returns the value of the polynomial at the specified value for x.
public Polynomial differentiate()  returns a new polynomial, the derivative of this polynomial.
public String toString()  returns a string representation of this polynomial
Java Code:import algoritharium.*; import java.util.Scanner; import java.awt.Color; public class Polygrapher { public static void main(String[] args) { new ImageViewer(); } double aRange, bRange, x; int function; double[] c = new double[4]; public void createGraph(int w, int h) { Image img = ImageViewer.getImage(); Color[][] c = new Color[h][w]; for(int ci=0;ci<w;ci++) { for(int cj=0;cj<h;cj++) { int si=ci*img.getWidth()/w; int sj=cj*img.getHeight()/h; Color color = img.getPixelColor(si,sj); c[cj][ci]=color; } } ImageViewer.createImage(c); } public void plot() { createGraph(800,600); Scanner input = new Scanner(System.in); System.out.print("Coefficient for x^3: "); c[0] = input.nextInt(); System.out.print("Coefficient for x^2: "); c[1] = input.nextInt(); System.out.print("Coefficient for x: "); c[2] = input.nextInt(); System.out.print("Constant: "); c[3] = input.nextInt(); function = (int)(c[0]*(x*x*x) + c[1]*(x*x) + c[2]*x+c[3]); //Polynomial[] p = new Polynomial[function]; ????? System.out.print("Starting x: "); aRange = input.nextInt(); System.out.print("Ending x: "); bRange = input.nextInt(); getGraphData(p[0],aRange,bRange,np); graph(p, aRange, Color.GREEN); } public double[] getGraphData(Polynomial p, double a, double b, int nPoints) { double max; double min; a=aRange; b=bRange; double graphHeight=600; double sf=graphHeight/(maxmin); //nPoints=(int)((maxmin)*sf); ???????? np=nPoints; }
and here is my constructor class:
Java Code:public class Polynomial { private double[] c; private int n; public Polynomial (double [] c){ // constructor c = new double [c.length]; n = c.length 1; for (int i =0; i < c.length; i++) c[i] = c[i]; } public String toString() { if (n ==0) return "" + c[0]; if (n==1) return c[1] + "x + " + c[0]; String s = c[n] + "x^" + n; for (int i = n1; i >= 0; i) { if (c[i] == 0) continue; else if (c[i] > 0) s += " + " + (c[i]); else if (c[i] < 0) s += "  " + (c[i]); if (i == 1) s += "x"; else if (i > 1) s += "x^" + i; } return s; } public double evaluate(double x){ double y=0.0; int i; for (i = n; i >= 0; i) y = c[i] + x * y; return y; } public Polynomial differentiate(){ double [] deriv; if (n == 0) { deriv = new double [1]; deriv [0] = 0; } else { deriv = new double [c.length  1]; for (int i = 0 ; i < deriv.length; i++) deriv [i] = (i + 1) * c[i + 1]; } return new Polynomial(deriv); } }
Last edited by captain; 12012011 at 10:23 AM.
 12012011, 01:53 PM #2
Similar Threads

Need help graphing
By rperalta in forum Java 2DReplies: 6Last Post: 09242011, 05:59 PM 
Graphing GUI
By spyroth in forum NetBeansReplies: 3Last Post: 02122011, 03:52 AM 
Scientific Graphing
By shruthi Nadiger in forum AWT / SwingReplies: 2Last Post: 03152010, 12:37 AM 
Graphing in java
By collin389 in forum New To JavaReplies: 2Last Post: 01182010, 02:54 AM 
Help with graphing problem
By adlb1300 in forum New To JavaReplies: 2Last Post: 11262007, 03:50 PM
Bookmarks