// The Bisection Method, coded by David Protas, c.2002
// Any corrections or suggestions for improvement of this code will be appreciated
// and should be sent to david.protas@csun.edu
import java.applet.*;
import java.awt.*;
public class Bi extends Applet
{
final int LINE_SPAN = 34;
Panel top, p1, p2, p3;
Label enterCoeffs;
Label polynomial;
Label x5, x4, x3, x2, x1;
Label enterEndPts;
Label leftEndPt, rightEndPt;
Label answer;
Label message;
TextField a5, a4, a3, a2, a1, a0;
TextField a, b;
TextArea results;
Button calculate;
Color babyBlue;
public void init()
{
setLayout(new BorderLayout());
top = new Panel();
top.setLayout(new GridLayout(6,1));
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
enterCoeffs = new Label("Enter the coefficients: ");
polynomial = new Label("p(x) =");
x5 = new Label("x^5 +");
x4 = new Label("x^4 +");
x3 = new Label("x^3 +");
x2 = new Label("x^2 +");
x1 = new Label("x +");
enterEndPts = new Label("Enter the endpoints:");
leftEndPt = new Label(" a =");
rightEndPt = new Label(" b =");
answer = new Label(" ");
message = new Label();
a5 = new TextField(3);
a4 = new TextField(3);
a3 = new TextField(3);
a2 = new TextField(3);
a1 = new TextField(3);
a0 = new TextField(3);
a = new TextField(3);
b = new TextField(3);
results = new TextArea(" Left Endpt Right Endpt ", 14, 64);
results.setEditable(false);
calculate = new Button("Calculate");
p1 = new Panel();
babyBlue = new Color(204,255,255);
top.add(enterCoeffs);
p1.add(polynomial);
p1.add(a5);
p1.add(x5);
p1.add(a4);
p1.add(x4);
p1.add(a3);
p1.add(x3);
p1.add(a2);
p1.add(x2);
p1.add(a1);
p1.add(x1);
p1.add(a0);
top.add(p1);
top.add(enterEndPts);
p2.add(leftEndPt);
p2.add(a);
p2.add(rightEndPt);
p2.add(b);
top.add(p2);
p3.add(calculate);
p3.add(answer);
top.add(p3);
top.add(message);
add("North",top);
add("Center",results);
setBackground(babyBlue);
enterCoeffs.setBackground(babyBlue);
p1.setBackground(babyBlue);
enterEndPts.setBackground(babyBlue);
p2.setBackground(babyBlue);
answer.setBackground(babyBlue);
p3.setBackground(babyBlue);
message.setForeground(Color.red);
message.setBackground(babyBlue);
results.setFont(new Font("Courier",Font.PLAIN,10));
polynomial.setBackground(babyBlue);
x5.setBackground(babyBlue);
x4.setBackground(babyBlue);
x3.setBackground(babyBlue);
x2.setBackground(babyBlue);
x1.setBackground(babyBlue);
leftEndPt.setBackground(babyBlue);
rightEndPt.setBackground(babyBlue);
}
public boolean action(Event evt, Object arg)
{
if (evt.target instanceof Button)
{
int i, leftMarkPosition, rightMarkPosition;
String a5S, a4S, a3S, a2S, a1S, a0S, aS, bS, root, leftBlank = "",
rightBlank = "", halfInterval, interval = "", line, leftMark, rightMark;
double a5D, a4D, a3D, a2D, a1D, a0D, aD, bD, cD = 1.0;
boolean displayLine = true;
answer.setText("");
results.setText(" Left Endpt Right Endpt ");
a5S = a5.getText();
a4S = a4.getText();
a3S = a3.getText();
a2S = a2.getText();
a1S = a1.getText();
a0S = a0.getText();
aS = a.getText();
bS = b.getText();
if (entryValid(a5S) && entryValid(a4S) && entryValid(a3S) && entryValid(a2S) &&
entryValid(a1S) && entryValid(a0S) && entryValid(aS) && entryValid(bS)) {
a5D = doubleFromString(a5S);
a4D = doubleFromString(a4S);
a3D = doubleFromString(a3S);
a2D = doubleFromString(a2S);
a1D = doubleFromString(a1S);
a0D = doubleFromString(a0S);
aD = doubleFromString(aS);
bD = doubleFromString(bS);
message.setText("");
if (aD>=bD)
message.setText("Need a < b. Enter endpoints again.");
else {
if (p(aD,a5D,a4D,a3D,a2D,a1D,a0D)*p(bD,a5D,a4D,a3D,a2D,a1D,a0D) >= 0)
message.setText("p(a) and p(b) have same sign. Try again.");
else {
for (i = 0; i < LINE_SPAN - 2; i++)
interval = interval + "_";
if (p(aD,a5D,a4D,a3D,a2D,a1D,a0D) > 0) {
leftMark = "+";
rightMark = "-";
}
else {
leftMark = "-";
rightMark = "+";
}
line = leftMark + interval + rightMark;
results.appendText
("\n" + rndOff(aD) + " " + rndOff(bD) + " " + line);
while (bD - aD > 0.00001) {
leftMarkPosition = line.indexOf(leftMark);
rightMarkPosition = line.indexOf(rightMark);
if (leftMarkPosition > 0)
leftBlank = line.substring(0, leftMarkPosition);
else
leftBlank = "";
interval = line.substring(leftMarkPosition + 1, rightMarkPosition);
if (rightMarkPosition < LINE_SPAN)
rightBlank = line.substring(rightMarkPosition + 1);
else
rightBlank = "";
if (interval.length() > 1)
halfInterval = interval.substring(interval.length()/2);
else
halfInterval = "";
cD =(aD + bD)/2;
if (Math.abs(p(cD,a5D,a4D,a3D,a2D,a1D,a0D)) < 0.000000001) {
message.setText
("Bisection point is a root. Bisection method terminated.");
break; //root found, break out of loop
}
if (p(aD,a5D,a4D,a3D,a2D,a1D,a0D)*p(cD,a5D,a4D,a3D,a2D,a1D,a0D) > 0) {
aD = cD;
line = leftBlank + halfInterval.replace('_',' ') + leftMark +
halfInterval + rightMark + rightBlank;
}
else {
bD = cD;
line = leftBlank + leftMark + halfInterval + rightMark +
halfInterval.replace('_',' ') + rightBlank;
}
if (displayLine == true) {
results.appendText("\n" + rndOff(aD) + " " + rndOff(bD) +
" " + line);
if (halfInterval.length() <= 1)
displayLine = false;
}
else
results.appendText("\n" + rndOff(aD) + " " + rndOff(bD));
} //end of while (bD - aD > 0.00001)
root = rndOff(cD);
answer.setText(" root = " + root);
} //end of else
} //end of else
} //end of if entryValid
else
message.setText("Invalid entry. Try again.");
return true;
} //end of if evt.target
return false;
} // end of action
private double p(double t,double a5D,double a4D,double a3D,
double a2D,double a1D,double a0D)
{
double value;
value = a5D*t*t*t*t*t+a4D*t*t*t*t+a3D*t*t*t+a2D*t*t+a1D*t+a0D;
return value;
}
public static double doubleFromString(String str)
{
Double doubleObj = new Double(str);
return doubleObj.doubleValue();
}
public static String rndOff(double number) //to 5 places past the decimal
{
String strnum, bigstrnum, substrnum = " 0.00000";
double rndnum;
int period, lngth;
long longnum;
if ((number >= 0.001) || (number <= -0.001)) {
rndnum = Math.pow(0.1,5)*Math.round(Math.pow(10,5)*number);
if (rndnum == 0)
strnum = "0.00000";
else
strnum = String.valueOf(rndnum);
bigstrnum = " " + strnum + " ";
period = bigstrnum.indexOf('.');
substrnum = bigstrnum.substring(period -4, period + 6);
}
else {
longnum = Math.round(Math.pow(10,5)*number);
if (longnum == 0)
substrnum = " 0.00000";
else {
strnum = String.valueOf(longnum);
if (longnum < 0)
strnum = strnum.substring(1);
lngth = strnum.length();
switch (lngth) {
case 1:
substrnum = " 0.0000" + strnum;
break;
case 2:
substrnum = " 0.000" + strnum;
break;
default:
substrnum = " error";
}
if (longnum < 0)
substrnum = " -" + substrnum.substring(3);
}
}
return substrnum;
}
private boolean entryValid(String entry)
{
boolean status;
try {
double number = doubleFromString(entry);
status = true;
}
catch(NumberFormatException e) {
status = false;
}
return status;
}
} //end of Bi
Back to applet