Question

In: Computer Science

In JAVA PLEASE With the mathematics you have studied so far in your education you have...

In JAVA PLEASE

With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to an equation f(x) = 0 for some function f. Use the bisection method. To solve the problem using this method first find two values of x, A and B, such that when evaluated in the function f(x) they give opposites signs for the y value. If f(x) is continuous between these two values then we know that there is at least one x which evaluates to a 0 y value, which is between these two values A and B. Treat the positive value as an upper bound and the negative value as a lower bound. Divide the space between A and B in half and evaluate the function at that new point.   If the value is positive than it replaces the existing upper-bound and if it is negative it replaces the existing lower-bound. Continue dividing the space between the upper-bound and lower-bound in half and evaluating this new value and generating new upper and lower bounds as the case may be. Continue the evaluation process until the x value that you are plugging into the function evaluates to a y value that is zero plus or minus .0000001.

                Consider the possibility of finding all the real roots of any given function up to and including x raised to the fifth power. Input should consist of reading the coefficients one at a time to the powers of x up to 5 and some constant.   Do a desk check with calculator on y = X2 -2 and identify the variables associated with that problem. Write the code that follows your algorithm. Then test your program on other polynomials such as 2x5 -15x4 + 35x3 -15x2-37x + 30 (roots are -1, 1, 2, 2.5, 3) and 3x5 -17x4 + 25x3 + 5x2 -28x + 12 (roots are -1,1, 2/3, 2, 3). Use at lest 3 methods. One to read the 5 coefficients, one to calculate the value of the polynomial and one to do the binary bisection search. Use the following for loop to work through the X values:for(double x = -5.0000001; x < 5.0000001; x = x + .1)

OUTPUT SHOULD BE LIKE THIS

---------------------------

Please enter the number of coefficients : 5

PLEASE ENTER 5TH COEFFICIENT

2

PLEASE ENTER 4TH COEFFICIENT

-15

PLEASE ENTER 3RD COEFFICIENT

35

PLEASE ENTER 2ND COEFFICIENT

-15

PLEASE ENTER 1ST COEFFICIENT

-37

PLEASE ENTER ZERO COEFFICIENT

30

root is -0.99999999999947171

root is 1.0000000004308118

root is 2.0000000019209287

root is 2.500000000019

root is 2.999999999989406

-------------------------------

JUST LIKE THAT, DO IT WITHOUT ENTERING ANY BRAKETING NUMERS. NO BRAKETING NUMERS, PLEASE.

Do what way you want, as Long As You got the Output like that.  

THANK YOU SO MUCH

Solutions

Expert Solution


import java.util.Scanner;

public class BetterBisection {

public static void main(String[] args) {
double a, b, c;
double f_of_a, f_of_b;

int highest_degree;
System.out.println("Please enter the number of coefficients ");
Scanner input = new Scanner(System.in);
highest_degree = input.nextInt();
for (int i = highest_degree; i >= 0; i--) {
int coeff_deg_i;

System.out.println("PLEASE ENTER "+i+"TH COEFFICIENT");
coeff_deg_i = input.poly_input(i);

}

do {
a = input();
b = input();
if (f(a) * f(b) >= 0) {
System.out
.println("Sorry the two numbers are not bracketing the root. Please try again ");
}
} while (f(a) * f(b) >= 0);
f_of_a = f(a);
f_of_b = f(b);
double root = bisectionMethod(f_of_a, f_of_b, a, b);
System.out.println("Root is : " + root);
}

public static double input() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a bracketing number");
return (input.nextDouble());
}

public static double bisectionMethod(double f_of_a, double f_of_b, double a,
double b) {
double c;
double f_of_c;
final double TOLERANCE = 0.0001;
while (Math.abs(a - b) > TOLERANCE) {
c = (a + b) / 2;
f_of_c = f(c);
if (f_of_c * f(a) == 0 || f_of_c * f(b) == 0) {
return c;
} else if (f_of_c * f(a) > 0) {
a = c;
} else {
b = c;
}
}
return (a + b) / 2;
}

public static int poly_input(int degree) {
System.out.println("Please enter coefficient for degree " + degree);
Scanner input = new Scanner(System.in);
int coefficient;
coefficient = input.nextInt();
return coefficient;
}
}



Related Solutions

In JAVA, please With the mathematics you have studied so far in your education you have...
In JAVA, please With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to an equation f(x) = 0 for some function f....
Ans In Java Please Homework 6-1 So far in this course, all of your programs have...
Ans In Java Please Homework 6-1 So far in this course, all of your programs have been written to be fully contained inside a single method named main. The main method is where Java begins execution of your program. In this assignment, you will coding other methods in addition to the main method. These additional methods will perform specific functions and, in most cases, return results. Write all your methods in a class named Homework6Methods.java Write a public static method...
One the basis of what you have studied so far, how would you distinguish the neoclassical...
One the basis of what you have studied so far, how would you distinguish the neoclassical investment function [Investment = f(cost of borrowed funds)] from that of Keynes [Investment = f(expected profit rate - the cost of borrowed funds)]? In other words, why would Say’s law which is a key feature of neoclassical macroeconomics, not work if the investment is a function of expected profits?
Which of the three ethical philosophies that we have studied so far imparts the greatest wisdom...
Which of the three ethical philosophies that we have studied so far imparts the greatest wisdom in your well-reasoned opinion? In answering this question, discuss the concrete implications that these theories have for particular issues in both personal and political ethics. Give the best reasons you can think of to justify your answers (for example, reasons that might persuade an open minded person who initially disagrees with you). (20 points <600 words) USE ARISTOTLE, THE HUMAN GOOD AND FUNCTION ARGUMENT
Please use the economics knowledge you have learned so far to analyze the case below. You...
Please use the economics knowledge you have learned so far to analyze the case below. You can propose your own questions and then answer them. Remember, there is no absolutely correct answer to this exercise. Its purpose is to provide you an opportunity to demonstrate your ability to think like an economist by applying economic principles to interpret the logic of a real-world phenomenon. The Fortunes of Delta Airlines Follow the Business Cycle Delta Airlines has grown into the world’s...
Please use the economics knowledge you have learned so far to analyze the case below. You...
Please use the economics knowledge you have learned so far to analyze the case below. You can propose your own questions and then answer them. Remember, there is no absolutely correct answer to this exercise. Its purpose is to provide you an opportunity to demonstrate your ability to think like an economist by applying economic principles to interpret the logic of a real-world phenomenon. Can Greece Function without Banks? In 2001, Greece and most other European countries abandoned their individual...
Please use the economics knowledge you have learned so far to analyze the case below. You...
Please use the economics knowledge you have learned so far to analyze the case below. You can propose your own questions and then answer them. Remember, there is no absolutely correct answer to this exercise. Its purpose is to provide you an opportunity to demonstrate your ability to think like an economist by applying economic principles to interpret the logic of a real-world phenomenon. Why Is JPMorgan Chase Laying Off Workers? Many workers lost their jobs during the 2007–2009 recession...
Please use the economics knowledge you have learned so far to analyze the case below. You...
Please use the economics knowledge you have learned so far to analyze the case below. You can propose your own questions and then answer them. Remember, there is no absolutely correct answer to this exercise. Its purpose is to provide you an opportunity to demonstrate your ability to think like an economist by applying economic principles to interpret the logic of a real-world phenomenon. Will Economic Reforms in Mexico Boost Economic Growth? More than 18,000 U.S. companies, including Fortune 500...
Please use the economics knowledge you have learned so far to analyze the case below. You...
Please use the economics knowledge you have learned so far to analyze the case below. You can propose your own questions and then answer them. Remember, there is no absolutely correct answer to this exercise. Its purpose is to provide you an opportunity to demonstrate your ability to think like an economist by applying economic principles to interpret the logic of a real-world phenomenon. Economic Growth and the Business Cycle at Corning, Inc. In 1851, Amory Houghton founded the company...
For this assignment, you will apply what you learned in analyzing Java™ code so far in...
For this assignment, you will apply what you learned in analyzing Java™ code so far in this course by writing your own Java™ program. The Java™ program you write should do the following: Accept user input that represents the number of sides in a polygon. Note: The code to do this is already written for you. If input value is not between 3 and 5, display an informative error message If input value is between 3 and 5, use a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT