2

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.

enter image description here

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();
        }
    }
}
demongolem
  • 9,474
  • 36
  • 90
  • 105

2 Answers2

1

As for this: double array[][] = {n, xn, fxn, dfxn, xn+1};

Because it is a 2d array, it is initialized differently then other arrays:

double[][] array = new double[][]{
          { n, xn, fxn, dfxn, xn+1}
        };

Now, because xn+1 is an operation, you to parenthesize it in order to assign it to the array:

double[][] array = new double[][]{
          { n, xn, fxn, dfxn, (xn+1)}
        };

Also, when you call array(array);, you are trying to pass a double array to an int method. Try changing the method's constructor:

public static void array(double[][] array){
    //...
}

Finally, you need to close your scanner before you call your array method in order to prevent a resource leak: s.close();

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
1

The errors / mistakes that I found in your code are

  1. Initialing array of doubles
    Instead double array[][] = { n, xn, fxn, dfxn, xn + 1 };
    It should be double array[][] = { { n, xn, fxn, dfxn, xn + 1 } };

  2. You should change method signature for method array
    Instead public static void array(int x[][]) {
    Use public static void array(double x[][]) {

  3. You never close stram s.
    Add s.close() to the end of main method.

djm.im
  • 3,295
  • 4
  • 30
  • 45