3
\$\begingroup\$

I have created a bit of code that plots data for a project (XY Line Chart). It works. But it isn't what I'd consider good. I am looking to improve it in two ways:

  1. Make it more generic, so that I can just feed in a dataset of series and it spit out a chart. This might become useful in later assignments.

  2. I'd like to understand how to customize what I have, which seems very difficult to do given the way I've mashed together various snippets I've found online. By customize, I mean add custom labels, change series colors or thickness, etc.

So, below you can find my code thus far. Keep in mind I don't really understand much about Java objects, classes, what extends means, etc. Please keep the explanation at dummy level. I'm also looking for review of the chart stuff specifically, but if you see other opportunities to dump knowledge on me, feel free.

This bit is in a separate document, because someone told me that is a good thing to do:

public class XYPlot extends JFrame {

    public XYPlot(String title, String header, String xAxis, String yAxis, XYSeriesCollection dataset) {
        super(header);
        //final XYSeriesCollection data = new XYSeriesCollection(series);
        final JFreeChart chart = ChartFactory.createXYLineChart(
                title,
                xAxis,
                yAxis,
                dataset       
        );

        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(1200, 800));
        setContentPane(chartPanel);
    } 
}

This is my "main" document, where all my stuff executes:

/**
 * Numerical Analysis - Math 521
 * @author Wesley Neill
 * wesley.neill@gmail.com
 * wcn0002@mix.wvu.edu
 */

public class RombergApp {

    private static int counter;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

    seriesPlotter(functionDataCollector(), "Function Evaluations", "Math 521", "n in [m,9)", "Log_10(F(n,m))");
    seriesPlotter(errorDataCollector(), "Error Plot", "Math 521", "n in [m,9)", "Log_10(E(n,m))" );
    //main_function();
    }


    public static double f(double x){

        double y = Math.sin(x);
        counter ++;        
        return y;
    }

    public static double R00(double a, double b){

        return 0.5*(b-a)*(f(a) + f(b));
    }

    public static double RN0(double Rn_minus_1, double n, double a, double b){

        double value =0;
        double dx = (b-a)/Math.pow(2,n);

        for (double i=1; i<= Math.pow(2, n-1); i++){
            value = value + f(a + (2*i-1)*dx);
        }

        return 0.5*Rn_minus_1 + dx*value;

    }

    public static double RNM(double Rn_mm1, double Rnm1_mm1, double m){

        double answer = (1/(Math.pow(4, m)-1))*(Math.pow(4, m)*Rn_mm1 - Rnm1_mm1);
        return answer;
        //return (Rn_mm1 + 1/(Math.pow(4, m)-1))*(Rn_mm1 - Rnm1_mm1);
    }

    public static double [][] R_nm(double n, double m, double a, double b){

        double [][] RR  = new double [21][21];        
        int i;
        int j;

        for(j = 0; j<=m; j++){

            if (j==0){
                for(i = 0; i<=n; i++){
                    if(i==0&&j==0){
                        RR[i][j] = R00(a,b);
                    } else
                        RR[i][j] = RN0(RR[i-1][j],i, a, b);
                }
            }else
                for(i = 0; i<=n; i++){
                    if (i<j){
                        RR[i][j] = 0;
                    } else
                        RR[i][j]=RNM(RR[i][j-1], RR[i-1][j-1],j);
                }
        }
        return RR;
    }

    public static void main_function(){
        counter = 0;
        double a = Double.parseDouble((String) JOptionPane.showInputDialog("Enter a: "));
        double b = Double.parseDouble((String) JOptionPane.showInputDialog("Enter b: "));
        double n = Double.parseDouble((String) JOptionPane.showInputDialog("Enter n: "));
        double m = Double.parseDouble((String) JOptionPane.showInputDialog("Enter m: "));
        double[][] RR = R_nm(n, m, 0, (Math.PI)/2);
        double actual = Math.cos(0)-Math.cos((Math.PI)/2);
        double error = abs((actual - RR[intValue(n)][intValue(m)])/actual);


        System.out.println("Error: "+ error);
        System.out.println("Function evaluations: "+ "["+n+"]"+"["+m+"]: " + counter);
        System.out.println();

//        for(int i = 0; i<=n; i++){
//            for(int j = 0; j<=m; j++){
//                if(RR[i][j]==0){ 
//                }else
//                System.out.printf("%-35s", "["+i+"]"+"["+j+"]: " +  RR[i][j]);
//            }
//            System.out.println();
//        }

    }

    public static XYSeriesCollection functionDataCollector(){
        //counter = 0;

        XYSeries series;
        XYSeriesCollection dataset = new XYSeriesCollection();

        for(int i=0;i<3;i++){
            series = new XYSeries("m="+ Integer.toString(i));
            for(int k = i; k<9;k++){
                R_nm(k,i,0,(Math.PI)/2);
                System.out.println("Function evaluations for ["+k+"]"+"["+i+"]: "  + counter);
                series.add(k, Math.log10(counter));               
                counter = 0;
               // System.out.println("Function counter reset to: " + counter);
            }         
            dataset.addSeries(series);   
        }   
        return dataset;
    }

        public static XYSeriesCollection errorDataCollector(){
        //counter = 0;

        XYSeries series;
        XYSeriesCollection dataset = new XYSeriesCollection();
        double actual = Math.cos(0)-Math.cos((Math.PI)/2);
        double error;
        double[][] RR = R_nm(9,3, 0, (Math.PI)/2);

        for(int i=0;i<3;i++){
            series = new XYSeries("m="+ Integer.toString(i));
            for(int k = i; k<9; k++){
               error = abs((1 - RR[intValue(k)][intValue(i)])/1);
               series.add(k, Math.log10(error));
            }
            dataset.addSeries(series);   
        }

        return dataset;
    }



    public static void seriesPlotter(XYSeriesCollection dataset, String title, String header, String xAxis, String yAxis) {

        XYPlot myPlot = new XYPlot(title, header, xAxis, yAxis, dataset);
        myPlot.pack();
        myPlot.setVisible(true);

    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.