I wanted to tabulate my data for Newtons Raphsons Method only for quadratic equations in neat array something like. What I am having difficulty doing is allocating the variables in the array, allocating the previous value of xn1 to the xn value in the subsequent row and having double values in the array.
For example (In my code below) I have variables such as (n, xn, fxn, dfxn, xn1). I wanted to allocate the value of those variables under the respective headers like the image below.
In the [image], the xn+1 value in the first row becomes the xn value in the second row. I am having trouble doing that. I tried to do xn = xn1
in the end of a for loop
but that didn't seem to work.
Lastly, every time I try to allocate the double
variable values to double array[][]
, it gives me an error saying double cannot be converted to double[]
.
Code:
import java.util.Scanner;
import java.lang.Math;
import java.util.Formatter;
public class Newton {
static double a, b, c;
static double n = 1;
static double xn;
static double fxn;
static double dfxn;
static double xn1;
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a");
double a = s.nextDouble();
System.out.println("Enter b");
double b = s.nextDouble();
System.out.println("Enter c");
double c = s.nextDouble();
System.out.println("Enter The value you want to start with");
double xn = s.nextDouble();
fxn = a * Math.pow(xn, 2) + b * xn + c;
dfxn = 2 * a * xn + b;
xn1 = xn - (fxn / dfxn);
double array[][] = { n, xn, fxn, dfxn, xn + 1 };
System.out.println();
System.out.println("n\t" + "Xn\t" + "fXn\t" + "dfXn\t" + "Xn+1");
array(array);
}
public static void array(int x[][]) {
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x[row].length; column++) {
System.out.print(x[row][column] + "\t");
}
System.out.println();
}
}
}