Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package polynomial;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.text.DecimalFormat;
  12. import java.util.Scanner;
  13.  
  14. /**
  15.  *
  16.  * @author casey
  17.  */
  18. public class Polynomial {
  19.  
  20.    
  21.     public static void main(String[] args) throws FileNotFoundException {
  22.        
  23.         Scanner read = new Scanner(new BufferedReader(new FileReader("poly.txt")));
  24.         //read.useDelimiter("~|\\n");
  25.         String line = "";
  26.             //first line into x array
  27.             line = read.nextLine();
  28.             System.out.println("LINE: "+line);
  29.             String[] x = line.split("\\s+");
  30.             //second line into y array
  31.             line = read.nextLine();
  32.             System.out.println("LINE: "+line);
  33.             String[] y = line.split("\\s+");    
  34.        
  35.         read.close();
  36.        
  37.         Double[][] array = new Double[(x.length)*2][y.length+1];
  38.        
  39.         DecimalFormat df = new DecimalFormat("###.###");  
  40.          int a = 0;
  41.        
  42.         for(int i = 0; i<(x.length*2); i=i+2){  
  43.            
  44.             array[i][0] = Double.parseDouble(x[a]);
  45.             a++;
  46.            
  47.         }
  48.         a=0;
  49.         for(int j = 0; j<(y.length*2); j=j+2){  
  50.             array[j][1] = Double.parseDouble(y[a]);
  51.             a++;
  52.  
  53.         }
  54.        
  55.        
  56.         System.out.println("x\t"+"f[]\t"+"f[,]\t"+"f[,,]\t"+"f[,,,]");
  57.         int j = 0;
  58.         int i = 0;
  59.        
  60.            
  61.           for(i = 0; i <=x.length; i=i+2){  
  62.             array[i+1][j+2] = ((array[i+2][j+1]) - (array[i][j+1])) /((array[i+2][j]) - (array[i][j]));              
  63.           }
  64.        
  65.        
  66.        
  67.        
  68.             array[i-2][j+3] = ((array[i-1][j+2]) - (array[i-3][j+2])) /((array[i][j]) - (array[i-4][j]));
  69.             array[i-4][j+3] = ((array[i-3][j+2]) - (array[i-5][j+2])) /((array[i-2][j]) - (array[i-6][j]));
  70.             array[i-3][j+4] = ((array[i-2][j+3]) - (array[i-4][j+3])) /((array[i][j]) - (array[i-6][j]));
  71.            
  72.        
  73.        
  74.         for(int s = 0; s<x.length*2; s++){
  75.             for(int t = 0; t<y.length+1; t++){
  76.                 //System.out.println("Piece: "+df.format(array[0][0]));
  77.                  System.out.print((array[s][t])+"\t");
  78.             }
  79.             System.out.println();
  80.         }
  81.         //3 + (1/2)(x-1) +  (1/3)(x-1)(x-3/2) - 2(x-1)(x-3/2)x
  82.         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])+")+"
  83.                 +df.format(array[3][4])+"(x-"+array[4][0]+")(x-"+(array[0][0]+")"+"(x-"+(array[2][0])+")");
  84.         System.out.println("Interloping Polynomial is: "+polynomial);
  85.     }
  86.    
  87.    
  88.    
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement