Use JFreeChart to draw smooth curves and calculate them using the mathematical principle of least squares method for your reference. The specific content is as follows
Draw the graphics:
Code:
FittingCurve.java
package org.jevy; import java.util.ArrayList; import java.util.List; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYItemRenderer; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; public class FittingCurve extends ApplicationFrame{ List<Double> equation = null; //Set the number of polynomials int times = 2; public FittingCurve(String title) { super(title); // Use the least squares method to calculate the coefficients before each term in the fitted polynomial. /* Please note: Polynomial curve parameter calculation is processed separately from Chart chart generation. Calculation of polynomial curve parameters: Responsible for calculating polynomial coefficients and returning the polynomial coefficient List. Chart chart generation: Only responsible for drawing according to the given data. For example, connect a given point. In this example, the smooth curve is drawn with a very dense point line. Since we calculated the coefficients of the polynomial, we made the X-axis data grow in very small steps, and for each X value, we used the polynomial to calculate the Y value, thus obtaining a number of (x,y) groups of points. Draw the dot lines composed of these (x, y) and show a smooth curve. XYSeries is a JFreeChart plot dataset, used to draw a set of related data. XYSeries corresponds to the X and Y axis data set. The data is added as follows: XYSeries s.add(x,y); XYSeriesCollection is a collection of XYSeries. When multiple curves need to be drawn on a Chart, the XYSeries corresponding to multiple curves need to be added to XYSeriesCollection Add method: dataset.addSeries(s1); dataset.addSeries(s2); */ //The number of polynomials is from high to low. The parameters required by this function are: x-axis data <List>, y-axis data <List>, and the number of polynomials <2> this.equation = this.getCurveEquation(this.getData().get(0),this.getData().get(1),this.times); //Generate Chart JFreeChart chart = this.getChart(); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); chartPanel.setMouseZoomable(true, false); setContentPane(chartPanel); } public static void main(String[] args) { // TODO Auto-generated method stub FittingCurve demo = new FittingCurve("XYFittingCurve"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } //Generate chart public JFreeChart getChart(){ //Get XYDataset xydataset = this.getXYDataset(); //Create line chart represented by coordinates JFreeChart xyChart = ChartFactory.createXYLineChart( "Quadratic polynomial fit smooth curve", "X axis", "Y axis", xydataset, PlotOrientation.VERTICAL, true, true, false); //Generate the shape of the coordinate point XYPlot plot = (XYPlot) xyChart.getPlot(); XYItemRenderer r = plot.getRenderer(); if (r instanceof XYLineAndShapeRenderer) { XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r; renderer.setBaseShapesVisible(false); //Is the shape of the coordinate point visible renderer.setBaseShapesFilled(false); } ValueAxis yAxis = plot.getRangeAxis(); yAxis.setLowerMargin(2); return xyChart; } //The dataset is added to the corresponding set according to the logical relationship public XYDataset getXYDataset() { //The preset data point dataset XYSeries s2 = new XYSeries("Double-dot line"); for(int i=0; i<data.get(0).size(); i++){ s2.add(data.get(0).get(i),data.get(1).get(i)); } //The dataset is drawn by fitting the curve XYSeries s1 = new XYSeries("Fitting curve"); //Get fit polynomial coefficients, the equation has been instantiated in the construction method List<Double> list = this.equation; //Get preset point data List<List<Double>> data = this.getData(); //get Max and Min of x; List<Double> xList = data.get(0); double max = this.getMax(xList); double min = this.getMin(xList); double step = max - min; double x = min; double step2 = step/800.0; //Restore the polynomial in the form of a polynomial, and use the polynomial to calculate the value of y when given x for(int i=0; i<800; i++){ x = x + step2; int num = list.size()-1; double temp = 0.0; for(int j=0; j<list.size(); j++){ temp = temp + Math.pow(x, (num-j))*list.get(j); } s1.add(x, temp); } //Add the preset data set to fit the data set to XYSeriesCollection XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(s1); dataset.addSeries(s2); return dataset; } //Simulate the plot data (points) public List<List<Double>> getData(){ //x is the x-axis coordinate List<Double> x = new ArrayList<Double>(); List<Double> y = new ArrayList<Double>(); for(int i=0; i<10; i++){ x.add(-5.0+i); } y.add(26.0); y.add(17.1); y.add(10.01); y.add(5.0); y.add(2.01); y.add(1.0); y.add(2.0); y.add(5.01); y.add(10.1); y.add(17.001); List<List<Double>> list = new ArrayList<List<Double>>(); list.add(x); list.add(y); return list; } //The following code calculates the polynomial coefficients for the least squares method//The least squares method polynomial fitting public List<Double> getCurveEquation(List<Double> x, List<Double> y, int m){ if(x.size() != y.size() || x.size() <= m+1){ return new ArrayList<Double>(); } List<Double> result = new ArrayList<Double>(); List<Double> S = new ArrayList<Double>(); List<Double> T = new ArrayList<Double>(); //Calculate S0 S1... S2m for(int i=0; i<=2*m; i++){ double si = 0.0; for(double xx:x){ si = si + Math.pow(xx, i); } S.add(si); } //Calculate T0 T1 … Tm for(int j=0; j<=m; j++){ double ti = 0.0; for(int k=0; k<y.size(); k++){ ti = ti + y.get(k)*Math.pow(x.get(k), j); } T.add(ti); } //Put S and T into a two-dimensional array as a matrix double[][] matrix = new double[m+1][m+2]; for(int k=0; k<m+1; k++){ double[] matrix = matrix[k]; for(int q=0; q<m+1; q++){ matrixi[q] = S.get(k+q); } matrixi[m+1] = T.get(k); } for(int p=0; p<matrix.length; p++){ for(int pp=0; pp<matrix[p].length; pp++){ System.out.print(" matrix["+p+"]["+pp+"]="+matrix[p][pp]); } System.out.println(); } //Convert the matrix into a triangular matrix matrix = this.matrixConvert(matrix); //Calculate the polynomial coefficients, and the polynomials are arranged from high to low result = this.MatrixCalcu(matrix); return result; } //Convert the matrix to a triangular matrix public double[][] matrixConvert(double[][] d){ for(int i=0; i<d.length-1; i++){ double[] dd1 = d[i]; double num1 = dd1[i]; for(int j=i; j<d.length-1;j++ ){ double[] dd2 = d[j+1]; double num2 = dd2[i]; for(int k=0; k<dd2.length; k++){ dd2[k] = (dd2[k]*num1 - dd1[k]*num2); } } } for(int ii=0; ii<d.length; ii++){ for(int kk=0; kk<d[ii].length; kk++) System.out.print(d[ii][kk]+" "); System.out.println(); } return d; } // Calculate the coefficients before a univariate multi-order equation, which are arranged as xm xm-1 … x0 (polynomial orders are arranged from high to low) public List<Double> MatrixCalcu(double[][] d){ int i = d.length -1; int j = d[0].length -1; List<Double> list = new ArrayList<Double>(); double res = d[i][j]/d[i][j-1]; list.add(res); for(int k=i-1; k>=0; k--){ double num = d[k][j]; for(int q=j-1; q>k; q--){ num = num - d[k][q]*list.get(j-1-q); } res = num/d[k][k]; list.add(res); } return list; } //Get the maximum and minimum value of Double data in List public double getMax(List<Double> data){ double res = data.get(0); for(int i=0; i<data.size()-1; i++){ if(res<data.get(i+1)){ res = data.get(i+1); } } return res; } public double getMin(List<Double> data){ double res = data.get(0); for(int i=0; i<data.size()-1; i++){ if(res>data.get(i+1)){ res = data.get(i+1); } } return res; } } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.