/* <日付を入力> * 演習6.1 * f(x) = e^x の微分を求めるサンプルプログラム * ファイル書き込みのためにjava.ioクラスを使用. */ import java.io.*; // ファイル操作を行うためのインポート public class Differential1 { // フィールドの定義 static final double X_MAX = 1.0; // 計算の最大値 static final double MIN_DIV = X_MAX/100.0; // 微小区間の定義 // メインメソッド public static void main(String [] args) { try { // 変数の定義 double dx = MIN_DIV; // xの微小区間 double x; // 独立変数 double dy; // 導関数の値 // double dy1, dy2; // 導関数の値 double d2y; // 2階の導関数の値 double y0, y1, y2; // 定義関数の計算用 // ファイル書き込みオブジェクトの作成 FileWriter fout = new FileWriter("data.txt"); // defineFunctionメソッドを使うためにオブジェクトを作成 Differential1 differential = new Differential1(); // 差分商による微分計算 for (x=0; x<=X_MAX; x+=MIN_DIV) { y0 = differential.defineFunction(x); y1 = differential.defineFunction(x + dx); y2 = differential.defineFunction(x + 2*dx); // 1階差分商の計算 // dy1 = (y1 - y0) / dx; // dy2 = (y2 - y1) / dx; dy = (y1 - y0) / dx; // 2階差分商の計算 // d2y = (dy2 - dy1) / dx; d2y = (y2 - 2*y1 + y0) / (dx*dx); // 計算結果のファイル出力 // fout.write(x + " " + y0 + " " + dy1 + " " + d2y + " " + "\n"); fout.write(x + " " + y0 + " " + dy + " " + d2y + " " + "\n"); // 計算結果の標準出力(確認用) // System.out.println(x + " " + y0 + " " + dy1 + " " + d2y + " " + "\n"); System.out.println(x + " " + y0 + " " + dy + " " + d2y + " " + "\n"); } // ファイルを閉じる fout.close(); } catch (Exception e) { System.out.println(e); } } // main // 微分する関数の定義 double defineFunction(double x) { return Math.exp(x); } } // class Differential