| 情報学部 | 菅沼ホーム | 目次 | 索引 |
/****************************/
/* 指数分布の計算 */
/* coded by Y.Suganuma */
/****************************/
#include <stdio.h>
double p; // α%値を計算するとき時α/100を設定
double ram; // 母数
/****************************************/
/* 指数分布の計算(P(X = x), P(X < x)) */
/* x : データ */
/* ram : 母数 */
/* pr : P(X = x) */
/* return : P(X < x) */
/****************************************/
#include <math.h>
double exponential(double x, double ram, double *pr)
{
double f = 0.0, y;
if (x < 0)
*pr = 0.0;
else {
y = exp(-ram * x);
*pr = ram * y;
f = 1.0 - y;
}
return f;
}
/******************************/
/* P(X > x) - 1 + p(関数値) */
/******************************/
double exponential_f(double x)
{
return p - exp(-ram * x);
}
/**************************/
/* P(X = x)(関数の微分) */
/**************************/
double exponential_df(double x)
{
return ram * exp(-ram * x);
}
/*****************************************************/
/* Newton法による非線形方程式(f(x)=0)の解 */
/* f : f(x)を計算する関数名 */
/* df : f(x)の微分を計算する関数名 */
/* x0 : 初期値 */
/* eps1 : 終了条件1(|x(k+1)-x(k)|<eps1) */
/* eps2 : 終了条件2(|f(x(k))|<eps2) */
/* max : 最大試行回数 */
/* ind : 実際の試行回数 */
/* (負の時は解を得ることができなかった) */
/* return : 解 */
/*****************************************************/
#include <math.h>
double newton(double(*f)(double), double(*df)(double), double x0,
double eps1, double eps2, int max, int *ind)
{
double g, dg, x, x1;
int sw;
x1 = x0;
x = x1;
*ind = 0;
sw = 0;
while (sw == 0 && *ind >= 0) {
sw = 1;
*ind += 1;
g = (*f)(x1);
if (fabs(g) > eps2) {
if (*ind <= max) {
dg = (*df)(x1);
if (fabs(dg) > eps2) {
x = x1 - g / dg;
if (fabs(x-x1) > eps1 && fabs(x-x1) > eps1*fabs(x)) {
x1 = x;
sw = 0;
}
}
else
*ind = -1;
}
else
*ind = -1;
}
}
return x;
}
/********/
/* main */
/********/
int main()
{
double x, pr, f, up, h;
int sw;
char file1[100], file2[100];
FILE *out1, *out2;
printf("母数は? ");
scanf("%lf", &ram);
printf("目的とする結果は? \n");
printf(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)\n");
printf(" =1 : p%値( P(X > u) = 0.01p となるuの値) ");
scanf("%d", &sw);
if (sw == 0) {
printf("グラフ出力?(=1: yes, =0: no) ");
scanf("%d", &sw);
// 密度関数と分布関数の値
if (sw == 0) {
printf(" データは? ");
scanf("%lf", &x);
f = exponential(x, ram, &pr);
printf("P(X = %f) = %f, P( X < %f) = %f (母数 = %f)\n", x, pr, x, f, ram);
}
// グラフ出力
else {
printf(" 密度関数のファイル名は? ");
scanf("%s", file1);
printf(" 分布関数のファイル名は? ");
scanf("%s", file2);
out1 = fopen(file1,"w");
out2 = fopen(file2,"w");
printf(" データの上限は? ");
scanf("%lf", &up);
printf(" 刻み幅は? ");
scanf("%lf", &h);
for (x = 0; x < up+0.5*h; x += h) {
f = exponential(x, ram, &pr);
fprintf(out1, "%f %f\n", x, pr);
fprintf(out2, "%f %f\n", x, f);
}
}
}
// %値
else {
printf("%の値は? ");
scanf("%lf", &x);
p = 0.01 * x;
if (p < 1.0e-7)
printf("%f%値 = ∞ (母数 = %f)\n", x, ram);
else {
f = newton(exponential_f, exponential_df, 0.0, 1.0e-6, 1.0e-10, 100, &sw);
printf("%f%値 = %f sw %d (母数 = %f)\n", x, f, sw, ram);
}
}
return 0;
}
/****************************/
/* 指数分布の計算 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
import java.text.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Test {
static double p; // α%値を計算するとき時α/100を設定
static double ram; // 母数
/********/
/* main */
/********/
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double x, f, up, h;
double pr[] = new double [1];
int sw, sw1[] = new int [1];
System.out.print("母数は? ");
ram = Double.parseDouble(in.readLine());
System.out.println("目的とする結果は? ");
System.out.print(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)\n");
System.out.print(" =1 : p%値( P(X > u) = 0.01p となるuの値) ");
sw = Integer.parseInt(in.readLine());
if (sw == 0) {
System.out.print("グラフ出力?(=1: yes, =0: no) ");
sw = Integer.parseInt(in.readLine());
// 密度関数と分布関数の値
if (sw == 0) {
System.out.print(" データは? ");
x = Double.parseDouble(in.readLine());
f = App.exponential(x, ram, pr);
System.out.println("P(X = " + x + ") = " + pr[0] + ", P( X < " + x + ") = " + f + " (母数 = " + ram + ")");
}
// グラフ出力
else {
String file1, file2;
System.out.print(" 密度関数のファイル名は? ");
file1 = in.readLine();
System.out.print(" 分布関数のファイル名は? ");
file2 = in.readLine();
PrintStream out1 = new PrintStream(new FileOutputStream(file1));
PrintStream out2 = new PrintStream(new FileOutputStream(file2));
System.out.print(" データの上限は? ");
up = Double.parseDouble(in.readLine());
System.out.print(" 刻み幅は? ");
h = Double.parseDouble(in.readLine());
// データ取得
ArrayList <Double> x1 = new ArrayList <Double> ();
ArrayList <Double> y1 = new ArrayList <Double> ();
ArrayList <Double> y2 = new ArrayList <Double> ();
for (x = 0; x < up+0.5*h; x += h) {
f = App.exponential(x, ram, pr);
out1.println(x + " " + pr[0]);
out2.println(x + " " + f);
x1.add(x);
y1.add(pr[0]);
y2.add(f);
}
// グラフの描画
graph(x1, y1, y2);
}
}
// %値
else {
System.out.print("%の値は? ");
x = Double.parseDouble(in.readLine());
p = 0.01 * x;
if (p < 1.0e-7)
System.out.println(x + "%値 = ∞ (母数 = " + ram + ")");
else {
f = App.p_exponential(sw1);
System.out.println(x + "%値 = " + f + " sw " + sw1[0] + " (母数 = " + ram + ")");
}
}
}
/*************************/
/* グラフの描画 */
/* x1 : x座標データ */
/* y1 : 密度関数 */
/* y2 : 分布関数 */
/*************************/
static void graph(ArrayList <Double> x1, ArrayList <Double> y1, ArrayList <Double> y2)
{
// 密度関数
String title1[]; // グラフ,x軸,及び,y軸のタイトル
String g_title1[]; // 凡例(グラフの内容)
double x_scale[]; // x軸目盛り
double y_scale1[]; // y軸目盛り
double data_x[][], data1_y[][]; // データ
int n = x1.size();
// グラフ,x軸,及び,y軸のタイトル
title1 = new String [3];
title1[0] = "密度関数(指数分布 λ:" + ram + ")";
title1[1] = "x";
title1[2] = "f(x)";
// 凡例
g_title1 = new String [1];
g_title1[0] = "密度関数";
// x軸目盛り
x_scale = new double[3];
double x_max = (x1.get(n-1)).doubleValue();
double x_step = x_max / 5;
int x_p = 0;
boolean ok = true;
int k = 0;
while (ok) {
if (x_step < 1.0) {
x_step *= 10;
k++;
}
else if (x_step >= 10.0) {
x_step /= 10;
k--;
}
else {
ok = false;
if (x_step-(int)x_step > 1.0e-5)
x_step = (int)x_step + 1;
else
x_step = (int)x_step;
if (k != 0) {
x_step = x_step * Math.pow(10, -k);
if (k > 0)
x_p = k;
}
double t = 0.0;
while (t < x_max-0.001*x_step)
t += x_step;
x_max = t;
}
}
x_scale[0] = 0.0; // 最小値
x_scale[1] = x_max; // 最大値
x_scale[2] = x_step; // 刻み幅
// y軸目盛り
y_scale1 = new double[3];
double y_max = ram;
double y_step = y_max / 5;
int y_p1 = 0;
ok = true;
k = 0;
while (ok) {
if (y_step < 1.0) {
y_step *= 10;
k++;
}
else if (y_step >= 10.0) {
y_step /= 10;
k--;
}
else {
ok = false;
if (y_step-(int)y_step > 1.0e-5)
y_step = (int)y_step + 1;
else
y_step = (int)y_step;
if (k != 0) {
y_step = y_step * Math.pow(10, -k);
if (k > 0)
y_p1 = k;
}
double t = 0.0;
while (t < y_max-0.001*y_step)
t += y_step;
y_max = t;
}
}
y_scale1[0] = 0.0; // 最小値
y_scale1[1] = y_max; // 最大値
y_scale1[2] = y_step; // 刻み幅
// データ
data_x = new double [1][n];
data1_y = new double [1][n];
for (int i1 = 0; i1 < n; i1++)
data_x[0][i1] = (x1.get(i1)).doubleValue();
for (int i1 = 0; i1 < n; i1++)
data1_y[0][i1] = (y1.get(i1)).doubleValue();
// 作図
LineGraph gp1 = new LineGraph(title1, g_title1, x_scale, x_p, y_scale1, y_p1, data_x, data1_y, true, false);
// 分布関数
String title2[]; // グラフ,x軸,及び,y軸のタイトル
String g_title2[]; // 凡例(グラフの内容)
double y_scale2[]; // y軸目盛り
double data2_y[][]; // データ
// グラフ,x軸,及び,y軸のタイトル
title2 = new String [3];
title2[0] = "分布関数(指数分布 λ:" + ram + ")";
title2[1] = "x";
title2[2] = "F(x)";
// 凡例
g_title2 = new String [1];
g_title2[0] = "分布関数";
// y軸目盛り
y_scale2 = new double[3];
int y_p2 = 1;
y_scale2[0] = 0.0; // 最小値
y_scale2[1] = 1.0; // 最大値
y_scale2[2] = 0.2; // 刻み幅
// データ
data2_y = new double [1][n];
for (int i1 = 0; i1 < n; i1++)
data2_y[0][i1] = (y2.get(i1)).doubleValue();
// 作図
LineGraph gp2 = new LineGraph(title2, g_title2, x_scale, x_p, y_scale2, y_p2, data_x, data2_y, true, false);
}
}
/****************/
/* 関数値の計算 */
/****************/
class Kansu {
private int sw;
// コンストラクタ
Kansu (int s) {sw = s;}
// double型関数
double snx(double x)
{
double y = 0.0;
switch (sw) {
// 関数値(f(x))の計算(指数分布)
case 0:
y = Test.p - Math.exp(-Test.ram * x);
break;
// 関数の微分の計算(指数分布)
case 1:
y = Test.ram * Math.exp(-Test.ram * x);
break;
}
return y;
}
}
/************************/
/* 科学技術系算用の手法 */
/************************/
class App {
/****************************************/
/* 指数分布の計算(P(X = x), P(X < x)) */
/* x : データ */
/* ram : 母数 */
/* z : P(X = x) */
/* return : P(X < x) */
/****************************************/
static double exponential(double x, double ram, double pr[])
{
double f = 0.0, y;
if (x < 0)
pr[0] = 0.0;
else {
y = Math.exp(-ram * x);
pr[0] = ram * y;
f = 1.0 - y;
}
return f;
}
/****************************************/
/* 指数分布のp%値(P(X > u) = 0.01p) */
/* ind : >=0 : normal(収束回数) */
/* =-1 : 収束しなかった */
/****************************************/
static double p_exponential(int ind[])
{
// ニュートン法
Kansu kn1 = new Kansu(0);
Kansu kn2 = new Kansu(1);
double xx = newton(0.0, 1.0e-6, 1.0e-10, 100, ind, kn1, kn2);
return xx;
}
/*****************************************************/
/* Newton法による非線形方程式(f(x)=0)の解 */
/* x1 : 初期値 */
/* eps1 : 終了条件1(|x(k+1)-x(k)|<eps1) */
/* eps2 : 終了条件2(|f(x(k))|<eps2) */
/* max : 最大試行回数 */
/* ind : 実際の試行回数 */
/* (負の時は解を得ることができなかった) */
/* kn1 : 関数を計算するクラスオブジェクト */
/* kn2 : 関数の微分を計算するクラスオブジェクト */
/* return : 解 */
/*****************************************************/
static double newton(double x1, double eps1, double eps2, int max,
int ind[], Kansu kn1, Kansu kn2)
{
double g, dg, x;
int sw;
x = x1;
ind[0] = 0;
sw = 0;
while (sw == 0 && ind[0] >= 0) {
ind[0]++;
sw = 1;
g = kn1.snx(x1);
if (Math.abs(g) > eps2) {
if (ind[0] <= max) {
dg = kn2.snx(x1);
if (Math.abs(dg) > eps2) {
x = x1 - g / dg;
if (Math.abs(x-x1) > eps1 && Math.abs(x-x1) > eps1*Math.abs(x)) {
x1 = x;
sw = 0;
}
}
else
ind[0] = -1;
}
else
ind[0] = -1;
}
}
return x;
}
}
/****************************/
/* 折れ線グラフの描画 */
/* coded by Y.Suganuma */
/****************************/
class LineGraph extends JFrame {
Draw_line pn;
/*********************************************************/
/* コンストラクタ(折れ線グラフ2) */
/* title_i : グラフ,x軸,及び,y軸のタイトル */
/* g_title_i : 凡例 */
/* x_scale_i : データの最小値,最大値,目盛幅(y) */
/* place_x_i : 小数点以下の桁数(x軸) */
/* y_scale_i : データの最小値,最大値,目盛幅(y) */
/* place_y_i : 小数点以下の桁数(y軸) */
/* data_x_i : グラフのデータ(x軸) */
/* data_y_i : グラフのデータ(y軸) */
/* d_t_i : タイトル表示の有無 */
/* d_g_i : 凡例表示の有無 */
/*********************************************************/
LineGraph(String title_i[], String g_title_i[], double x_scale_i[],
int place_x_i, double y_scale_i[], int place_y_i,
double data_x_i[][], double data_y_i[][], boolean d_t_i,
boolean d_g_i)
{
// JFrameクラスのコンストラクタの呼び出し
super("折れ線グラフ(2)");
// Windowサイズと表示位置を設定
int width = 900, height = 600; // Windowの大きさ(初期サイズ)
setSize(width, height);
Toolkit tool = getToolkit();
Dimension d = tool.getScreenSize();
setLocation(d.width / 2 - width / 2, d.height / 2 - height / 2);
// 描画パネル
Container cp = getContentPane();
pn = new Draw_line(title_i, g_title_i, x_scale_i, place_x_i, y_scale_i, place_y_i, data_x_i, data_y_i, d_t_i, d_g_i, this);
cp.add(pn);
// ウィンドウを表示
setVisible(true);
// イベントアダプタ
addWindowListener(new WinEnd());
addComponentListener(new ComponentResize());
}
/**********************/
/* Windowのサイズ変化 */
/**********************/
class ComponentResize extends ComponentAdapter
{
public void componentResized(ComponentEvent e)
{
pn.repaint();
}
}
/************/
/* 終了処理 */
/************/
class WinEnd extends WindowAdapter
{
public void windowClosing(WindowEvent e) {
setVisible(false);
}
}
}
class Draw_line extends JPanel {
String title[]; // グラフのタイトル
String g_title[]; // 凡例(グラフの内容)
String x_title[]; // x軸への表示
double x_scale[]; // y軸目盛り
double y_scale[]; // y軸目盛り
double data_x[][], data_y[][]; // データ
boolean d_t; // タイトル表示の有無
boolean d_g; // 凡例表示の有無
boolean type = true; // 横軸が項目かデータか
boolean ver = true; // 縦か横か
int place_x; // 小数点以下の桁数(x軸)
int place_y; // 小数点以下の桁数(y軸)
int width = 900, height = 600; // Windowの大きさ(初期サイズ)
int bx1, bx2, by1, by2; // 表示切り替えボタンの位置
LineGraph line;
String change = "横 色"; // 表示切り替えボタン
float line_w = 1.0f; // 折れ線グラフ等の線の太さ
boolean line_m = true; // 折れ線グラフ等にマークを付けるか否か
Color cl[] = {Color.black, Color.magenta, Color.blue, Color.orange, Color.cyan,
Color.pink, Color.green, Color.yellow, Color.darkGray, Color.red}; // グラフの色
int n_g; // グラフの数
/*********************************************************/
/* コンストラクタ(折れ線グラフ2) */
/* title_i : グラフ,x軸,及び,y軸のタイトル */
/* g_title_i : 凡例 */
/* x_scale_i : データの最小値,最大値,目盛幅(y) */
/* place_x_i : 小数点以下の桁数(x軸) */
/* y_scale_i : データの最小値,最大値,目盛幅(y) */
/* place_y_i : 小数点以下の桁数(y軸) */
/* data_x_i : グラフのデータ(x軸) */
/* data_y_i : グラフのデータ(y軸) */
/* d_t_i : タイトル表示の有無 */
/* d_g_i : 凡例表示の有無 */
/*********************************************************/
Draw_line(String title_i[], String g_title_i[], double x_scale_i[],
int place_x_i, double y_scale_i[], int place_y_i,
double data_x_i[][], double data_y_i[][], boolean d_t_i,
boolean d_g_i, LineGraph line_i)
{
// 背景色
setBackground(Color.white);
// テーブルデータの保存
title = title_i;
g_title = g_title_i;
x_scale = x_scale_i;
place_x = place_x_i;
y_scale = y_scale_i;
place_y = place_y_i;
data_x = data_x_i;
data_y = data_y_i;
d_t = d_t_i;
d_g = d_g_i;
type = false;
line = line_i;
// イベントアダプタ
addMouseListener(new ClickMouse(this));
}
/********/
/* 描画 */
/********/
public void paintComponent (Graphics g)
{
super.paintComponent(g); // 親クラスの描画(必ず必要)
double r, x1, y1, sp;
int i1, i2, cr, k, k_x, k_y, k1, k2, kx, kx1, ky, ky1, han, len;
int x_l, x_r, y_u, y_d; // 描画領域
int f_size; // フォントサイズ
int n_p; // データの数
String s1;
Font f;
FontMetrics fm;
Graphics2D g2 = (Graphics2D)g;
//
// Windowサイズの取得
//
Insets insets = line.getInsets();
Dimension d = line.getSize();
width = d.width - (insets.left + insets.right);
height = d.height - (insets.top + insets.bottom);
x_l = insets.left + 10;
x_r = d.width - insets.right - 10;
y_u = 20;
y_d = d.height - insets.bottom - insets.top;
//
// グラフタイトルの表示
//
r = 0.05; // タイトル領域の割合
f_size = ((y_d - y_u) < (x_r - x_l)) ? (int)((y_d - y_u) * r) : (int)((x_r - x_l) * r);
if (f_size < 5)
f_size = 5;
if (d_t) {
f = new Font("TimesRoman", Font.BOLD, f_size);
g.setFont(f);
fm = g.getFontMetrics(f);
len = fm.stringWidth(title[0]);
g.drawString(title[0], (x_l+x_r)/2-len/2, y_d-f_size/2);
y_d -= f_size;
}
//
// 表示切り替えボタンの設置
//
f_size = (int)(0.8 * f_size);
if (f_size < 5)
f_size = 5;
f = new Font("TimesRoman", Font.PLAIN, f_size);
fm = g.getFontMetrics(f);
g.setFont(f);
g.setColor(Color.yellow);
len = fm.stringWidth(change);
bx1 = x_r - len - 7 * f_size / 10;
by1 = y_u - f_size / 2;
bx2 = bx1 + len + f_size / 2;
by2 = by1 + 6 * f_size / 5;
g.fill3DRect(bx1, by1, len+f_size/2, 6*f_size/5, true);
g.setColor(Color.black);
g.drawString(change, x_r-len-f_size/2, y_u+f_size/2);
//
// 凡例の表示
//
n_g = g_title.length;
if (d_g) {
han = 0;
for (i1 = 0; i1 < n_g; i1++) {
len = fm.stringWidth(g_title[i1]);
if (len > han)
han = len;
}
han += 15;
r = 0.2; // 凡例領域の割合
k1 = (int)((x_r - x_l) * r);
if (han > k1)
han = k1;
kx = x_r - han;
ky = y_u + 3 * f_size / 2;
k = 0;
g2.setStroke(new BasicStroke(7.0f));
for (i1 = 0; i1 < n_g; i1++) {
g.setColor(cl[k]);
g.drawLine(kx, ky, kx+10, ky);
g.setColor(Color.black);
g.drawString(g_title[i1], kx+15, ky+2*f_size/5);
k++;
if (k >= cl.length)
k = 0;
ky += f_size;
}
g2.setStroke(new BasicStroke(1.0f));
x_r -= (han + 10);
}
else
x_r -= (int)(0.03 * (x_r - x_l));
//
// x軸及びy軸のタイトルの表示
//
if (ver) { // 縦
if (title[1].length() > 0 && !title[1].equals("-")) {
len = fm.stringWidth(title[1]);
g.drawString(title[1], (x_l+x_r)/2-len/2, y_d-4*f_size/5);
y_d -= 7 * f_size / 4;
}
else
y_d -= f_size / 2;
if (title[2].length() > 0 && !title[2].equals("-")) {
g.drawString(title[2], x_l, y_u+f_size/2);
y_u += f_size;
}
}
else { // 横
if (title[2].length() > 0 && !title[2].equals("-")) {
len = fm.stringWidth(title[2]);
g.drawString(title[2], (x_l+x_r)/2-len/2, y_d-4*f_size/5);
y_d -= 7 * f_size / 4;
}
else
y_d -= f_size / 2;
if (title[1].length() > 0 && !title[1].equals("-")) {
g.drawString(title[1], x_l, y_u+f_size/2);
y_u += f_size;
}
}
//
// x軸,y軸,及び,各軸の目盛り
//
f_size = (int)(0.8 * f_size);
if (f_size < 5)
f_size = 5;
f = new Font("TimesRoman", Font.PLAIN, f_size);
fm = g.getFontMetrics(f);
y_d -= 3 * f_size / 2;
k_y = (int)((y_scale[1] - y_scale[0]) / (0.99 * y_scale[2]));
k_x = 0;
if (!type)
k_x = (int)((x_scale[1] - x_scale[0]) / (0.99 * x_scale[2]));
g.setFont(f);
DecimalFormat df_x, df_y;
df_x = new DecimalFormat("#");
df_y = new DecimalFormat("#");
if (!type) {
if (place_x != 0) {
s1 = "#.";
for (i1 = 0; i1 < place_x; i1++)
s1 += "0";
df_x = new DecimalFormat(s1);
}
}
if (place_y != 0) {
s1 = "#.";
for (i1 = 0; i1 < place_y; i1++)
s1 += "0";
df_y = new DecimalFormat(s1);
}
// 縦表示
if (ver) {
// y軸
y1 = y_scale[0];
len = 0;
for (i1 = 0; i1 < k_y+1; i1++) {
s1 = df_y.format(y1);
k1 = fm.stringWidth(s1);
if (k1 > len)
len = k1;
y1 += y_scale[2];
}
g.drawLine(x_l+len+5, y_u, x_l+len+5, y_d);
g.drawLine(x_r, y_u, x_r, y_d);
y1 = y_scale[0];
x1 = y_d;
sp = (double)(y_d - y_u) / k_y;
for (i1 = 0; i1 < k_y+1; i1++) {
ky = (int)Math.round(x1);
s1 = df_y.format(y1);
k1 = fm.stringWidth(s1);
g.drawString(s1, x_l+len-k1, ky+f_size/2);
g.drawLine(x_l+len+5, ky, x_r, ky);
y1 += y_scale[2];
x1 -= sp;
}
x_l += (len + 5);
// x軸
if (type) {
n_p = x_title.length;
sp = (double)(x_r - x_l) / n_p;
x1 = x_l + sp / 2.0;
for (i1 = 0; i1 < n_p; i1++) {
kx = (int)Math.round(x1);
k1 = fm.stringWidth(x_title[i1]);
g.drawString(x_title[i1], kx-k1/2, y_d+6*f_size/5);
g.drawLine(kx, y_d, kx, y_d-5);
x1 += sp;
}
}
else {
x1 = x_scale[0];
y1 = x_l;
sp = (double)(x_r - x_l) / k_x;
for (i1 = 0; i1 < k_x+1; i1++) {
kx = (int)Math.round(y1);
s1 = df_x.format(x1);
k1 = fm.stringWidth(s1);
g.drawString(s1, kx-k1/2, y_d+6*f_size/5);
g.drawLine(kx, y_d, kx, y_u);
x1 += x_scale[2];
y1 += sp;
}
}
}
// 横表示
else {
// y軸
if (type) {
n_p = x_title.length;
len = 0;
for (i1 = 0; i1 < n_p; i1++) {
k1 = fm.stringWidth(x_title[i1]);
if (k1 > len)
len = k1;
}
g.drawLine(x_l+len+5, y_u, x_l+len+5, y_d);
g.drawLine(x_r, y_u, x_r, y_d);
sp = (double)(y_d - y_u) / n_p;
x1 = y_d - sp / 2.0;
for (i1 = 0; i1 < n_p; i1++) {
ky = (int)Math.round(x1);
k1 = fm.stringWidth(x_title[n_p-1-i1]);
g.drawString(x_title[n_p-1-i1], x_l+len-k1, ky+f_size/2);
g.drawLine(x_l+len+5, ky, x_l+len+10, ky);
x1 -= sp;
}
g.drawLine(x_l+len+5, y_u, x_r, y_u);
g.drawLine(x_l+len+5, y_d, x_r, y_d);
x_l += (len + 5);
}
else {
y1 = x_scale[0];
len = 0;
for (i1 = 0; i1 < k_x+1; i1++) {
s1 = df_x.format(y1);
k1 = fm.stringWidth(s1);
if (k1 > len)
len = k1;
y1 += x_scale[2];
}
g.drawLine(x_l+len+5, y_u, x_l+len+5, y_d);
g.drawLine(x_r, y_u, x_r, y_d);
y1 = x_scale[0];
x1 = y_d;
sp = (double)(y_d - y_u) / k_x;
for (i1 = 0; i1 < k_x+1; i1++) {
ky = (int)Math.round(x1);
s1 = df_x.format(y1);
k1 = fm.stringWidth(s1);
g.drawString(s1, x_l+len-k1, ky+f_size/2);
g.drawLine(x_l+len+5, ky, x_r, ky);
y1 += x_scale[2];
x1 -= sp;
}
x_l += (len + 5);
}
// x軸
x1 = y_scale[0];
y1 = x_l;
sp = (double)(x_r - x_l) / k_y;
for (i1 = 0; i1 < k_y+1; i1++) {
kx = (int)Math.round(y1);
s1 = df_y.format(x1);
k1 = fm.stringWidth(s1);
g.drawString(s1, kx-k1/2, y_d+6*f_size/5);
g.drawLine(kx, y_d, kx, y_u);
x1 += y_scale[2];
y1 += sp;
}
}
//
// グラフの表示
//
g2.setStroke(new BasicStroke(line_w));
cr = (int)line_w + 6;
// 縦表示
if (ver) {
if (type) {
n_p = x_title.length;
sp = (double)(x_r - x_l) / n_p;
k1 = 0;
for (i1 = 0; i1 < n_g; i1++) {
g.setColor(cl[k1]);
x1 = x_l + sp / 2.0;
kx1 = 0;
ky1 = 0;
for (i2 = 0; i2 < n_p; i2++) {
kx = (int)Math.round(x1);
ky = y_d - (int)((y_d - y_u) * (data_y[i1][i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
if (line_m)
g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
if (i2 > 0)
g.drawLine(kx1, ky1, kx, ky);
kx1 = kx;
ky1 = ky;
x1 += sp;
}
k1++;
if (k1 >= cl.length)
k1 = 0;
}
}
else {
n_p = data_x[0].length;
k1 = 0;
for (i1 = 0; i1 < n_g; i1++) {
g.setColor(cl[k1]);
kx1 = 0;
ky1 = 0;
for (i2 = 0; i2 < n_p; i2++) {
kx = x_l + (int)((x_r - x_l) * (data_x[i1][i2] - x_scale[0]) / (x_scale[1] - x_scale[0]));
ky = y_d - (int)((y_d - y_u) * (data_y[i1][i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
if (line_m)
g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
if (i2 > 0)
g.drawLine(kx1, ky1, kx, ky);
kx1 = kx;
ky1 = ky;
}
k1++;
if (k1 >= cl.length)
k1 = 0;
}
}
}
// 横表示
else {
if (type) {
n_p = x_title.length;
sp = (double)(y_d - y_u) / n_p;
k1 = 0;
for (i1 = 0; i1 < n_g; i1++) {
g.setColor(cl[k1]);
y1 = y_d - sp / 2.0;
kx1 = 0;
ky1 = 0;
for (i2 = 0; i2 < n_p; i2++) {
ky = (int)Math.round(y1);
kx = x_l + (int)((x_r - x_l) * (data_y[i1][n_p-1-i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
if (line_m)
g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
if (i2 > 0)
g.drawLine(kx1, ky1, kx, ky);
kx1 = kx;
ky1 = ky;
y1 -= sp;
}
k1++;
if (k1 >= cl.length)
k1 = 0;
}
}
else {
n_p = data_x[0].length;
k1 = 0;
for (i1 = 0; i1 < n_g; i1++) {
g.setColor(cl[k1]);
kx1 = 0;
ky1 = 0;
for (i2 = 0; i2 < n_p; i2++) {
kx = x_l + (int)((x_r - x_l) * (data_y[i1][i2] - y_scale[0]) / (y_scale[1] - y_scale[0]));
ky = y_d - (int)((y_d - y_u) * (data_x[i1][i2] - x_scale[0]) / (x_scale[1] - x_scale[0]));
if (line_m)
g.fillOval(kx-cr/2, ky-cr/2, cr, cr);
if (i2 > 0)
g.drawLine(kx1, ky1, kx, ky);
kx1 = kx;
ky1 = ky;
}
k1++;
if (k1 >= cl.length)
k1 = 0;
}
}
}
g2.setStroke(new BasicStroke(1.0f));
}
/************************************/
/* マウスがクリックされたときの処理 */
/************************************/
class ClickMouse extends MouseAdapter
{
Draw_line dr;
ClickMouse(Draw_line dr1)
{
dr = dr1;
}
public void mouseClicked(MouseEvent e)
{
int xp = e.getX();
int yp = e.getY();
// 縦表示と横表示の変換
if (xp > bx1 && xp < bx1+(bx2-bx1)/2 && yp > by1 && yp < by2) {
if (ver) {
ver = false;
change = "縦 色";
}
else {
ver = true;
change = "横 色";
}
repaint();
}
// グラフの色,線の太さ等
else if (xp > bx1+(bx2-bx1)/2 && xp < bx2 && yp > by1 && yp < by2) {
Modify md = new Modify(dr.line, dr);
md.setVisible(true);
}
}
}
}
/****************************/
/* 色及び線の太さの変更 */
/* coded by Y.Suganuma */
/****************************/
class Modify extends JDialog implements ActionListener, TextListener {
Draw_line dr; // 折れ線グラフ
JButton bt_dr;
TextField rgb[];
TextField r[];
TextField g[];
TextField b[];
JTextField tx;
JRadioButton r1, r2;
float line_w = 1.0f; // 折れ線グラフ等の線の太さ
boolean line_m = true; // 折れ線グラフ等にマークを付けるか否か
Color cl[]; // グラフの色
int n_g; // グラフの数
int wd; // 線の太さを変更するか
int mk; // マークを変更するか
int n;
JPanel jp[];
// 折れ線グラフ
Modify(Frame host, Draw_line dr1)
{
super(host, "色と線の変更", true);
// 初期設定
dr = dr1;
wd = 1;
mk = 1;
n_g = dr.n_g;
if (n_g > 10)
n_g = 10;
n = n_g + 3;
line_w = dr.line_w;
line_m = dr.line_m;
cl = new Color[n_g];
for (int i1 = 0; i1 < n_g; i1++)
cl[i1] = dr.cl[i1];
set();
// ボタン
Font f = new Font("TimesRoman", Font.BOLD, 20);
bt_dr = new JButton("OK");
bt_dr.setFont(f);
bt_dr.addActionListener(this);
jp[n-1].add(bt_dr);
}
// 設定
void set()
{
setSize(450, 60*(n));
Container cp = getContentPane();
cp.setBackground(Color.white);
cp.setLayout(new GridLayout(n, 1, 5, 5));
jp = new JPanel[n];
for (int i1 = 0; i1 < n; i1++) {
jp[i1] = new JPanel();
cp.add(jp[i1]);
}
Font f = new Font("TimesRoman", Font.BOLD, 20);
// 色の変更
JLabel lb[][] = new JLabel[n_g][3];
rgb = new TextField[n_g];
r = new TextField[n_g];
g = new TextField[n_g];
b = new TextField[n_g];
for (int i1 = 0; i1 < n_g; i1++) {
rgb[i1] = new TextField(3);
rgb[i1].setFont(f);
rgb[i1].setBackground(new Color(cl[i1].getRed(), cl[i1].getGreen(), cl[i1].getBlue()));
jp[i1].add(rgb[i1]);
lb[i1][0] = new JLabel(" 赤");
lb[i1][0].setFont(f);
jp[i1].add(lb[i1][0]);
r[i1] = new TextField(3);
r[i1].setFont(f);
r[i1].setBackground(Color.white);
r[i1].setText(Integer.toString(cl[i1].getRed()));
r[i1].addTextListener(this);
jp[i1].add(r[i1]);
lb[i1][1] = new JLabel("緑");
lb[i1][1].setFont(f);
jp[i1].add(lb[i1][1]);
g[i1] = new TextField(3);
g[i1].setFont(f);
g[i1].setBackground(Color.white);
g[i1].setText(Integer.toString(cl[i1].getGreen()));
g[i1].addTextListener(this);
jp[i1].add(g[i1]);
lb[i1][2] = new JLabel("青");
lb[i1][2].setFont(f);
jp[i1].add(lb[i1][2]);
b[i1] = new TextField(3);
b[i1].setFont(f);
b[i1].setBackground(Color.white);
b[i1].setText(Integer.toString(cl[i1].getBlue()));
b[i1].addTextListener(this);
jp[i1].add(b[i1]);
}
// 線の変更
if (wd > 0) {
JLabel lb1 = new JLabel("線の太さ:");
lb1.setFont(f);
jp[n_g].add(lb1);
tx = new JTextField(2);
tx.setFont(f);
tx.setBackground(Color.white);
tx.setText(Integer.toString((int)line_w));
jp[n_g].add(tx);
}
if (mk > 0) {
JLabel lb2 = new JLabel("マーク:");
lb2.setFont(f);
jp[n-2].add(lb2);
ButtonGroup gp = new ButtonGroup();
r1 = new JRadioButton("付ける");
r1.setFont(f);
gp.add(r1);
jp[n-2].add(r1);
r2 = new JRadioButton("付けない");
r2.setFont(f);
gp.add(r2);
jp[n-2].add(r2);
if (line_m)
r1.doClick();
else
r2.doClick();
}
}
// TextFieldの内容が変更されたときの処理
public void textValueChanged(TextEvent e)
{
for (int i1 = 0; i1 < n_g; i1++) {
if (e.getSource() == r[i1] || e.getSource() == g[i1] || e.getSource() == b[i1]) {
String str = r[i1].getText();
int rc = str.length()>0 ? Integer.parseInt(str) : 0;
str = g[i1].getText();
int gc = str.length()>0 ? Integer.parseInt(str) : 0;
str = b[i1].getText();
int bc = str.length()>0 ? Integer.parseInt(str) : 0;
rgb[i1].setBackground(new Color(rc, gc, bc));
}
}
}
// 値の設定
public void actionPerformed(ActionEvent e)
{
for (int i1 = 0; i1 < n_g; i1++) {
String str = r[i1].getText();
int rc = str.length()>0 ? Integer.parseInt(str) : 0;
str = g[i1].getText();
int gc = str.length()>0 ? Integer.parseInt(str) : 0;
str = b[i1].getText();
int bc = str.length()>0 ? Integer.parseInt(str) : 0;
dr.cl[i1] = new Color(rc, gc, bc);
}
dr.line_w = Integer.parseInt(tx.getText());
if (r1.isSelected())
dr.line_m = true;
else
dr.line_m = false;
dr.repaint();
setVisible(false);
}
}
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>指数分布</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<SCRIPT TYPE="text/javascript">
method = 0;
ram = 0.0;
p = 0.0;
function main()
{
ram = parseFloat(document.getElementById("ram").value);
let z = new Array();
let sw1 = new Array();
// 1点
if (method == 0) {
let x = parseFloat(document.getElementById("x").value);
let y = exponential(x, ram, z);
let str = "P(X = " + x + ") = " + z[0] + "\n";
str = str + "P(X < " + x + ") = " + y;
document.getElementById("xx").value = str;
}
// 複数点
else if (method == 1) {
// データ取得
let from = parseFloat(document.getElementById("from").value);
let to = parseFloat(document.getElementById("to").value);
let h = parseFloat(document.getElementById("h").value);
let x1 = new Array();
let y1 = new Array();
let y2 = new Array();
let mx = 0;
let n = 0;
for (let x = from; x < to+0.5*h; x += h) {
let y = exponential(x, ram, z);
x1[n] = x;
y1[n] = z[0];
y2[n] = y;
if (z[0] > mx)
mx = z[0];
n++;
}
let str1 = "密度関数\n";
let str2 = "分布関数\n";
for (let i1 = 0; i1 < n; i1++) {
str1 = str1 + x1[i1] + " " + y1[i1] + "\n";
str2 = str2 + x1[i1] + " " + y2[i1] + "\n";
}
document.getElementById("xx").value = str1;
document.getElementById("yy").value = str2;
// グラフの描画
let gp1 = "2,指数分布(密度関数),x,f(x),1,グラフ1," + from + "," + to;
let sp1 = (to - from) / 5;
let sp2 = Math.floor(mx / 5 * 100);
if (sp2 % 5 > 0)
sp2 = sp2 + 5 - sp2 % 5;
sp2 /= 100;
mx = sp2 * 5;
gp1 = gp1 + "," + sp1 + ",1,0.0," + mx + "," + sp2 + ",2," + n;
for (let i1 = 0; i1 < n; i1++)
gp1 = gp1 + "," + x1[i1];
for (let i1 = 0; i1 < n; i1++)
gp1 = gp1 + "," + y1[i1];
gp1 = gp1 + ",1,0";
let str = "graph_js.htm?gp=" + gp1;
open(str, "density", "width=950, height=700");
let gp2 = "2,指数分布(分布関数),x,F(x),1,グラフ1," + from + "," + to;
sp2 = 0.2;
mx = 1.0;
gp2 = gp2 + "," + sp1 + ",1,0.0," + mx + "," + sp2 + ",1," + n;
for (let i1 = 0; i1 < n; i1++)
gp2 = gp2 + "," + x1[i1];
for (let i1 = 0; i1 < n; i1++)
gp2 = gp2 + "," + y2[i1];
gp2 = gp2 + ",1,0";
str = "graph_js.htm?gp=" + gp2;
open(str, "distribution", "width=950, height=700");
}
// %値
else {
let x = parseFloat(document.getElementById("p").value);
p = x / 100.0;
if (p < 1.0e-7) {
str = x + "%値 = ∞";
document.getElementById("xx").value = str;
}
else {
let y = p_exponential(sw1);
if (sw1[0] < 0)
document.getElementById("xx").value = "収束しませんでした";
else {
str = x + "%値 = " + y;
document.getElementById("xx").value = str;
}
}
}
}
/****************/
/* 関数値の計算 */
/****************/
function snx(sw, x)
{
let y = 0.0;
switch (sw) {
// 関数値(f(x))の計算(指数分布)
case 1:
y = p - Math.exp(-ram * x);
break;
// 関数の微分の計算(指数分布)
case 2:
y = ram * Math.exp(-ram * x);
break;
}
return y;
}
/****************************************/
/* 指数分布の計算(P(X = x), P(X < x)) */
/* x : データ */
/* ram : 母数 */
/* z : P(X = x) */
/* return : P(X < x) */
/****************************************/
function exponential(x, ram, pr)
{
let f = 0.0;
let y;
if (x < 0)
pr[0] = 0.0;
else {
y = Math.exp(-ram * x);
pr[0] = ram * y;
f = 1.0 - y;
}
return f;
}
/****************************************/
/* 指数分布のp%値(P(X > u) = 0.01p) */
/* ind : >=0 : normal(収束回数) */
/* =-1 : 収束しなかった */
/****************************************/
function p_exponential(ind)
{
// ニュートン法
let xx = newton(0.0, 1.0e-6, 1.0e-10, 100, ind);
return xx;
}
/*****************************************************/
/* Newton法による非線形方程式(f(x)=0)の解 */
/* x1 : 初期値 */
/* eps1 : 終了条件1(|x(k+1)-x(k)|<eps1) */
/* eps2 : 終了条件2(|f(x(k))|<eps2) */
/* max : 最大試行回数 */
/* ind : 実際の試行回数 */
/* (負の時は解を得ることができなかった) */
/* return : 解 */
/*****************************************************/
function newton(x1, eps1, eps2, max, ind)
{
let g;
let dg;
let x;
let sw;
x = x1;
ind[0] = 0;
sw = 0;
while (sw == 0 && ind[0] >= 0) {
ind[0]++;
sw = 1;
g = snx(1, x1);
if (Math.abs(g) > eps2) {
if (ind[0] <= max) {
dg = snx(2, x1);
if (Math.abs(dg) > eps2) {
x = x1 - g / dg;
if (Math.abs(x-x1) > eps1 && Math.abs(x-x1) > eps1*Math.abs(x)) {
x1 = x;
sw = 0;
}
}
else
ind[0] = -1;
}
else
ind[0] = -1;
}
}
return x;
}
/********/
/* 目的 */
/********/
function set_m()
{
let sel = document.getElementById("method");
for (let i1 = 0; i1 < 3; i1++) {
if (sel.options[i1].selected) {
method = i1;
break;
}
}
// 1点
if (method == 0) {
document.getElementById("x_t").style.display = "";
document.getElementById("p_t").style.display = "none";
document.getElementById("from_t").style.display = "none";
document.getElementById("to_t").style.display = "none";
document.getElementById("h_t").style.display = "none";
document.getElementById("yy").style.display = "none";
}
// 複数点
else if (method == 1) {
document.getElementById("x_t").style.display = "none";
document.getElementById("p_t").style.display = "none";
document.getElementById("from_t").style.display = "";
document.getElementById("to_t").style.display = "";
document.getElementById("h_t").style.display = "";
document.getElementById("yy").style.display = "";
}
// %値
else {
document.getElementById("x_t").style.display = "none";
document.getElementById("p_t").style.display = "";
document.getElementById("from_t").style.display = "none";
document.getElementById("to_t").style.display = "none";
document.getElementById("h_t").style.display = "none";
document.getElementById("yy").style.display = "none";
}
}
</SCRIPT>
</HEAD>
<BODY STYLE="font-size: 130%; text-align:center; background-color: #eeffee;">
<H2 STYLE="text-align:center"><B>指数分布</B></H2>
λ:<INPUT ID="ram" STYLE="font-size: 100%" TYPE="text" SIZE="3" VALUE="1.0">
<SPAN ID="x_t">x:<INPUT ID="x" STYLE="font-size: 100%;" TYPE="text" SIZE="3" VALUE="1.0"> </SPAN>
<SPAN ID="p_t" STYLE="display: none">%値:<INPUT ID="p" STYLE="font-size: 100%;" TYPE="text" SIZE="3" VALUE="5.0"> </SPAN>
<SPAN ID="from_t" STYLE="display: none">下限:<INPUT ID="from" DISABLED STYLE="font-size: 100%;" TYPE="text" SIZE="3" VALUE="0.0"> </SPAN>
<SPAN ID="to_t" STYLE="display: none">上限:<INPUT ID="to" STYLE="font-size: 100%;" TYPE="text" SIZE="3" VALUE="5.0"> </SPAN>
<SPAN ID="h_t" STYLE="display: none">刻み幅:<INPUT ID="h" STYLE="font-size: 100%;" TYPE="text" SIZE="3" VALUE="0.1"> </SPAN>
目的:<SELECT ID="method" onChange="set_m()" STYLE="font-size:100%">
<OPTION SELECTED>確率(1点)
<OPTION>確率(複数点)
<OPTION>p%値
</SELECT>
<BUTTON STYLE="font-size: 100%; background-color: pink" onClick="main()">OK</BUTTON><BR><BR>
<TEXTAREA ID="xx" COLS="40" ROWS="30" STYLE="font-size: 100%"></TEXTAREA>
<TEXTAREA ID="yy" COLS="40" ROWS="30" STYLE="font-size: 100%; display: none"></TEXTAREA>
</BODY>
</HTML>
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>グラフの表示</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<LINK REL="stylesheet" TYPE="text/css" HREF="../master.css">
<SCRIPT TYPE="text/javascript" SRC="graph.js"></SCRIPT>
<SCRIPT TYPE="text/javascript">
function GetParameter()
{
let result = new Array();
if(1 < window.location.search.length) {
// 最初の1文字 (?記号) を除いた文字列を取得する
let str = window.location.search.substring(1);
// 区切り記号 (&) で文字列を配列に分割する
let param = str.split('&');
for(let i1 = 0; i1 < param.length; i1++ ) {
// パラメータ名とパラメータ値に分割する
let element = param[i1].split('=');
let Name = decodeURIComponent(element[0]);
let Value = decodeURIComponent(element[1]);
// パラメータ名をキーとして連想配列に追加する
result[Name] = Value;
}
}
return result;
}
</SCRIPT>
</HEAD>
<BODY CLASS="white" STYLE="text-align: center">
<DIV ID="cl_line" STYLE="text-align: center; display: none">
<FORM>
<DIV ID="c0">
<INPUT ID="rgb0" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r0" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(0)">
緑<INPUT ID="g0" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(0)">
青<INPUT ID="b0" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(0)">
</DIV>
<DIV ID="c1">
<INPUT ID="rgb1" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r1" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(1)">
緑<INPUT ID="g1" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(1)">
青<INPUT ID="b1" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(1)">
</DIV>
<DIV ID="c2">
<INPUT ID="rgb2" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r2" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(2)">
緑<INPUT ID="g2" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(2)">
青<INPUT ID="b2" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(2)">
</DIV>
<DIV ID="c3">
<INPUT ID="rgb3" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r3" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(3)">
緑<INPUT ID="g3" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(3)">
青<INPUT ID="b3" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(3)">
</DIV>
<DIV ID="c4">
<INPUT ID="rgb4" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r4" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(4)">
緑<INPUT ID="g4" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(4)">
青<INPUT ID="b4" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(4)">
</DIV>
<DIV ID="c5">
<INPUT ID="rgb5" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r5" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(5)">
緑<INPUT ID="g5" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(5)">
青<INPUT ID="b5" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(5)">
</DIV>
<DIV ID="c6">
<INPUT ID="rgb6" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r6" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(6)">
緑<INPUT ID="g6" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(6)">
青<INPUT ID="b6" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(6)">
</DIV>
<DIV ID="c7">
<INPUT ID="rgb7" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r7" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(7)">
緑<INPUT ID="g7" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(7)">
青<INPUT ID="b7" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(7)">
</DIV>
<DIV ID="c8">
<INPUT ID="rgb8" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r8" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(8)">
緑<INPUT ID="g8" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(8)">
青<INPUT ID="b8" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(8)">
</DIV>
<DIV ID="c9">
<INPUT ID="rgb9" TYPE="text" SIZE="3" STYLE="font-size: 90%">
赤<INPUT ID="r9" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(9)">
緑<INPUT ID="g9" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(9)">
青<INPUT ID="b9" TYPE="text" SIZE="1" STYLE="font-size: 90%" onblur="c_change(9)">
</DIV>
<DIV ID="line_m">
マーク:<INPUT ID="l_m1" TYPE="radio" NAME="mark" STYLE="font-size: 90%" CHECKED>付ける
<INPUT ID="l_m2" TYPE="radio" NAME="mark" STYLE="font-size: 90%">付けない
</DIV>
<DIV ID="line_w">
線の太さ:<INPUT ID="l_w" TYPE="text" SIZE="3" STYLE="font-size: 90%" VALUE="2">
</DIV>
<DIV>
<SPAN STYLE="background-color: pink; font-size: 100%" onClick="D_Change()">OK</SPAN>
</DIV>
</FORM>
</DIV>
<BR>
<DIV STYLE="text-align: center">
<CANVAS ID="canvas_e" STYLE="background-color: #eeffee;" WIDTH="900" HEIGHT="600" onClick="Click(event)"></CANVAS>
</DIV>
<SCRIPT TYPE="text/javascript">
let result = GetParameter();
graph(result['gp']);
</SCRIPT>
</BODY>
</HTML>
<?php
/****************************/
/* 指数分布の計算 */
/* coded by Y.Suganuma */
/****************************/
/****************************************/
/* 指数分布の計算(P(X = x), P(X < x)) */
/* x : データ */
/* ram : 母数 */
/* pr : P(X = x) */
/* return : P(X < x) */
/****************************************/
function exponential($x, $ram, &$pr)
{
$f = 0.0;
if ($x < 0)
$pr = 0.0;
else {
$y = exp(-$ram * $x);
$pr = $ram * $y;
$f = 1.0 - $y;
}
return $f;
}
/******************************/
/* P(X > x) - 1 + p(関数値) */
/******************************/
function exponential_f($x)
{
global $p, $ram;
return $p - exp(-$ram * $x);
}
/**************************/
/* P(X = x)(関数の微分) */
/**************************/
function exponential_df($x)
{
global $ram;
return $ram * exp(-$ram * $x);
}
/*****************************************************/
/* Newton法による非線形方程式(f(x)=0)の解 */
/* f : f(x)を計算する関数名 */
/* df : f(x)の微分を計算する関数名 */
/* x0 : 初期値 */
/* eps1 : 終了条件1(|x(k+1)-x(k)|<eps1) */
/* eps2 : 終了条件2(|f(x(k))|<eps2) */
/* max : 最大試行回数 */
/* ind : 実際の試行回数 */
/* (負の時は解を得ることができなかった) */
/* return : 解 */
/*****************************************************/
function newton($f, $df, $x0, $eps1, $eps2, $max, &$ind)
{
$x1 = $x0;
$x = $x1;
$ind = 0;
$sw = 0;
while ($sw == 0 && $ind >= 0) {
$sw = 1;
$ind += 1;
$g = $f($x1);
if (abs($g) > $eps2) {
if ($ind <= $max) {
$dg = $df($x1);
if (abs($dg) > $eps2) {
$x = $x1 - $g / $dg;
if (abs($x-$x1) > $eps1 && abs($x-$x1) > $eps1*abs($x)) {
$x1 = $x;
$sw = 0;
}
}
else
$ind = -1;
}
else
$ind = -1;
}
}
return $x;
}
/********/
/* main */
/********/
printf("母数は? ");
fscanf(STDIN, "%lf", $ram);
printf("目的とする結果は? \n");
printf(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)\n");
printf(" =1 : p%値( P(X > u) = 0.01p となるuの値) ");
fscanf(STDIN, "%d", $sw);
if ($sw == 0) {
printf("グラフ出力?(=1: yes, =0: no) ");
fscanf(STDIN, "%d", $sw);
// 密度関数と分布関数の値
if ($sw == 0) {
printf(" データは? ");
fscanf(STDIN, "%lf", $x);
$f = exponential($x, $ram, $pr);
printf("P(X = %f) = %f, P( X < %f) = %f (母数 = %f)\n", $x, $pr, $x, $f, $ram);
}
// グラフ出力
else {
printf(" 密度関数のファイル名は? ");
fscanf(STDIN, "%s", $file1);
printf(" 分布関数のファイル名は? ");
fscanf(STDIN, "%s", $file2);
$out1 = fopen($file1, "wb");
$out2 = fopen($file2, "wb");
printf(" データの上限は? ");
fscanf(STDIN, "%lf", $up);
printf(" 刻み幅は? ");
fscanf(STDIN, "%lf", $h);
for ($x = 0; $x < $up+0.5*$h; $x += $h) {
$f = exponential($x, $ram, $pr);
fwrite($out1, $x." ".$pr."\n");
fwrite($out2, $x." ".$f."\n");
}
}
}
// %値
else {
printf("%の値は? ");
fscanf(STDIN, "%lf", $x);
$p = 0.01 * $x;
if ($p < 1.0e-7)
printf("%f%値 = ∞ (母数 = %f)\n", $x, $ram);
else {
$f = newton("exponential_f", "exponential_df", 0.0, 1.0e-6, 1.0e-10, 100, $sw);
printf("%f%値 = %f sw %d (母数 = %f)\n", $x, $f, $sw, $ram);
}
}
?>
############################
# 指数分布の計算
# coded by Y.Suganuma
############################
##########################################
# 指数分布の計算(P(X = x), P(X < x))
# x : データ
# ram : 母数
# pr : P(X = x)
# return : P(X < x)
##########################################
def exponential(x, ram, pr)
f = 0.0
if x < 0
pr[0] = 0.0
else
y = Math.exp(-ram * x)
pr[0] = ram * y
f = 1.0 - y
end
return f
end
############################################
# Newton法による非線形方程式(f(x)=0)の解
# x0 : 初期値
# eps1 : 終了条件1(|x(k+1)-x(k)|<eps1)
# eps2 : 終了条件2(|f(x(k))|<eps2)
# max : 最大試行回数
# ind : 実際の試行回数
# (負の時は解を得ることができなかった)
# fn : f(x)とその微分を計算する関数名
# return : 解
# coded by Y.Suganuma
############################################
def newton(x0, eps1, eps2, max, ind, &fn)
x1 = x0
x = x1
ind[0] = 0
sw = 0
while sw == 0 and ind[0] >= 0
sw = 1
ind[0] += 1
g = fn.call(0, x1)
if g.abs() > eps2
if ind[0] <= max
dg = fn.call(1, x1)
if dg.abs() > eps2
x = x1 - g / dg
if (x-x1).abs() > eps1 && (x-x1).abs() > eps1*x.abs()
x1 = x
sw = 0
end
else
ind[0] = -1
end
else
ind[0] = -1
end
end
end
return x
end
#################################
# 関数値(f(x))とその微分の計算
#################################
exp_snx = Proc.new { |sw, x|
if sw == 0
$p - Math.exp(-$ram * x)
else
$ram * Math.exp(-$ram * x)
end
}
# 密度関数と分布関数の値
print("母数は? ")
$ram = Float(gets())
print("目的とする結果は?\n")
print(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)\n")
print(" =1 : p%値( P(X > u) = 0.01p となるuの値) ")
sw = Integer(gets())
pr = Array.new(1)
if sw == 0
print("グラフ出力?(=1: yes, =0: no) ")
sw = Integer(gets())
if sw == 0
# 密度関数と分布関数の値
print(" データは? ")
x = Float(gets())
f = exponential(x, $ram, pr)
print("P(X = " + String(x) + ") = " + String(pr[0]) + ", P( X < " + String(x) + ") = " + String(f) + " (母数 = " + String($ram) + ")\n")
# グラフ出力
else
print(" 密度関数のファイル名は? ")
file1 = gets().strip()
print(" 分布関数のファイル名は? ")
file2 = gets().strip()
print(" データの上限は? ")
up = Integer(gets())
print(" 刻み幅は? ")
h = Float(gets())
out1 = open(file1, "w")
out2 = open(file2, "w")
x = 0
while x < up+0.5*h
f = exponential(x, $ram, pr)
out1.print(String(x) + " " + String(pr[0]) + "\n")
out2.print(String(x) + " " + String(f) + "\n")
x += h
end
out1.close()
out2.close()
end
# %値
else
print("%の値は? ")
x = Float(gets())
$p = 0.01 * x
if $p < 1.0e-7
print(String(x) + "%値 = ∞ (母数 = " + String($ram) + ")\n")
else
ok = Array.new(1)
f = newton(0.0, 1.0e-6, 1.0e-10, 100, ok, &exp_snx)
print(String(x) + "%値 = " + String(f) + " (母数 = " + String($ram) + ")\n")
end
end
# -*- coding: UTF-8 -*-
import numpy as np
import sys
from math import *
##########################################
# 指数分布の計算(P(X = x), P(X < x))
# x : データ
# ram : 母数
# pr : P(X = x)
# return : P(X < x)
##########################################
def exponential(x, ram, pr) :
f = 0.0
if x < 0 :
pr[0] = 0.0
else :
y = exp(-ram * x)
pr[0] = ram * y
f = 1.0 - y
return f
############################################
# Newton法による非線形方程式(f(x)=0)の解
# fn : f(x)を計算する関数名
# dfn : f(x)の微分を計算する関数名
# x0 : 初期値
# eps1 : 終了条件1(|x(k+1)-x(k)|<eps1)
# eps2 : 終了条件2(|f(x(k))|<eps2)
# max : 最大試行回数
# ind : 実際の試行回数
# (負の時は解を得ることができなかった)
# return : 解
# coded by Y.Suganuma
############################################
def newton(fn, dfn, x0, eps1, eps2, max, ind) :
x1 = x0
x = x1
ind[0] = 0
sw = 0
while sw == 0 and ind[0] >= 0 :
sw = 1
ind[0] += 1
g = fn(x1)
if abs(g) > eps2 :
if ind[0] <= max :
dg = dfn(x1)
if abs(dg) > eps2 :
x = x1 - g / dg
if abs(x-x1) > eps1 and abs(x-x1) > eps1*abs(x) :
x1 = x
sw = 0
else :
ind[0] = -1
else :
ind[0] = -1
return x
############################
# 指数分布の計算
# coded by Y.Suganuma
############################
############################
# P(X > x) - 1 + p(関数値)
############################
def exponential_f(x) :
return p - exp(-ram * x)
############################
# P(X = x)(関数の微分)
############################
def exponential_df(x) :
return ram * exp(-ram * x)
# 密度関数と分布関数の値
s = input("母数は? ")
ram = float(s)
print("目的とする結果は? ")
print(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)")
s = input(" =1 : p%値( P(X > u) = 0.01p となるuの値) ")
sw = int(s)
pr = np.empty(1, np.float)
if sw == 0 :
s = input("グラフ出力?(=1: yes, =0: no) ")
sw = int(s)
if sw == 0 :
# 密度関数と分布関数の値
s = input(" データは? ")
x = float(s)
f = exponential(x, ram, pr)
print("P(X = " + str(x) + ") = " + str(pr[0]) + ", P( X < " + str(x) + ") = " + str(f) + " (母数 = " + str(ram) + ")")
# グラフ出力
else :
file1 = input(" 密度関数のファイル名は? ")
file2 = input(" 分布関数のファイル名は? ")
s = input(" データの上限は? ")
up = float(s)
s = input(" 刻み幅は? ")
h = float(s)
out1 = open(file1, "w")
out2 = open(file2, "w")
x = 0
while x < up+0.5*h :
f = exponential(x, ram, pr)
out1.write(str(x) + " " + str(pr[0]) + "\n")
out2.write(str(x) + " " + str(f) + "\n")
x += h
out1.close()
out2.close()
# %値
else :
s = input("%の値は? ")
x = float(s)
p = 0.01 * x
if p < 1.0e-7 :
print(str(x) + "%値 = ∞ (母数 = " + str(ram) + ")")
else :
ok = np.empty(1, np.int)
f = newton(exponential_f, exponential_df, 0.0, 1.0e-6, 1.0e-10, 100, ok)
print(str(x) + "%値 = " + str(f) + " (母数 = " + str(ram) + ")")
/****************************/
/* 指数分布の計算 */
/* coded by Y.Suganuma */
/****************************/
using System;
using System.IO;
class Program
{
static void Main()
{
Test1 ts = new Test1();
}
}
class Test1
{
public double p; // α%値を計算するとき時α/100を設定
public double ram; // 母数
/********/
/* main */
/********/
public Test1()
{
Console.Write("母数は? ");
ram = double.Parse(Console.ReadLine());
Console.WriteLine("目的とする結果は? ");
Console.WriteLine(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)");
Console.Write(" =1 : p%値( P(X > u) = 0.01p となるuの値) ");
int sw = int.Parse(Console.ReadLine());
if (sw == 0) {
Console.Write("グラフ出力?(=1: yes, =0: no) ");
sw = int.Parse(Console.ReadLine());
double f, pr = 0.0;
// 密度関数と分布関数の値
if (sw == 0) {
Console.Write(" データは? ");
double x = double.Parse(Console.ReadLine());
f = exponential(x, ram, ref pr);
Console.WriteLine("P(X = " + x + ") = " + pr + ", P( X < " + x + ") = " + f + " (母数 = " + ram + ")");
}
// グラフ出力
else {
Console.Write(" 密度関数のファイル名は? ");
String file1 = Console.ReadLine();
Console.Write(" 分布関数のファイル名は? ");
String file2 = Console.ReadLine();
Console.Write(" データの上限は? ");
double up = double.Parse(Console.ReadLine());
Console.Write(" 刻み幅は? ");
double h = double.Parse(Console.ReadLine());
StreamWriter out1 = new StreamWriter(file1);
StreamWriter out2 = new StreamWriter(file2);
// データ取得
for (double x = 0; x < up+0.5*h; x += h) {
f = exponential(x, ram, ref pr);
out1.WriteLine(x + " " + pr);
out2.WriteLine(x + " " + f);
}
out1.Close();
out2.Close();
}
}
// %値
else {
Console.Write("%の値は? ");
double x = double.Parse(Console.ReadLine());
p = 0.01 * x;
if (p < 1.0e-7)
Console.WriteLine(x + "%値 = ∞ (母数 = " + ram + ")");
else {
int sw1 = 0;
double f = newton(0.0, 1.0e-6, 1.0e-10, 100, ref sw1, exponential_f, exponential_df);
Console.WriteLine(x + "%値 = " + f + " sw " + sw1 + " (母数 = " + ram + ")");
}
}
}
/****************************************/
/* 指数分布の計算(P(X = x), P(X < x)) */
/* x : データ */
/* ram : 母数 */
/* pr : P(X = x) */
/* return : P(X < x) */
/****************************************/
double exponential(double x, double ram, ref double pr)
{
double f = 0.0;
if (x < 0)
pr = 0.0;
else {
double y = Math.Exp(-ram * x);
pr = ram * y;
f = 1.0 - y;
}
return f;
}
/******************************/
/* P(X > x) - 1 + p(関数値) */
/******************************/
double exponential_f(double x)
{
return p - Math.Exp(-ram * x);
}
/**************************/
/* P(X = x)(関数の微分) */
/**************************/
double exponential_df(double x)
{
return ram * Math.Exp(-ram * x);
}
/*****************************************************/
/* Newton法による非線形方程式(f(x)=0)の解 */
/* x1 : 初期値 */
/* eps1 : 終了条件1(|x(k+1)-x(k)|<eps1) */
/* eps2 : 終了条件2(|f(x(k))|<eps2) */
/* max : 最大試行回数 */
/* ind : 実際の試行回数 */
/* (負の時は解を得ることができなかった) */
/* fn : 関数値を計算する関数 */
/* dfn : 関数の微分値を計算する関数 */
/* return : 解 */
/*****************************************************/
double newton(double x1, double eps1, double eps2, int max, ref int ind,
Func fn, Func dfn)
{
double x = x1;
int sw = 0;
ind = 0;
while (sw == 0 && ind >= 0) {
ind++;
sw = 1;
double g = fn(x1);
if (Math.Abs(g) > eps2) {
if (ind <= max) {
double dg = dfn(x1);
if (Math.Abs(dg) > eps2) {
x = x1 - g / dg;
if (Math.Abs(x-x1) > eps1 && Math.Abs(x-x1) > eps1*Math.Abs(x)) {
x1 = x;
sw = 0;
}
}
else
ind = -1;
}
else
ind = -1;
}
}
return x;
}
}
'**************************'
' 指数分布の計算 '
' coded by Y.Suganuma '
'**************************'
Imports System.IO
Module Test
Dim p As Double ' α%値を計算するとき時α/100を設定
Dim ram As Double ' 母数
Sub Main()
Console.Write("母数は? ")
ram = Double.Parse(Console.ReadLine())
Console.WriteLine("目的とする結果は? ")
Console.WriteLine(" =0 : 確率の計算( P(X = x) 及び P(X < x) の値)")
Console.Write(" =1 : p%値( P(X > u) = 0.01p となるuの値) ")
Dim sw As Integer = Integer.Parse(Console.ReadLine())
If sw = 0
Console.Write("グラフ出力?(=1: yes, =0: no) ")
sw = Integer.Parse(Console.ReadLine())
Dim f As Double
Dim pr As Double = 0.0
' 密度関数と分布関数の値
If sw = 0
Console.Write(" データは? ")
Dim x As Double = double.Parse(Console.ReadLine())
f = exponential(x, ram, pr)
Console.WriteLine("P(X = " & x & ") = " & pr & ", P( X < " & x & ") = " & f & " (母数 = " & ram & ")")
' グラフ出力
Else
Console.Write(" 密度関数のファイル名は? ")
Dim file1 As String = Console.ReadLine().Trim()
Console.Write(" 分布関数のファイル名は? ")
Dim file2 As String = Console.ReadLine().Trim()
Console.Write(" データの上限は? ")
Dim up As Double = Double.Parse(Console.ReadLine())
Console.Write(" 刻み幅は? ")
Dim h As Double = Double.Parse(Console.ReadLine())
Dim out1 As StreamWriter = new StreamWriter(file1)
Dim out2 As StreamWriter = new StreamWriter(file2)
' データ取得
Dim x As Double = 0
Do While x < up+0.5*h
f = exponential(x, ram, pr)
out1.WriteLine(x & " " & pr)
out2.WriteLine(x & " " & f)
x += h
Loop
out1.Close()
out2.Close()
End If
' %値
Else
Console.Write("%の値は? ")
Dim x As Double = Double.Parse(Console.ReadLine())
p = 0.01 * x
If p < 1.0e-7
Console.WriteLine(x & "%値 = ∞ (母数 = " & ram & ")")
Else
' P(X > x) - 1 + p(関数値)(ラムダ式)
Dim exponential_f = Function(v) As Double
Return p - Math.Exp(-ram * v)
End Function
' P(X = x)(関数の微分)(ラムダ式)
Dim exponential_df = Function(v) As Double
Return ram * Math.Exp(-ram * v)
End Function
Dim sw1 As Integer = 0
Dim f As Double = newton(0.0, 1.0e-6, 1.0e-10, 100, sw1, exponential_f, exponential_df)
Console.WriteLine(x & "%値 = " & f & " sw " & sw1 & " (母数 = " & ram & ")")
End If
End If
End Sub
'**************************************'
' 指数分布の計算(P(X = x), P(X < x)) '
' x : データ '
' ram1 : 母数 '
' pr : P(X = x) '
' return : P(X < x) '
'**************************************'
Function exponential(x As Double, ram1 As Double, ByRef pr As Double)
Dim f As Double = 0.0
If x < 0
pr = 0.0
Else
Dim y As Double = Math.Exp(-ram1 * x)
pr = ram1 * y
f = 1.0 - y
End If
Return f
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''
' Newton法による非線形方程式(f(x)=0)の解 '
' x1 : 初期値 '
' eps1 : 終了条件1(|x(k+1)-x(k)|<eps1) '
' eps2 : 終了条件2(|f(x(k))|<eps2) '
' max : 最大試行回数 '
' ind : 実際の試行回数 '
' (負の時は解を得ることができなかった) '
' fn : 関数値を計算する関数 '
' dfn : 関数の微分値を計算する関数 '
' return : 解 '
'''''''''''''''''''''''''''''''''''''''''''''''''''''
Function newton(x1 As Double, eps1 As Double, eps2 As Double, max As Integer,
ByRef ind As Integer,
fn As Func(Of Double, Double), dfn As Func(Of Double, Double))
Dim x As Double = x1
Dim sw As Integer = 0
ind = 0
Do While sw = 0 and ind >= 0
ind += 1
sw = 1
Dim g As Double = fn(x1)
If Math.Abs(g) > eps2
If ind <= max
Dim dg As Double = dfn(x1)
If Math.Abs(dg) > eps2
x = x1 - g / dg
If Math.Abs(x-x1) > eps1 and Math.Abs(x-x1) > eps1*Math.Abs(x)
x1 = x
sw = 0
End If
Else
ind = -1
End If
Else
ind = -1
End If
End If
Loop
Return x
End Function
End Module
| 情報学部 | 菅沼ホーム | 目次 | 索引 |