/* S0710.java * ショットノイズのシミュレーション * (C) H.Ishikawa 2008 */ package simulation; import java.applet.*; import java.awt.*; import java.awt.event.*; import window.Window; public class S0710 extends Applet implements ActionListener { Button button0; Button button1; boolean sw = false; public void init() { button0 = new Button(" 実行 "); button1 = new Button("クリア"); add(button0); add(button1); button0.addActionListener(this); button1.addActionListener(this); } public void actionPerformed(ActionEvent e) { String label = e.getActionCommand(); if (label.equals(" 実行 ")) sw = true; else sw = false; repaint(); } public void paint(Graphics g) { Window w; w = new Window(); int height; int width; int SPACE = 30; int T_END = 1000000; double P = 0.01; int P_WIDTH = 10000; /* パルス幅 */ int t, tt; int i[] = new int[T_END]; /* 電流 */ int f[] = new int[150]; /* ヒストグラム */ double x = 0.0; double x2 = 0.0; int n = 0; int ii, j; /* スクリーンの大きさを読み取る */ width = getSize().width; height = getSize().height; /* グラフィックの準備 */ w.setWindow(0, 0.0, 0.0, (double) T_END, 150.0, SPACE, height - SPACE, width / 2, SPACE); w.axis(0, "t", T_END / 10, "i", 50.0, g); w.moveTo(0, 0.0, 0.0, g); w.setWindow(1, 0.0, 0.0, 0.1, 150.0, width / 2 + SPACE, height - SPACE, width - SPACE, SPACE); w.axis(1, "freq", 0.01, "i", 50, g); /* メイン */ if (sw == true) { w.moveTo(1, 0.0, 0.0, g); for (t = 0; t < T_END; t++) { //(1) if (P > Math.random()) { //(2) for (tt = t; tt <= t + P_WIDTH; tt++) { if (tt < T_END) { i[tt] = i[tt] + 1; } } } } for (t = P_WIDTH; t < T_END; t++) { f[i[t]] = f[i[t]] + 1; //(3) x = x + (double) i[t]; x2 = x2 + (double) i[t] * (double) i[t]; n = n + 1; } /* 表示 */ for (t = 0; t < T_END; t++) { g.setColor(Color.green); w.lineTo(0, (double) t, (double) (i[t]), g); } for (ii = 0; ii < 150; ii++) { g.setColor(Color.blue); w.line(1, 0.0, (double) ii, (double) f[ii] / (double) n, (double) ii, g); } g.setColor(Color.black); g.drawString("mean = " + (x / (double) n) + " var = " + (x2 - x * x / (double) n) / (double) n, 10, height); stop(); } } }