Advertisement
ttaaa

ODE Examples

May 8th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 KB | None | 0 0
  1. package back;
  2.  
  3. import back.exception.UnavailableCodeException;
  4.  
  5. import java.util.ArrayList;
  6.  
  7. public enum OrdinaryDifferentialEquations implements OrdinaryDifferentialEquation {
  8.     EQUATION_1 {    //y' = 2x^2
  9.         @Override
  10.         public double getValue(double xValue, double yValue) {
  11.             return 2 * Math.pow(xValue, 2);
  12.         }
  13.  
  14.         @Override
  15.         public ConstEquation getGeneralSolution() {
  16.             return new ConstEquation() {
  17.                 @Override
  18.                 public DerivativeFunc getFunction(ArrayList<Double> consts) {
  19.                     return new DerivativeFunc() {
  20.                         @Override
  21.                         public double get(double argument) {
  22.                             return 2 * Math.pow(argument, 3) / 3 + consts.get(0);
  23.                         }
  24.  
  25.                         @Override
  26.                         public Interval[] getNotAllowedScope() {
  27.                             return new Interval[0];
  28.                         }
  29.                     };
  30.                 }
  31.  
  32.                 @Override
  33.                 public ArrayList<Double> resolveConsts(Point[] points) {
  34.                     ArrayList<Double> consts = new ArrayList<>();
  35.  
  36.                     //FIXME(Not for University course): work only for one point now
  37.                     consts.add(points[0].second - 2 * Math.pow(points[0].first, 3) / 3);
  38.  
  39.                     return consts;
  40.                 }
  41.             };
  42.         }
  43.  
  44.         @Override
  45.         public String toString() {
  46.             return "y' = 2x^2";
  47.         }
  48.     },
  49.     EQUATION_2 {    //y' = x - y
  50.         @Override
  51.         public double getValue(double xValue, double yValue) {
  52.             return xValue - yValue;
  53.         }
  54.  
  55.         @Override
  56.         public ConstEquation getGeneralSolution() {
  57.             return new ConstEquation() {
  58.                 @Override
  59.                 public DerivativeFunc getFunction(ArrayList<Double> consts) {
  60.                     return new DerivativeFunc() {
  61.                         @Override
  62.                         public double get(double argument) {
  63.                             return consts.get(0) * Math.exp(-argument) + argument - 1;
  64.                         }
  65.  
  66.                         @Override
  67.                         public Interval[] getNotAllowedScope() {
  68.                             return new Interval[0];
  69.                         }
  70.                     };
  71.                 }
  72.  
  73.                 @Override
  74.                 public ArrayList<Double> resolveConsts(Point[] points) {
  75.                     ArrayList<Double> consts = new ArrayList<>();
  76.  
  77.                     //FIXME(Not for University course): work only for one point now
  78.                     consts.add((points[0].second + 1 - points[0].first) * Math.exp(points[0].first));
  79.  
  80.                     return consts;
  81.                 }
  82.             };
  83.         }
  84.  
  85.         @Override
  86.         public String toString() {
  87.             return "y' = x - y";
  88.         }
  89.     },
  90.     EQUATION_3 {    //y' = sin(x) + y
  91.         @Override
  92.         public double getValue(double xValue, double yValue) {
  93.             return Math.sin(xValue) + yValue;
  94.         }
  95.  
  96.         @Override
  97.         public ConstEquation getGeneralSolution() {
  98.             return new ConstEquation() {
  99.                 @Override
  100.                 public DerivativeFunc getFunction(ArrayList<Double> consts) {
  101.                     return new DerivativeFunc() {
  102.                         @Override
  103.                         public double get(double argument) {
  104.                             return consts.get(0) * Math.exp(argument) - Math.sin(argument) / 2 - Math.cos(argument) / 2;
  105.                         }
  106.  
  107.                         @Override
  108.                         public Interval[] getNotAllowedScope() {
  109.                             return new Interval[0];
  110.                         }
  111.                     };
  112.                 }
  113.  
  114.                 @Override
  115.                 public ArrayList<Double> resolveConsts(Point[] points) {
  116.                     ArrayList<Double> consts = new ArrayList<>();
  117.  
  118.                     //FIXME(Not for University course): work only for one point now
  119.                     consts.add((points[0].second + Math.sin(points[0].first) / 2 + Math.cos(points[0].first) / 2) * Math.exp(-points[0].first));
  120.  
  121.                     return consts;
  122.                 }
  123.             };
  124.         }
  125.  
  126.         @Override
  127.         public String toString() {
  128.             return "y' = sin(x) + y";
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement