
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
this is the code for Newton divided difference in java, user give input and get result
Typology: Exercises
1 / 1
This page cannot be seen from the preview
Don't miss anything!

On special offer
package newtondivideddifference; import java.util.Scanner; public class Newtondivideddifference { static double x[]; static double y[][]; static int arraysize; public Newtondivideddifference(int arraysize) { this.arraysize = arraysize; x = new double[arraysize]; y = new double[arraysize][arraysize]; } public static void dividedDifferenceTable() { for (int i = 1; i < arraysize; i++) { for (int j = 0; j < arraysize - i; j++) { y[i][j] = (y[i - 1][j] - y[i - 1][j + 1]) / (x[j] - x[i + j]); } } } public static double solution(double xi) { dividedDifferenceTable(); double result = y[0][0]; double dfactor; for (int i = 1; i < arraysize; i++) { dfactor = 1.0; for (int j = 1; j <= i; j++) { dfactor = dfactor * (xi - x[j - 1]); } result = result + dfactor * y[i][0]; System.out.println("y0:" + y[i][0]); } return result; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Please enter how many data: "); int size = in.nextInt(); Newtondivideddifference ob = new Newtondivideddifference(size); System.out.println("Please enter all data: "); for (int i = 0; i < arraysize; i++) { x[i] = in.nextDouble(); y[0][i] = in.nextDouble(); } System.out.println("Please enter x value for which you want the solution:"); double xi = in.nextDouble(); System.out.println("The solution is: " + solution(xi)); } }