正規分布

/*
	A basic extension of the java.applet.Applet class
 */
import java.awt.*;
import java.applet.*;
public class Applet1 extends Applet
{
    double x,y,a,b,s,x1,x2,x3;
    int n,i;
    
    
	public void init()
	{
		// Take out this line if you don't use 
symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
		symantec.itools.lang.Context.setApplet(this);
	
		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		setLayout(null);
		setSize(786,432);
		textField1.setText("1");
		add(textField1);
		textField1.setBounds(420,264,36,24);
		label2.setText(" 計算結果");
		add(label2);
		label2.setBackground(java.awt.Color.green);
		label2.setBounds(420,300,144,24);
		button1.setLabel("Start");
		add(button1);
		button1.setBackground(java.awt.Color.lightGray);
		button1.setBounds(468,264,52,21);
		//}}
	
		//{{REGISTER_LISTENERS
		SymAction lSymAction = new SymAction();
		SymText lSymText = new SymText();
		textField1.addTextListener(lSymText);
		textField1.addActionListener(lSymAction);
		button1.addActionListener(lSymAction);
		//}}
	}
	
	//{{DECLARE_CONTROLS
	java.awt.TextField textField1 = new java.awt.TextField();
	java.awt.Label label2 = new java.awt.Label();
	java.awt.Button button1 = new java.awt.Button();
	//}}
	
	public void paint(Graphics g)
    	{
		g.drawLine(0,200,800,200);
        	g.drawLine(300,10,300,250);
        	for (x=-4;x<4;x=x+0.03)
        	{
                 y=func(x);
			g.setColor(Color.blue);
                 g.drawLine((int)(100*x+300), 
(int)(-400*y+200), (int)(100*x+300), (int)(-400*y+200));
           }
            String x2=textField1.getText();
            x3=new Double(x2).doubleValue();
           for (x=0;x<x3;x=x+0.03)
           {
                y=func(x);
                g.drawLine((int)(100*x+300), 
200, (int)(100*x+300), (int)(-400*y+200));
           }
    }
            
public double func(double x)
{
    //return Math.sqrt(1-x*x);
    return 1/(Math.sqrt((2*Math.PI)))*Math.exp(-(x*x/2));//標準正規分布
}
	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == textField1)
				textField1_ActionPerformed(event);
			else if (object == button1)
				button1_ActionPerformed(event);
		}
	}
	class SymText implements java.awt.event.TextListener
	{
		public void textValueChanged(java.awt.event.TextEvent event)
		{
			Object object = event.getSource();
			if (object == textField1)
				textField1_TextValueChanged(event);
		}
	}
	void textField1_TextValueChanged(java.awt.event.TextEvent event)
	{
		// to do: code goes here.
		repaint();
			 
	}
	void textField1_ActionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
		repaint();	 
	}
	void button1_ActionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
		a = 0;
        b = x3;
        int n=5000;
        double h = (b - a) / (2 * n);
        x1 = a;
        s = 0;        
         for(i = 0;i<=n ;i++)
         {         
                x2 = x1 + h;
                x3 = x2 + h;
                s = s + (func(x1) + 4 * func(x2) + func(x3)) * h / 3;
//シンプソンの公式で確率を求める
                x1 = x3;
                int ind=(int)(i+2);
                    label2.setText(" "+s);
         }
          
	}
}

BACK >>