Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- public class Main {
- public static double[] xn = {0.135, 0.876, 1.336, 2.301, 2.851};
- public static double[] yn = {2.382,-0.212,-1.305,-3.184,-4.365};
- public static double x = 0;
- public static double[] znam = new double[5];
- public static String[] langStr = new String[5];
- public static double [] lang = new double[5];
- public static String[] p = new String[5];
- public static double[][] razn = new double[5][5];
- public static double [] razn1 =new double[5];
- public static String[] newtStr = new String[5];
- public static double[] newt = new double[5];
- public static double[] a = new double[4];
- public static double [] b = new double[4];
- public static BigDecimal [] a1 = new BigDecimal[4];
- public static BigDecimal [] b1 = new BigDecimal[4];
- public static String [] lineStr = new String[4];
- public static void main(String[] args) {
- lagrange();
- newton();
- lineSpline();
- }
- public static void lagrange(){
- System.out.println("Polimom lagrange:");
- for (int i = 0;i< xn.length;i++){
- double tmp = 1;
- for(int j = 0;j< xn.length;j++){
- if(i!=j){
- tmp *= xn[i]-xn[j];
- }
- }
- znam[i] = 1/tmp;
- BigDecimal z = new BigDecimal(znam[i]).setScale(3, RoundingMode.HALF_UP);
- p[i]= "p("+i+") = " + z;
- for (int j = 0;j<p.length;j++){
- if(i!=j){
- p[i]+="(x- "+ xn[j] +")";
- }
- }
- System.out.println(p[i]);
- }
- for(int j = 0;j< p.length;j++){
- int ch= p[j].indexOf("(",2);
- int l = p[j].length();
- p[j] = p[j].substring(ch, l);
- }
- System.out.println("Iteratciony process langrage: ");
- for(int i = 0;i<xn.length;i++){
- znam[i]*=yn[i];
- BigDecimal z = new BigDecimal(znam[i]).setScale(3, RoundingMode.HALF_UP);
- langStr[i] = z+p[i];
- if(znam[i]>0 && i!=0) {
- System.out.println("+ "+langStr[i]);
- }else{
- System.out.println(langStr[i]);
- }
- }
- double ans = 0;
- x =xn[1]+xn[2];
- for(int i = 0;i<xn.length;i++){
- double tmp = 1;
- for(int j = 0;j< xn.length;j++){
- if(i!=j){
- tmp*= x- xn[j];
- }
- }
- lang[i] = znam[i]*tmp;
- }
- for(Double i:lang){
- ans+=i;
- }
- BigDecimal answer = new BigDecimal(ans).setScale(3, RoundingMode.HALF_UP);
- System.out.println("L4(x1+x2) = "+"L4("+x+") = " + answer);
- }
- public static void newton(){
- System.out.println("\nPolinom Newton:");
- //заполенение y 1 строку razn
- for(int i=0;i< razn.length;i++){
- razn[0][i] = yn[i];
- razn1[0] = yn[0];
- }
- //1 порядок
- for(int i = 1;i< razn.length;i++){
- razn[1][i] = (razn[0][i]-razn[0][i-1])/(xn[i]-xn[i-1]);
- }
- razn1[1] = razn[1][1];
- //2 порядок
- for(int i = 2;i< razn.length;i++){
- razn[2][i] = (razn[1][i]-razn[1][i-1])/(xn[i]-xn[i-2]);
- }
- razn1[2] = razn[2][2];
- //3 порядок
- for(int i = 3;i< razn.length;i++){
- razn[3][i] = (razn[2][i]-razn[2][i-1])/(xn[i]-xn[i-3]);
- }
- razn1[3] = razn[3][3];
- //4 порядок
- for(int i = 4;i< razn.length;i++){
- razn[4][i] = (razn[3][i]-razn[3][i-1])/(xn[i]-xn[i-4]);
- }
- razn1[4] = razn[4][4];
- BigDecimal [] raz = new BigDecimal[5];
- for(int i = 0;i<razn1.length;i++){
- raz[i] = new BigDecimal(razn1[i]).setScale(3,RoundingMode.HALF_UP);
- }
- newtStr[0] = String.valueOf(raz[0]);
- for(int i = 1;i<raz.length;i++) {
- if (razn1[i] < 0) {
- newtStr[i] = String.valueOf(raz[i]);
- } else {
- newtStr[i] = " + " + String.valueOf(raz[i]);
- }
- }
- newtStr[1]+="(x-"+xn[0]+")";
- newtStr[2]+="(x-"+xn[0]+")(x-"+xn[1]+")";
- newtStr[3]+="(x-"+xn[0]+")(x-"+xn[1]+")(x-"+xn[2]+")";
- newtStr[4]+="(x-"+xn[0]+")(x-"+xn[1]+")(x-"+xn[2]+")(x-"+xn[3]+")";
- x =xn[1]+xn[2];
- System.out.print("N4 = ");
- for(int i = 0;i<newtStr.length;i++){
- System.out.println(newtStr[i]);
- }
- newt[0] = razn1[0];
- newt[1] = razn1[1]*(x-xn[0]);
- newt[2] = razn1[2]*(x-xn[0])*(x-xn[1]);
- newt[3] = razn1[3]*(x-xn[0])*(x-xn[1])*(x-xn[2]);
- newt[4] = razn1[4]*(x-xn[0])*(x-xn[1])*(x-xn[2])*(x-xn[3]);
- double ans = newt[0]+newt[1]+newt[2]+newt[3]+newt[4];
- BigDecimal answer = new BigDecimal(ans).setScale(3, RoundingMode.HALF_UP);
- System.out.println("N4(x1+x2) = "+"N4("+x+") = " + answer);
- }
- public static void lineSpline(){
- System.out.println("\nLinear spline:");
- System.out.println("Ф(x) = ");
- for(int i = 1;i< xn.length;i++){
- System.out.println("a"+i+"x + b"+i+", "+xn[i-1]+"<x<"+xn[i]);
- }
- System.out.println("\nSystem: ");
- for(int i = 1;i< xn.length;i++){
- lineStr[i-1] = String.valueOf(xn[i-1])+"a"+String.valueOf(i)+" + b"+String.valueOf(i)
- +" = "+String.valueOf(yn[i-1])+"\n"
- +String.valueOf(xn[i])+"a"+String.valueOf(i)+" + b"+String.valueOf(i)
- +" = "+String.valueOf(yn[i]);
- System.out.println(lineStr[i-1]+"\n");
- }
- System.out.println("Coefficient: ");
- for(int i = 1;i< yn.length;i++){
- a[i-1] = (yn[i-1]-yn[i])/(xn[i-1]-xn[i]);
- b[i-1] = yn[i]-xn[i]*a[i-1];
- a1[i-1] = new BigDecimal(a[i-1]).setScale(3, RoundingMode.HALF_UP);
- b1[i-1] = new BigDecimal(b[i-1]).setScale(3, RoundingMode.HALF_UP);
- System.out.println("a"+i+" = "+a1[i-1]+"\nb"+i+" = "+b1[i-1]+"\n");
- }
- System.out.println("Ф(x) = ");
- int j = 0;
- for(int i = 1;i< xn.length;i++){
- if(b[j]<0) {
- System.out.println(a1[j] + "x " + b1[j] + ", " + xn[i- 1] + "<x<" + xn[i]);
- }else{
- System.out.println(a1[j] + "x + " + b1[j] + ", " + xn[i- 1] + "<x<" + xn[i]);
- }
- j++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement