Here we can visually understand the convergence of Cauchy sequence by entering the value of x and N. Definition: The function f(x,N,i) of two positive integers x, i and a rational number N is defined by
.
The function g(N,i) of a positive integer i and a rational number N is defined by
.
[•] is Gauss' symbol and r(x) is a positive square root of x. The vertical axis shows a sequence of values on the function of (x,N,i). The horizontal axis shows a sequence of values on the function of (N,i). The function of (x,N,i) has a minimum value in the assigned (i=1) condition and a maximum value in the assigned (i=1000) condition when the domain of i is between 1 and 1000.
The domain of x is over 0 and below 10^8, and the domain of N is over 0.1^16 and below 10^16. When you enter the numerical values of x and N, please push the enter key each time you enter x or N in the text box.
If you enter the value of x and N in other domains, the program automatically sets the variables such as x=10^5 and N=10^5.
source code: CauchySequence.java
import java.math.BigDecimal;
import java.math.BigInteger;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class CauchySequence extends Applet implements ActionListener{
private static final long serialVersionUID = -6724959421340457497L;
JTextField yx = new JTextField("100000");
JTextField yn = new JTextField("100000");
JLabel label1 = new JLabel("Convergence of Cauchy sequence", JLabel.CENTER);
JLabel label2 = new JLabel("A natural number, 0<x<10^8", JLabel.CENTER);
JLabel label3 = new JLabel("A rational number, 0.1^16<N<10^16", JLabel.CENTER);
// variable set
double x = 100000;
double n = 100000;
public void init(){
label1.setPreferredSize(new Dimension(416,24));
label1.setFont(new Font("Serif",Font.BOLD,14));
add(label1);
label2.setPreferredSize(new Dimension(170,23));
label2.setFont(new Font("Serif",Font.BOLD,11));
add(label2);
yx.setPreferredSize(new Dimension(80,23));
add(yx);
label3.setPreferredSize(new Dimension(210,23));
label3.setFont(new Font("Serif",Font.BOLD,11));
add(label3);
yn.setPreferredSize(new Dimension(120,23));
add(yn);
yx.addActionListener(this);
yn.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==yx){
x=Double.valueOf(yx.getText()).doubleValue();
if(x <= 0 || x >= Math.pow(10.0,8) || x!=(long)x){
x=100000;
}
}
if(e.getSource()==yn){
n = Double.valueOf(yn.getText()).doubleValue();
BigDecimal n0 = new BigDecimal(yn.getText());
BigDecimal n1 = new BigDecimal(Math.pow(0.1,16));
BigDecimal n2 = new BigDecimal(Math.pow(10.0,16));
int d1 = n0.compareTo(n1);
int d2 = n0.compareTo(n2);
if(d1==-1 || d1==0 || d2==0 || d2==1){
n=100000;
}
}
yx.setText(""+(long)x);
yn.setText(""+n);
repaint();
}
public void paint(Graphics g){
// variable set
int i;
double b, m, p, y, minp, maxp, minb, maxb;
BigDecimal a0, a2, b0, n0, p0, p1, x0;
BigInteger a1;
double[] pp = new double[1000];
double[] bb = new double[1000];
int[] xxx = new int[1000];
int[] yyy = new int[1000];
for(i=1;i<=1000;i++){
p = Math.exp(10-0.01*i);
m = n/p;
p0 = new BigDecimal(p);
n0 = new BigDecimal(n);
p1 = n0.divide(p0,30,BigDecimal.ROUND_HALF_EVEN);
pp[i-1] = Math.log(m);
y = Math.sqrt(x);
x0 = new BigDecimal(y);
a0 = x0.multiply(p1);
a1 = a0.toBigInteger();
a2 = new BigDecimal(a1);
b0 = a2.divide(p1,30,BigDecimal.ROUND_HALF_EVEN);
b = b0.doubleValue();
bb[i-1] = b;
}
minp = pp[0];
maxp = pp[0];
minb = bb[0];
maxb = bb[0];
for(i=0;i<=999;i++){
if(pp[i]>maxp){
maxp = pp[i];
}
}
for(i=0;i<=999;i++){
if(pp[i]<minp){
minp = pp[i];
}
}
for(i=0;i<=999;i++){
if(bb[i]>maxb){
maxb = bb[i];
}
}
for(i=0;i<=999;i++){
if(bb[i]<minb){
minb = bb[i];
}
}
for (i=0;i<=999;i++){
double xx = maxp-minp;
double yy = maxb-minb;
xxx[i] = (int)((pp[i]-minp)*(351/xx))+33;
yyy[i] = 441-(int)((bb[i]-minb)*(351/yy));
}
Graphics2D g2 = (Graphics2D)g;
GradientPaint gp1 = new GradientPaint(0, 0, new Color(154,181,228), 0,470,new Color(225,232,245), true);
g2.setPaint(gp1);
g2.fillRect(0,0,416,470);
super.paint(g);
GradientPaint gp2 = new GradientPaint(0, 33, new Color(225,232,245), 0,351,new Color(154,181,228), true);
g2.setPaint(gp2);
g2.fillRect(33,90,354,351);
g2.setColor (Color.black);
g2.setFont(new Font ("Serif",Font.PLAIN,11));
g2.drawString("f",15,95);
g2.drawString("(x,N,i)",1,108);
g2.drawString("g(N,i)",365,455);
for (i=0;i<=998;i++){
g.drawLine(xxx[i], yyy[i], xxx[i+1], yyy[i+1]);
}
}
}
I have recently begun to code the examples of numerical computation in Java and hope to make better use of the computational power and the graphical user interface.
コメントする