delphi3
asked on
Automating a formula
Hi all,
This is not homework.
This is a JAVA program but I need to get the math formula to a better format.
I am looking for an improvement in expanding the formula(s) for finding the roots of a polynomial. The present setup is to use Horner's method and specifically show formulas that are for polynomials of degree 3 or 4. I would like to express the formula using any number of degree. And for the moment, I do not see how to create a general formula using Horner's method.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// hornersMethodDemo attempts to find solutions to polynomial equations that are of degree 2 or 3.
// Any terms that are missing are given coefficients 0. Step size is variable.
/* Sample problems:
p(x) = x^2-25 use coefficients 1,0,-25 separated by "," as shown; interval from -7 to 7
p(x) = x^2-8x+15 use for coefficients 1,-8,15 separated by "," as shown; interval from 0 to 7
p(x) = x^3-11x^2+14x+80 use for coefficients 1,-11,14,80 separated by "," as shown; interval from -10 to 10
p(x) = 0.3x^2-3.8x+12 use coefficients 0.3,-3.8,+12 separated by "," as shown; interval from 5 to 7
note:
any x value that is at one point is above the line y = 0 and the next is below the line y=0, there exist a root.
or similarly, any x value that is at one point is below the line y = 0 and the next is above the line y=0, there exist a root.
*/
// This example demonstrates the use of JLabel, JTextField
// and JButton.
public class hornersMethodDemo
implements ActionListener {
JFrame HornerFrame;
JPanel HornerPanel;
JTextField FromTField;
JTextField ToTField;
JTextField IncTField;
JTextField PolyCoefs;
JLabel FromLabel, ToLabel, IncLabel, PolyCoefLabel, CalcValueLabel;
JButton Calculate;
// Constructor
public hornersMethodDemo() {
// Create the frame and container.
HornerFrame = new JFrame("Looking For Roots");
HornerPanel = new JPanel();
HornerPanel.setLayout(new GridLayout(3, 2));
// Add the widgets.
addWidgets();
// Add the panel to the frame.
HornerFrame.getContentPane ().add(Hor nerPanel, BorderLayout.CENTER);
// Exit when the window is closed.
HornerFrame.setDefaultClos eOperation (JFrame.EX IT_ON_CLOS E);
// Show the frame.
HornerFrame.pack();
HornerFrame.setVisible(tru e);
}
// Create and add the widgets for converter.
private void addWidgets() {
// Create widgets.
FromTField = new JTextField();
ToTField= new JTextField();
IncTField = new JTextField();
PolyCoefs = new JTextField();
FromLabel = new JLabel("Evaluate X From", SwingConstants.LEFT);
ToLabel= new JLabel("Evaluate X To", SwingConstants.LEFT);
IncLabel = new JLabel("Step Size", SwingConstants.LEFT);
PolyCoefLabel = new JLabel("Enter Coefficients", SwingConstants.LEFT);
CalcValueLabel = new JLabel("See System Out For Value", SwingConstants.LEFT);
Calculate = new JButton("Calculate");
// Listen to events from Calc button.
Calculate.addActionListene r(this);
// Add widgets to container.
HornerPanel.add(FromLabel) ;
HornerPanel.add(FromTField );
HornerPanel.add( ToLabel);
HornerPanel.add(ToTField);
HornerPanel.add(IncLabel);
HornerPanel.add(IncTField) ;
HornerPanel.add(PolyCoefLa bel);
HornerPanel.add(PolyCoefs) ;
HornerPanel.add(Calculate) ;
HornerPanel.add(CalcValueL abel);
//HornerPanel.add(CalcValu e );
FromLabel.setBorder(Border Factory.cr eateEmptyB order(5, 5, 5, 5));
ToLabel.setBorder(BorderFa ctory.crea teEmptyBor der(5, 5, 5, 5));
IncLabel.setBorder(BorderF actory.cre ateEmptyBo rder(5, 5, 5, 5));
CalcValueLabel.setBorder(B orderFacto ry.createE mptyBorder (5, 5, 5, 5));
}
// Implementation of ActionListener interface.
public void actionPerformed(ActionEven t event) {
try {
float gofrmX = Float.parseFloat(FromTFiel d.getText( ));//from
float gotoX = Float.parseFloat(ToTField. getText()) ;//to
float stp = Float.parseFloat(IncTField .getText() );//step
String in = new String(PolyCoefs.getText() ); //coefficients in normal order
String[] atoms = in.split(",");
//P(x) = a0 + x *(a1 + x*(a2+x*(...x*an)...)): model
float y = 0;
int atmlength = atoms.length;
System.out.print(gofrmX + " ** is the first X point ** ");
while (gofrmX <= gotoX )
{
if (atmlength == 3) // 2nd degree
{
y = Float.parseFloat(atoms[ato ms.length- 1]) + gofrmX*(Float.parseFloat(a toms[atoms .length-2] ) + gofrmX*Float.parseFloat(at oms[atoms. length-3]) );
}
if (atmlength == 4) // 3rd degree
{
y = Float.parseFloat(atoms[ato ms.length- 1]) + gofrmX*(Float.parseFloat(a toms[atoms .length-2] ) + gofrmX*(Float.parseFloat(a toms[atoms .length-3] ) + gofrmX*Float.parseFloat(at oms[atoms. length-4]) ));
}
System.out.print("Value for y is " + y);
if( y < 0 ){
System.out.println(" and is below the line y = 0 ");
}
if( y == 0 ){
System.out.println(" and is on the line y = 0 and is a root ");
}
if( y > 0 ){
System.out.println(" and is above the line y = 0 ");
}
gofrmX = gofrmX + stp;
System.out.print(gofrmX + " ** is another step ** ");
}
}
catch (Exception e) {
//e.printStackTrace();
}
}
// main method
public static void main(String[] args) {
// Set the look and feel.
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatform LookAndFee lClassName ());
}
catch (Exception e) {}
hornersMethodDemo calculator = new hornersMethodDemo();
}
}
Any help is appreciated.
Delphi3
This is not homework.
This is a JAVA program but I need to get the math formula to a better format.
I am looking for an improvement in expanding the formula(s) for finding the roots of a polynomial. The present setup is to use Horner's method and specifically show formulas that are for polynomials of degree 3 or 4. I would like to express the formula using any number of degree. And for the moment, I do not see how to create a general formula using Horner's method.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// hornersMethodDemo attempts to find solutions to polynomial equations that are of degree 2 or 3.
// Any terms that are missing are given coefficients 0. Step size is variable.
/* Sample problems:
p(x) = x^2-25 use coefficients 1,0,-25 separated by "," as shown; interval from -7 to 7
p(x) = x^2-8x+15 use for coefficients 1,-8,15 separated by "," as shown; interval from 0 to 7
p(x) = x^3-11x^2+14x+80 use for coefficients 1,-11,14,80 separated by "," as shown; interval from -10 to 10
p(x) = 0.3x^2-3.8x+12 use coefficients 0.3,-3.8,+12 separated by "," as shown; interval from 5 to 7
note:
any x value that is at one point is above the line y = 0 and the next is below the line y=0, there exist a root.
or similarly, any x value that is at one point is below the line y = 0 and the next is above the line y=0, there exist a root.
*/
// This example demonstrates the use of JLabel, JTextField
// and JButton.
public class hornersMethodDemo
implements ActionListener {
JFrame HornerFrame;
JPanel HornerPanel;
JTextField FromTField;
JTextField ToTField;
JTextField IncTField;
JTextField PolyCoefs;
JLabel FromLabel, ToLabel, IncLabel, PolyCoefLabel, CalcValueLabel;
JButton Calculate;
// Constructor
public hornersMethodDemo() {
// Create the frame and container.
HornerFrame = new JFrame("Looking For Roots");
HornerPanel = new JPanel();
HornerPanel.setLayout(new GridLayout(3, 2));
// Add the widgets.
addWidgets();
// Add the panel to the frame.
HornerFrame.getContentPane
// Exit when the window is closed.
HornerFrame.setDefaultClos
// Show the frame.
HornerFrame.pack();
HornerFrame.setVisible(tru
}
// Create and add the widgets for converter.
private void addWidgets() {
// Create widgets.
FromTField = new JTextField();
ToTField= new JTextField();
IncTField = new JTextField();
PolyCoefs = new JTextField();
FromLabel = new JLabel("Evaluate X From", SwingConstants.LEFT);
ToLabel= new JLabel("Evaluate X To", SwingConstants.LEFT);
IncLabel = new JLabel("Step Size", SwingConstants.LEFT);
PolyCoefLabel = new JLabel("Enter Coefficients", SwingConstants.LEFT);
CalcValueLabel = new JLabel("See System Out For Value", SwingConstants.LEFT);
Calculate = new JButton("Calculate");
// Listen to events from Calc button.
Calculate.addActionListene
// Add widgets to container.
HornerPanel.add(FromLabel)
HornerPanel.add(FromTField
HornerPanel.add( ToLabel);
HornerPanel.add(ToTField);
HornerPanel.add(IncLabel);
HornerPanel.add(IncTField)
HornerPanel.add(PolyCoefLa
HornerPanel.add(PolyCoefs)
HornerPanel.add(Calculate)
HornerPanel.add(CalcValueL
//HornerPanel.add(CalcValu
FromLabel.setBorder(Border
ToLabel.setBorder(BorderFa
IncLabel.setBorder(BorderF
CalcValueLabel.setBorder(B
}
// Implementation of ActionListener interface.
public void actionPerformed(ActionEven
try {
float gofrmX = Float.parseFloat(FromTFiel
float gotoX = Float.parseFloat(ToTField.
float stp = Float.parseFloat(IncTField
String in = new String(PolyCoefs.getText()
String[] atoms = in.split(",");
//P(x) = a0 + x *(a1 + x*(a2+x*(...x*an)...)): model
float y = 0;
int atmlength = atoms.length;
System.out.print(gofrmX + " ** is the first X point ** ");
while (gofrmX <= gotoX )
{
if (atmlength == 3) // 2nd degree
{
y = Float.parseFloat(atoms[ato
}
if (atmlength == 4) // 3rd degree
{
y = Float.parseFloat(atoms[ato
}
System.out.print("Value for y is " + y);
if( y < 0 ){
System.out.println(" and is below the line y = 0 ");
}
if( y == 0 ){
System.out.println(" and is on the line y = 0 and is a root ");
}
if( y > 0 ){
System.out.println(" and is above the line y = 0 ");
}
gofrmX = gofrmX + stp;
System.out.print(gofrmX + " ** is another step ** ");
}
}
catch (Exception e) {
//e.printStackTrace();
}
}
// main method
public static void main(String[] args) {
// Set the look and feel.
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatform
}
catch (Exception e) {}
hornersMethodDemo calculator = new hornersMethodDemo();
}
}
Any help is appreciated.
Delphi3
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
rjkimble,
thanks for stopping by to view my Q and to respond.
I also read your well qualified backgound in the bio statement at EE, the one associated with your name. Very interesting.
I will take your advice and see what happens.
D3
thanks for stopping by to view my Q and to respond.
I also read your well qualified backgound in the bio statement at EE, the one associated with your name. Very interesting.
I will take your advice and see what happens.
D3
Thanks for your kind comments and for accepting my answer, but even though I am well qualified to answer mathematical questions, I'm not so sure that my answer is really the one you're seeking. I read at least one reference that suggested to me that you were looking for a way to take the input string and generate a method that computes the value of a polynomial with the indicated coefficients. That's what I was getting after. However, the MathWorld article shows that Horner's method is all about finding roots to polynomials, and that's a different thing altogether.
ASKER
Hi rjkimble,
I have pursued this matter further and a posted a related Q in the JAVA language section.
The history of "expertmb's" answer is noted in Q:
https://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_211315
58.html#12072270
the location of the items to be changed of my work above are:
.........
while (gofrmX <= gotoX )
{
if (atmlength == 3) // 2nd degree
{
y = Float.parseFloat(atoms[ato ms.length- 1]) +
gofrmX*(Float.parseFloat(a toms[atoms .length-2] ) +
gofrmX*Float.parseFloat(at oms[atoms. length-3]) );
}
if (atmlength == 4) // 3rd degree
{
y = Float.parseFloat(atoms[ato ms.length- 1]) +
gofrmX*(Float.parseFloat(a toms[atoms .length-2] ) +
gofrmX*(Float.parseFloat(a toms[atoms .length-3] ) +
gofrmX*Float.parseFloat(at oms[atoms. length-4]) ));
}
System.out.print("Value for y is " + y);
if( y < 0 ){
...........
the new replacement will be:
........
while (gofrmX <= gotoX )
{
y = 0;
float yC = Float.parseFloat(atoms[ato ms.length- 1]);// constant term
System.out.println ("Constant term "+ yC);
for(int i = atmlength; i >1 ; i--){
y = (y + (Float.parseFloat(atoms[at oms.length -i])))*gof rmX;
}
y= y + yC; // adding in the constant term
System.out.print ("total y = "+ y);
if( y < 0 ){
........
I am now able to do any number set of terms (well, maybe), thanks to EE expertmb's help
and yours was: "but you still have your work cut out for you for the rest of the technique."
I am sure that this whole problem and revisions will not buy anyone a cup of coffee at the local diner. :)
Delphi3
I have pursued this matter further and a posted a related Q in the JAVA language section.
The history of "expertmb's" answer is noted in Q:
https://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_211315
58.html#12072270
the location of the items to be changed of my work above are:
.........
while (gofrmX <= gotoX )
{
if (atmlength == 3) // 2nd degree
{
y = Float.parseFloat(atoms[ato
gofrmX*(Float.parseFloat(a
gofrmX*Float.parseFloat(at
}
if (atmlength == 4) // 3rd degree
{
y = Float.parseFloat(atoms[ato
gofrmX*(Float.parseFloat(a
gofrmX*(Float.parseFloat(a
gofrmX*Float.parseFloat(at
}
System.out.print("Value for y is " + y);
if( y < 0 ){
...........
the new replacement will be:
........
while (gofrmX <= gotoX )
{
y = 0;
float yC = Float.parseFloat(atoms[ato
System.out.println ("Constant term "+ yC);
for(int i = atmlength; i >1 ; i--){
y = (y + (Float.parseFloat(atoms[at
}
y= y + yC; // adding in the constant term
System.out.print ("total y = "+ y);
if( y < 0 ){
........
I am now able to do any number set of terms (well, maybe), thanks to EE expertmb's help
and yours was: "but you still have your work cut out for you for the rest of the technique."
I am sure that this whole problem and revisions will not buy anyone a cup of coffee at the local diner. :)
Delphi3
Thanks for the update, and good luck!
public static float horner( float x, String coefficents )
write some code that finds the last coefficient as a float, say c, and creates a new string, coefficients2, of the remaining ones. Then your return statement would look something like this:
return c + x * horner( x, coefficients2 );
if c is the last remaining coefficient, then just return c and you're done. I think that captures the idea behind Horner's method. This is just one approach. There are many others that do something similar.