// QuadratureSum.java // // approximate integral f(x) from x=a to x=b by simple sum // f(x) = 4*(x-x^2) // a=0, b=1, n=8, h=(b-a)/n=1/8 import java.awt.*; import java.awt.event.*; import java.text.*; class QuadratureSum extends Frame { double a = 0.0; double b = 1.0; int n = 8; // regions, 9 points double h = (b-a)/n; QuadratureSum() { setTitle("QuadratureSum(x) 0 to 1 of 4(x-x^2)"); setSize(450,520); setBackground(Color.white); setForeground(Color.black); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); } double f(double x) // "exact" function { return 4.0*(x-x*x); } public void paint(Graphics g) { // set up function to be approximated final int ns=50; // number of samples for plotting final double nf=(double)n; double fx[] = new double[ns]; // f(x) final double xmin=a; final double xmax=b; double ymin=0.0; // for automatic scaling some day double ymax=1.0; double dx; // x step increment for plotting curve double xstart=a; // initial value double x; // stepped value xmin to xmax dx = (xmax-xmin)/(double)(ns-1); // do NOT use x=x+dx, very inaccurate! for(int i=0; i