This is supposed to use the Newtonian Method of finding which value of x makes f(x) = 0. The beginning creates random numbers for a
6 through a
0 so that the function is as follows:
f(x) = a
6*x^6 + a
5*x^5 ... a
0*x^0
And the derivative is easy enough to figure out.
Newtonian's method a dynamic value of x that is represented as (x
in the function:
x: = x - f(x) / fprime(x)
The 'while' loop will continue looping until the value of f(x) (represented as double y) is less than the 0.001. In other words, it continues looping until it reaches a set accuracy. I did remember to set it so that f(x) is an absolute value so it doesn't loop more or less than it's supposed to.
The program works fine about half of the time, creating a valid approximation of x. However, the other half of the time, it goes into an infinite loop. I programmed this in an easy-teaching program/compiler/debugger called BlueJ, so the format may be a tad different.
Code:
import java.util.*;
public class Newton
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Scott Catlin (ScottALot)");
System.out.println("Program v1.0.0");
System.out.println("Bug Infested :D");
System.out.println("Enter up to seven values for a[0] to a[6]. Input 0 for values you would not like to include.");
System.out.println("All values can be easily edited in the source code. Simply replace the asix - azero values with your respective values.");
double asix = Math.random();
double afive = Math.random();
double afour = Math.random();
double athree = Math.random();
double atwo = Math.random();
double aone = Math.random();
double azero = Math.random();
System.out.println("");
System.out.println(asix+"X^6 + "+afive+"X^5 + "+afour+"X^4 + "+athree+"X^3 + "+atwo+"X^2 + "+aone+"X^1 + "+azero+"X^0");
System.out.println("");
System.out.println("Enter in a random x-value");
double x = input.nextDouble();
double y = asix*Math.pow(x,6)+afive*Math.pow(x,5)+afour*Math.pow(x,4)+athree*Math.pow(x,3)+atwo*Math.pow(x,2)+aone*Math.pow(x,1)+azero*Math.pow(x,0);
System.out.println("f("+x+") = "+y);
double yprime = asix*Math.pow(x,6)+afive*Math.pow(x,5)+afour*Math.pow(x,4)+athree*Math.pow(x,3)+atwo*Math.pow(x,2)+aone*Math.pow(x,1)+azero*Math.pow(x,0);
while (Math.abs(y)>0.001)
{
x = x-y/yprime;
y = asix*Math.pow(x,6)+afive*Math.pow(x,5)+afour*Math.pow(x,4)+athree*Math.pow(x,3)+atwo*Math.pow(x,2)+aone*Math.pow(x,1)+azero*Math.pow(x,0);
yprime = 6*asix*Math.pow(x,5)+5*afive*Math.pow(x,4)+4*afour*Math.pow(x,3)+3*athree*Math.pow(x,2)+2*atwo*Math.pow(x,1)+1*aone*Math.pow(x,0);
System.out.println("f("+x+") = "+y);
}
System.out.println("");
System.out.println("f(x) = 0 when x = approximately " + x);
System.out.println("");
System.out.print("Thank you for using my program. Improvements will be made in the future");
}
}
NOTE: When I made azero = 1 or 0, it works 100% of the time... so maybe that's the source of the issue? I'd like to keep it random, but I might be having a dipshit moment with it.