Java The code is running just needs an additional function to calculate the Laagrages form as well can you add that to it?
//Interpolation.java
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Interpolation {
public String filename;
public double[][] diffTable;
public ArrayList<Double> strY = new ArrayList<Double>();
public Interpolation(String filename){
this.filename = filename;
}
public void createTable(){
String[] x = null;
String[] fx = null;
FileInputStream fstream = null;
//Check to see if file exists or not
try {
fstream = new FileInputStream (this.filename);
}
catch(FileNotFoundException err) {
System.out.println("File not Found.");
}
DataInputStream data = new DataInputStream (fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(data));
//Check to see the contents of the file
try {
x = br.readLine().split("\\s+");
fx = br.readLine().split("\\s+");
}
catch(IOException io){
System.out.println("There is nothing to read");
}
diffTable = new double[x.length][fx.length + 1];
//setting the x and y values
for(int i = 0; i < x.length; ++i) {
diffTable[i][0] = Double.parseDouble(x[i]);
}
for(int i = 0; i < fx.length; ++i){
diffTable[i][1] = Double.parseDouble(fx[i]);
}
}
public void divided_Difference(){
int n = diffTable[0].length;
for(int j = 2; j < n; j++){
for(int i = 0; i < n - j; i++){
diffTable[i][j] = (diffTable[i+1][j-1] - diffTable[i][j-1]) /(diffTable[i + (j-1)][0] - diffTable[i][0]);
}
}
for (int i = 1; i < diffTable[0].length; ++i) {
strY.add(diffTable[0][i]);
}
}
//Making the interpolation function
public void interpolation() {
ArrayList<String> x = new ArrayList<String>();
String sign = "";
for (int i = 0; i < this.diffTable.length - 1; ++i) {
double xVal = this.diffTable[i][0];
if (xVal < 0) {
sign = "+";
}
else if (xVal > 0) {
sign = "-";
}
if (round(xVal) == 0) {
x.add("(x)");
}
else {
x.add(String.format("(x%s%.3f)", sign, xVal));
}
}
String polynomial = String.format("%.3f", strY.get(0));
for (int i = 1; i < x.size() + 1; ++i) {
double yVal = strY.get(i);
if (yVal != 0) {
if (yVal > 0) {
sign = "+";
}
else {
sign = "-";
}
String join = "";
for (int j = 0; j < i; ++j) {
join += x.get(j);
}
polynomial += String.format(" %s %.3f%s", sign, Math.abs(yVal), join);
}
}
System.out.println(" ");
System.out.println("\nThe interpolating polynomial in Newton's form is: " + polynomial);
}
public void print(){
int n = diffTable[0].length;
System.out.printf(" x \t y \t f(,) \t f(,,) \tf(,,,) ");
for(int i = 0; i < n - 1; i++){
System.out.printf("\n");
for(int j = 0; j < n - i; j++){
System.out.printf(" ");
System.out.printf(" %.3f ", diffTable[i][j]);
}
}
}
//Simplified Polynomial
public void simplifiedPolynomial(){
Polynomial polynomial = new Polynomial();
ArrayList<Double> value = new ArrayList<Double>();
ArrayList<ArrayList<Double>> array = new ArrayList<ArrayList<Double>>();
for(int i = 0; i < diffTable[0].length - 1; i++){
value.add(0.0);
}
value.add(0, strY.get(0));
array.add(value);
for(int i = 1; i < strY.size(); i++){
value = new ArrayList<Double>();
double yVal = strY.get(i);
for(int j = 0; j < i; j++){
value.add(diffTable[j][0]);
}
array.add(polynomial.polyFunction(yVal, value, diffTable[0].length));
}
value = polynomial.combineLike(array);
System.out.println("The simplified polynomial is: " + printString(value));
}
private String printString(ArrayList<Double> array) {
String polynomial = "";
String power = "";
for (int i = 0; i < array.size() - 1; i++) {
Double f = array.get(i);
power = String.format("x^%d", i);
if (f != 0) {
if (i == 0) {
polynomial += String.format(" %.3f", f);
}
else {
polynomial += String.format(" %+.3f%s", f, power);
}
}
}
return polynomial;
}
private double round(double value) {
return (double) Math.round(value * 1000) / 1000;
}
public static void main(String[] args){
Interpolation interpolation = new Interpolation("data.txt");
interpolation.createTable();
interpolation.divided_Difference();
interpolation.print();
interpolation.interpolation();
interpolation.simplifiedPolynomial();
}
}
Here is the question
Test data
1 1.5 0 2
3 3.25 3 1.67
Construct a program that asks the user for a text file which has the x and f(x) values. It then reads the data from this file creates and creates a divided difference table and uses that to create the interpolating polynomial. Print the polynomial in both the Newton's form and Lagrange's form. The example below only shows the Newton's form polynomial, Lagrange's polynomial and the simplified polynomial. You have to print polynomial in all three forms (Newton's, Lagrange and Simplified) The sample output of your program for this input file (download the txt file here Download here) should print out the divided difference table and the interpolating polynomial as shown below: x f[] f[ , ] f[ , , ] f[ , , , ] 1 3 1/2 3/2 13/4 1/3 1/6 -2 0 3 -5/3 -2/3 2 5/3 Interpolating polynomial in Newton's form is: 3 + 1/2(x-1) +1/3(x-1)(x-3/2) - 2(x-1)(x-3/2)x Interpolating polynomial in Laagrange's form is: 6(x-3/2)(x)(x-2) - 26/3(x-1)(x)(x-2) - (x-1)(x-3/2)(x-2) + 5/3(x-1)(x-3/2)(x) Simplified polynomial is: -2x^3 + 5.334x^2 - 3.334x + 3 Your program should work on any data (at most 50 node points) and not just the above sample data. If you are struggling to print the divided difference table in the above pyramid format, you can print it in the simpler triangular format instead. Also, you don't have to use fractions, you can convert them to decimal numbers and use those instead.
sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet
sectetur adipiscing eliUnlock access to this and over
10,000 step-by-step explanations
Have an account? Log In
sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque
sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor
sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante,
sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet
sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetu
sectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus