// Estimatin of Roots by Exner Enterprises, C.2000 // Author: Nicholas Exner // http://www.uiuc.edu/ph/www/exner // Free to redistribute/publish/modify pending that // the above comments are retained. import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.URL; import java.util.*; public class Bisection extends Applet { Image offscreenImage; Graphics h; double a=0.0,b=0.0; String S_formula; String key; Label label_a = new Label("A: "); Label label_b = new Label("B: "); Label label_c = new Label("Formula: "); TextField inputA; TextField inputB; TextField inputC; TextArea ta = new TextArea("Ready...",15,40); StringBuffer buf; Button calculate = new Button("Calculate"); Choice d= new Choice(); public void init(){ inputA = new TextField(3); inputB = new TextField(3); inputC = new TextField(10); d.addItem("Bisection"); d.addItem("False Position"); d.addItem("Secant"); d.addItem("Newton"); add(label_a); add(inputA); add(label_b); add(inputB); add(label_c); add(inputC); add(d); add(calculate); add(ta); key = "Bisection"; buf = new StringBuffer(); setBackground(Color.white); repaint(); } public void paint(Graphics g) { // Paint Nothing } public boolean mouseUp(Event evt, int x, int y) { return true; } public void processButton() { buf = new StringBuffer(); //Clean off buffer double aa = 0.0; double bb = 0.0; String spacedString=""; try { aa = Double.valueOf(inputA.getText()).doubleValue(); bb = Double.valueOf(inputB.getText()).doubleValue(); spacedString = inputC.getText(); } catch (NumberFormatException e) { } S_formula = ""; for (int i=0;i < spacedString.length();i++) { if (spacedString.charAt(i) != ' ') S_formula += spacedString.charAt(i); } if (key.equals("Bisection")) { bisection(aa,bb); } if (key.equals("False Position")) { falsePosition(aa,bb); } if (key.equals("Secant")) { secant(aa,bb); } if (key.equals("Newton")) { newton(aa,bb); } System.out.println("Button Pressed"); ta.setText(buf.toString()); } //End Processbutton public boolean action (Event evt, Object arg) { if (evt.target instanceof Button) { processButton(); } if (evt.target instanceof Choice){ key=(String)arg; } return true; } public void update(Graphics g) { paint(g); } public void bisection(double a,double b) { double m = 0.0; while (Math.abs(b-a) > .00001) { buf.append("A: "+a+" \tB: "+b+"\n"); System.out.println("A: "+a+" B: "+b); m = (a+b)/2; // Check to see if root was hit at midpoint if (f(m) == 0) { a=m; b=m; } else if (f(a)*f(m)< 0) { b=m; } else { a=m; } } // End while buf.append("approximate root is "+((b-a)/2)); } // End Bisection public void falsePosition(double a,double b) { double xold = a, xn = b, m =0.0; while (Math.abs(xold-xn) > .00001) { xold = xn; buf.append("A: "+a+" \tB: "+b+"\n"); m = (a*f(b)-b*f(a))/(f(b)-f(a)); // Check to see if root was hit if (f(m) == 0) { a=m; b=m; } else if (f(a)*f(m)< 0) { b=m; } else { a=m; } xn=m; } // End while buf.append("approximate root is "+xn); } // End False Position public void newton(double a,double b) { double xn = 0.0, n = 0.0001, xi = a; buf.append("A: "+f(xi)+"\n"); xn = xi-(f(xi)/fdx(xi)); while (Math.abs(xn-xi) > .00001) { buf.append("A: "+f(xn)+"\n"); xi=xn; xn= xi-(f(xi)/fdx(xi)); } // End while buf.append("approximate root is "+xn); } // End Newton public void secant(double a,double b) { double xold = a, xn = b, m =0.0; while (Math.abs(a-b) > .00001) { m = (a*f(b)-b*f(a))/(f(b)-f(a)); a=b; b=m; buf.append("A: "+a+" \tB: "+b+"\n"); } // End while buf.append("approximate root is "+b); } // End Secant public double f(double x) { String s = S_formula, tempNum = "", tempNum2= ""; double total=0.0; boolean addStat = true; for (int i=0; i < s.length();i++) { if (isNum(s,i)){ tempNum+=s.charAt(i); } if (s.charAt(i) == 'x'){ double totalTemp; // Get coefficient double temp=1; if (!tempNum.equals("")){ temp = Double.valueOf(tempNum).doubleValue(); tempNum=""; } i++; if (i= '0' && (s.charAt(i)<= '9')) { return true; } else return false; } } // End class Bisection