Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package polynomial;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.text.DecimalFormat;
- import java.util.Scanner;
- /**
- *
- * @author casey
- */
- public class Polynomial {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner read = new Scanner(new BufferedReader(new FileReader("poly.txt")));
- //read.useDelimiter("~|\\n");
- String line = "";
- //first line into x array
- line = read.nextLine();
- System.out.println("LINE: "+line);
- String[] x = line.split("\\s+");
- //second line into y array
- line = read.nextLine();
- System.out.println("LINE: "+line);
- String[] y = line.split("\\s+");
- read.close();
- Double[][] array = new Double[(x.length)*2][y.length+1];
- DecimalFormat df = new DecimalFormat("###.###");
- int a = 0;
- for(int i = 0; i<(x.length*2); i=i+2){
- array[i][0] = Double.parseDouble(x[a]);
- a++;
- }
- a=0;
- for(int j = 0; j<(y.length*2); j=j+2){
- array[j][1] = Double.parseDouble(y[a]);
- a++;
- }
- System.out.println("x\t"+"f[]\t"+"f[,]\t"+"f[,,]\t"+"f[,,,]");
- int j = 0;
- int i = 0;
- for(i = 0; i <=x.length; i=i+2){
- array[i+1][j+2] = ((array[i+2][j+1]) - (array[i][j+1])) /((array[i+2][j]) - (array[i][j]));
- }
- array[i-2][j+3] = ((array[i-1][j+2]) - (array[i-3][j+2])) /((array[i][j]) - (array[i-4][j]));
- array[i-4][j+3] = ((array[i-3][j+2]) - (array[i-5][j+2])) /((array[i-2][j]) - (array[i-6][j]));
- array[i-3][j+4] = ((array[i-2][j+3]) - (array[i-4][j+3])) /((array[i][j]) - (array[i-6][j]));
- for(int s = 0; s<x.length*2; s++){
- for(int t = 0; t<y.length+1; t++){
- //System.out.println("Piece: "+df.format(array[0][0]));
- System.out.print((array[s][t])+"\t");
- }
- System.out.println();
- }
- //3 + (1/2)(x-1) + (1/3)(x-1)(x-3/2) - 2(x-1)(x-3/2)x
- String polynomial = array[0][1]+"+"+array[1][2]+"(x-"+array[0][0]+")+"+df.format(array[2][3])+"(x-"+(array[0][0])+")"+"(x-"+(array[2][0])+")+"
- +df.format(array[3][4])+"(x-"+array[4][0]+")(x-"+(array[0][0]+")"+"(x-"+(array[2][0])+")");
- System.out.println("Interloping Polynomial is: "+polynomial);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement