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")));
- //first line into x array
- String 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];
- for(int i = 0; i<x.length*2;i++){
- for(int j = 0;j<array[0].length;j++){
- array[i][j] = 0.0;
- }
- }
- 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++;
- }
- //LAGRANGES METHODS
- /*System.out.println("x: ");
- for(int i=0; i<x.length;i++){
- System.out.print(x[i]+ "\t");
- }
- System.out.println("\ny: ");
- for(int i=0; i<y.length;i++){
- System.out.print(y[i]+ "\t");
- }
- System.out.println();
- System.out.println("l_0");
- int c = 0;
- Double[] denominator = new Double[x.length];
- int[] values = new int[x.length];
- for(int i = 0; i<x.length;i++){
- values[i] = i;
- }
- for(int i = 0; i<1; i++){
- denominator[i] = ((Double.parseDouble(x[c]) - Double.parseDouble(x[c+1])) * (Double.parseDouble(x[c]) - Double.parseDouble(x[c+2]))
- * (Double.parseDouble(x[c]) - Double.parseDouble(x[c+3])));
- }
- System.out.println("Denominator: "+denominator[0]);*/
- //DIVIDED METHOD
- System.out.println("x\t"+"f[]\t"+"f[,]\t"+"f[,,]\t"+"f[,,,]");
- int i = 0;
- for(int j=2; j<x.length-1;j++ ){
- for(i = 0; i <2*(x.length-1); i=i+2){
- array[i+1][j] = (((array[i+2][j-1]) - (array[i][j-1])) /((array[i+j][0]) - (array[i][0])));
- System.out.println("i+j: "+(i+j));
- }
- System.out.println("i: "+i);
- }
- //i=6;
- //j=0;
- // 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();
- }
- String polynomial = String.valueOf(df.format(array[0][1]));
- String xValue ="";
- int j = 1;
- int count = 0;
- for(int I=0; I<x.length-1;I++){
- xValue = xValue+"(x-"+x[count]+")";
- count++;
- System.out.println("Before i: "+I+" j: "+j);
- polynomial = polynomial + " + " + array[I+1][j+1]+ xValue;
- j++;
- }
- System.out.println("Interpolating polynimal is:");
- System.out.println(polynomial);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement