Seal_of_approval

Cauchy

Nov 22nd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.01 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. class EuilerSolve {
  4.  
  5.     private double a = 1.0;
  6.     private double b = 5.0;
  7.     //    private double V = 1;
  8.     private double V = 3;
  9.     private int N = 15;
  10.  
  11.  
  12.     ArrayList<Double> xList = new ArrayList<>();
  13.     ArrayList<Double> exactYList = new ArrayList<>();
  14.     ArrayList<Double> euilerYList = new ArrayList<>();
  15.     ArrayList<Double> diffList = new ArrayList<>();
  16.     ArrayList<Double> yList = new ArrayList<>();
  17.  
  18.     double origF(double x) {
  19.         return V * (x * x * x) + (x * x) + x;
  20.     }
  21.  
  22.     double stepF(double x, double fx) {
  23.         return V * (x * x * x) + (3 * V + 1) * (x * x) + 3 * x + 1 - fx;
  24.     }
  25.  
  26.     public void calcImproved() {
  27.         double step;
  28.         step = (b - a) / (N - 1);
  29.         xList.add(a);
  30.         euilerYList.add(V + 2);
  31.  
  32.         for (int i = 1; i < N; i++) {
  33.             xList.add(xList.get(i - 1) + step);
  34.         }
  35.         for (int i = 1; i < xList.size(); i++) {
  36.             double _tmpX = (xList.get(i - 1) + xList.get(i)) / 2;
  37.             double _tmpFx = euilerYList.get(i - 1) + (step / 2) * stepF(xList.get(i - 1), euilerYList.get(i - 1));
  38.  
  39.             double dy = euilerYList.get(i - 1) + step * stepF(_tmpX, _tmpFx);
  40.             euilerYList.add(dy);
  41.             //System.out.println(_tmpX);
  42.         }
  43.         for (int i = 0; i < N; i++) {
  44.             double exactVal = origF(xList.get(i));
  45.             exactYList.add(exactVal);
  46.             diffList.add(Math.abs(exactVal - euilerYList.get(i)));
  47.         }
  48.  
  49.         System.out.format("\tx\teuiler y\texact y\t\tdiff\n");
  50.         for (int i = 0; i < N; i++) {
  51.             System.out.format("%.3f%10.3f%10.3f%10.3f\n", xList.get(i), euilerYList.get(i), exactYList.get(i), diffList.get(i));
  52.         }
  53.     }
  54.  
  55.     public void calc() {
  56.         double step = b - a;
  57.         step /= (N - 1);
  58.         xList.add(a);
  59.         euilerYList.add(V + 2);
  60.  
  61.         for (int i = 1; i < N; i++) {
  62.             xList.add(xList.get(i - 1) + step);
  63.         }
  64.         for (int i = 1; i < N; i++) {
  65.             euilerYList.add(euilerYList.get(i - 1) + step * stepF(xList.get(i - 1), euilerYList.get(i - 1)));
  66.         }
  67.         for (int i = 0; i < N; i++) {
  68.             double exactVal = origF(xList.get(i));
  69.             exactYList.add(exactVal);
  70.             diffList.add(Math.abs(exactVal - euilerYList.get(i)));
  71.         }
  72.  
  73.         System.out.format("\tx\teuiler y\texact y\t\tdiff\n");
  74.         for (int i = 0; i < N; i++) {
  75.             System.out.format("%.3f%10.3f%10.3f%10.3f\n", xList.get(i), euilerYList.get(i), exactYList.get(i), diffList.get(i));
  76.         }
  77.     }
  78.  
  79.  
  80.     public void pred_kor_calc() {
  81.         double step = b - a;
  82.         step /= (N - 1);
  83.         xList.add(a);
  84.         yList.add(V + 2);
  85.  
  86.         for (int i = 1; i < N; i++) {
  87.             xList.add(xList.get(i - 1) + step);
  88.         }
  89.  
  90.         for (int i = 1; i < xList.size(); i++) {
  91.             double _tmpFx = stepF(xList.get(i - 1), yList.get(i - 1));
  92.             double tmpFx = yList.get(i - 1) + step * stepF(xList.get(i - 1),yList.get(i - 1));
  93.             double tmp = stepF(xList.get(i), tmpFx);
  94.             double y = yList.get(i - 1) + step * (_tmpFx + tmp) / 2 ;
  95.             yList.add(y);
  96.         }
  97.  
  98.         for (int i = 0; i < N; i++) {
  99.             double exactVal = origF(xList.get(i));
  100.             exactYList.add(exactVal);
  101.             diffList.add(Math.abs(exactVal - yList.get(i)));
  102.         }
  103.  
  104.         System.out.format("\tx\tpred_kor y\texact y\t\tdiff\n");
  105.         for (int i = 0; i < N; i++) {
  106.             System.out.format("%.3f%10.3f%10.3f%10.3f\n", xList.get(i), yList.get(i), exactYList.get(i), diffList.get(i));
  107.         }
  108.  
  109.     }
  110. }
  111.  
  112. public class CauchyEuilerSolve {
  113.     public static void main(String[] args) {
  114.         EuilerSolve es = new EuilerSolve();
  115.         es.calc();
  116.         System.out.println("===========");
  117.         EuilerSolve esImpr = new EuilerSolve();
  118.         esImpr.calcImproved();
  119.         EuilerSolve pred_kor = new EuilerSolve();
  120.         pred_kor.pred_kor_calc();
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment