« 高周波・RFニュース 特別編 iFixitがiPhone Airを分解、MLB(Main Logic Board)めちゃくちゃ小さい。REWAやPBKreviewsがiPhone 17 Proを分解、これもMLB小さい。5Gミリ波AiPは一番上に一個、iPhone17はMLB形状も含めてiPhone16とほとんど同じ | トップページ | 高周波・RFニュース 2025年9月23日 Next G Allianceが6Gレポート発行、EuMW2025開幕・日本ガイシがSubTHz用複合ウェハなど発表、Qualcomm Snapdragon Summit 2025は23日から、MediaTekがDimensity 9500発表、Nordicの新SoC、RCRwirelessの6Gレポート、など »

2025年9月22日 (月)

Javaの数値計算ライブラリApache Commons Mathを使う(4) 多項式フィッティング(今回は3次)をしてJFreeChartでプロットする。

今回はこの例題から。

 Visual C# (C_sharp)の数学ライブラリ Math.NET Numericsを使う(4) 多項式フィッティングをして、Array.ConvertAllで一括でフィッティングデータを得る。

コードはこんな感じ。


import java.awt.BorderLayout;
import javax.swing.JFrame;
import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
import org.apache.commons.math3.fitting.PolynomialCurveFitter;
import org.apache.commons.math3.fitting.WeightedObservedPoints;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class CubicFit extends JFrame {
    private static final long serialVersionUID = 1L;
    public static void main(String[] args) {
        CubicFit frame = new CubicFit();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(10, 10, 640, 480);
        frame.setTitle("Cubic Polynomial Fitting");
        frame.setVisible(true);
    }
   
    public CubicFit() {
       
        JFreeChart chart =
          ChartFactory.createXYLineChart("Polynomial Fitting",
                                         "x",
                                         "y",
                                         createData(),
                                         PlotOrientation.VERTICAL,
                                         true,
                                         false,
                                         false);

        XYPlot plot = chart.getXYPlot();
        XYLineAndShapeRenderer renderer =new XYLineAndShapeRenderer();
        NumberAxis yNumAxis = (NumberAxis)plot.getRangeAxis();
        yNumAxis.setRange(-2.0, 2.0);
       
        renderer.setSeriesLinesVisible(0, false);
        renderer.setSeriesShapesVisible(1, false);
        plot.setRenderer(renderer);
        ChartPanel cpanel = new ChartPanel(chart);
        getContentPane().add(cpanel, BorderLayout.CENTER);
    }
   
    private XYSeriesCollection createData(){
         
        double[] x = {0.0, 1.0, 2.0, 3.0,  4.0,  5.0};
        double[] y = {0.0, 0.8, 0.9, 0.1, -0.8, -1.0};
       
        WeightedObservedPoints obs = new WeightedObservedPoints();
        for (int i = 0; i < x.length; i++) {
            obs.add(x[i], y[i]);
        }
        PolynomialCurveFitter fitter = PolynomialCurveFitter.create(3);
        double[] coeff = fitter.fit(obs.toList());
       

       
        PolynomialFunction cubicFitFunction = new PolynomialFunction(coeff);
        double[] xval = new double[600];
        double[] yval = new double[600];
        for (int i = 0; i < xval.length; i++) {
            xval[i] = -2.0 + (6.0 - (-2.0)) * (double)i / (double) (xval.length - 1);
            yval[i] = cubicFitFunction.value(xval[i]);
        }
       
        XYSeriesCollection data = new XYSeriesCollection();

        XYSeries series1 = new XYSeries("Original Points");
        for (int i = 0 ; i < x.length ; i++){
          series1.add(x[i], y[i]);
        }

        XYSeries series2 = new XYSeries("Cubic Fitting");
        for (int i = 0 ; i < xval.length ; i++){
          series2.add(xval[i], yval[i]);
        }
       
        data.addSeries(series1);
        data.addSeries(series2);

        return data;
      }
}

結果はこちら。

Javapolyfit01

 

 

 

« 高周波・RFニュース 特別編 iFixitがiPhone Airを分解、MLB(Main Logic Board)めちゃくちゃ小さい。REWAやPBKreviewsがiPhone 17 Proを分解、これもMLB小さい。5Gミリ波AiPは一番上に一個、iPhone17はMLB形状も含めてiPhone16とほとんど同じ | トップページ | 高周波・RFニュース 2025年9月23日 Next G Allianceが6Gレポート発行、EuMW2025開幕・日本ガイシがSubTHz用複合ウェハなど発表、Qualcomm Snapdragon Summit 2025は23日から、MediaTekがDimensity 9500発表、Nordicの新SoC、RCRwirelessの6Gレポート、など »

パソコン・インターネット」カテゴリの記事

学問・資格」カテゴリの記事

日記・コラム・つぶやき」カテゴリの記事

コメント

コメントを書く

(ウェブ上には掲載しません)

« 高周波・RFニュース 特別編 iFixitがiPhone Airを分解、MLB(Main Logic Board)めちゃくちゃ小さい。REWAやPBKreviewsがiPhone 17 Proを分解、これもMLB小さい。5Gミリ波AiPは一番上に一個、iPhone17はMLB形状も含めてiPhone16とほとんど同じ | トップページ | 高周波・RFニュース 2025年9月23日 Next G Allianceが6Gレポート発行、EuMW2025開幕・日本ガイシがSubTHz用複合ウェハなど発表、Qualcomm Snapdragon Summit 2025は23日から、MediaTekがDimensity 9500発表、Nordicの新SoC、RCRwirelessの6Gレポート、など »

最近の記事

2026年2月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28

最近のコメント

無料ブログはココログ
フォト