import java.awt.*; import java.lang.Math.*; // 微分方程式 y'' + 2y'/x + y = 0 // の理論解と rngkt クラスによるルンゲクッタ法による解 // を比較する public class sphbes extends java.applet.Applet{ protected int Swidth,Sheight; protected int Cwidth,Cheight; protected double h; protected int yKeta,xKeta; protected Dimension WindowSize; protected Dimension WindowHalfSize; public void init(){ String tempStr; WindowSize = new Dimension(); WindowHalfSize = new Dimension(); WindowSize = getSize(); WindowHalfSize.height = WindowSize.height / 2; WindowHalfSize.width = WindowSize.width / 2; // x の刻み幅 try{ h = Double.valueOf(getParameter("h")).doubleValue(); }catch(NumberFormatException e){ h = 0.1; } // x の値を表示する文字数 tempStr = "" + h; xKeta = tempStr.length(); // y,z,dz の値を表示する文字数 try{ yKeta = Integer.parseInt(getParameter("yKeta"),10); }catch(NumberFormatException e){ yKeta = 7; } } public void paint(Graphics g){ double x; double z; double zz; double dz; double dzz; double[] y; double[] ans; // rngkt クラスを継承したクラスのオブジェクト変数 myrngkt rngktObj; y = new double[2]; ans = new double[2]; Font myFont; int FontHeight; int StringHeight; String tempStr; String DispStr; // x の初期値 x = 1.0; // y の初期値 y[0] = Math.sin(x)/x; // y' の初期値 y[1] = (x * Math.cos(x) - Math.sin(x)) / (x*x); // フォントの高さ FontHeight = 13; // 現在描画している文字の位置(高さ) StringHeight = 0; // ルンゲクッタ・クラスのサブクラスのオブジェクトを生成 rngktObj = new myrngkt(); // フォント・クラスを生成 myFont = new Font("TimesRoman",Font.PLAIN,FontHeight); // 題目を表示 StringHeight += FontHeight; g.drawString("" + " x , y , z , y-z",0,StringHeight); while(x < 10.0 && StringHeight < Sheight){ // ルンゲクッタ法による近似計算を実施 ans = rngktObj.rngkt_func(x,y,h); y[0] = ans[0]; y[1] = ans[1]; // 理論解の値を計算 x = x + h; z = Math.sin(x)/x; zz = (x * Math.cos(x) - Math.sin(x)) / (x*x); // y の値の理論解と近似解の差を計算 dz = y[0] - z; // y' の値の理論解と近似解の差を計算 // 今回はこれについては表示していない dzz = y[1] - zz; // 以下は表示のための文字列操作を実施 DispStr = "" + ""; tempStr = "" + x; if(xKeta < tempStr.length()){ tempStr = tempStr.substring(0,xKeta); } DispStr = "" + tempStr; tempStr = "" + y[0]; if(yKeta < tempStr.length()){ tempStr = tempStr.substring(0,yKeta); } DispStr += "" + ", " + tempStr; tempStr = "" + z; if(yKeta < tempStr.length()){ tempStr = tempStr.substring(0,yKeta); } DispStr += "" + ", " + tempStr; tempStr = "" + dz; DispStr += "" + ", " + tempStr; // ここで表示処理 StringHeight += FontHeight; g.drawString(DispStr,0,StringHeight); } } }