Results 1 to 2 of 2
  1. #1
    captain is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default 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
    Here's part of my main class

    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/(max-min);
    		//nPoints=(int)((max-min)*sf); ????????
    		np=nPoints;
    
    	}
    I know the getGraphData method is all wrong right now so don't mind that. But since the created data type "Polynomial" is one of the parameters I need to know how to invoke it correctly.

    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 = n-1; 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; 12-01-2011 at 09:23 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Polynomial Graphing help

    I need to know how to invoke it correctly.
    Can you explain what is not working in your posted code? Show an example of the program's output and explain what it wrong with the output and show what the output should be.

Similar Threads

  1. Need help graphing
    By rperalta in forum Java 2D
    Replies: 6
    Last Post: 09-24-2011, 05:59 PM
  2. Graphing GUI
    By spyroth in forum NetBeans
    Replies: 3
    Last Post: 02-12-2011, 02:52 AM
  3. Scientific Graphing
    By shruthi Nadiger in forum AWT / Swing
    Replies: 2
    Last Post: 03-14-2010, 11:37 PM
  4. Graphing in java
    By collin389 in forum New To Java
    Replies: 2
    Last Post: 01-18-2010, 01:54 AM
  5. Help with graphing problem
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •