Newton divided difference in java, Exercises of Mathematical Methods for Numerical Analysis and Optimization

this is the code for Newton divided difference in java, user give input and get result

Typology: Exercises

2018/2019
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 04/21/2019

mohammad-rakib
mohammad-rakib 🇧🇩

5

(1)

2 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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));
}
}
Discount

On special offer

Partial preview of the text

Download Newton divided difference in java and more Exercises Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

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)); } }