Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- class EuilerSolve {
- private double a = 1.0;
- private double b = 5.0;
- // private double V = 1;
- private double V = 3;
- private int N = 15;
- ArrayList<Double> xList = new ArrayList<>();
- ArrayList<Double> exactYList = new ArrayList<>();
- ArrayList<Double> euilerYList = new ArrayList<>();
- ArrayList<Double> diffList = new ArrayList<>();
- ArrayList<Double> yList = new ArrayList<>();
- double origF(double x) {
- return V * (x * x * x) + (x * x) + x;
- }
- double stepF(double x, double fx) {
- return V * (x * x * x) + (3 * V + 1) * (x * x) + 3 * x + 1 - fx;
- }
- public void calcImproved() {
- double step;
- step = (b - a) / (N - 1);
- xList.add(a);
- euilerYList.add(V + 2);
- for (int i = 1; i < N; i++) {
- xList.add(xList.get(i - 1) + step);
- }
- for (int i = 1; i < xList.size(); i++) {
- double _tmpX = (xList.get(i - 1) + xList.get(i)) / 2;
- double _tmpFx = euilerYList.get(i - 1) + (step / 2) * stepF(xList.get(i - 1), euilerYList.get(i - 1));
- double dy = euilerYList.get(i - 1) + step * stepF(_tmpX, _tmpFx);
- euilerYList.add(dy);
- //System.out.println(_tmpX);
- }
- for (int i = 0; i < N; i++) {
- double exactVal = origF(xList.get(i));
- exactYList.add(exactVal);
- diffList.add(Math.abs(exactVal - euilerYList.get(i)));
- }
- System.out.format("\tx\teuiler y\texact y\t\tdiff\n");
- for (int i = 0; i < N; i++) {
- System.out.format("%.3f%10.3f%10.3f%10.3f\n", xList.get(i), euilerYList.get(i), exactYList.get(i), diffList.get(i));
- }
- }
- public void calc() {
- double step = b - a;
- step /= (N - 1);
- xList.add(a);
- euilerYList.add(V + 2);
- for (int i = 1; i < N; i++) {
- xList.add(xList.get(i - 1) + step);
- }
- for (int i = 1; i < N; i++) {
- euilerYList.add(euilerYList.get(i - 1) + step * stepF(xList.get(i - 1), euilerYList.get(i - 1)));
- }
- for (int i = 0; i < N; i++) {
- double exactVal = origF(xList.get(i));
- exactYList.add(exactVal);
- diffList.add(Math.abs(exactVal - euilerYList.get(i)));
- }
- System.out.format("\tx\teuiler y\texact y\t\tdiff\n");
- for (int i = 0; i < N; i++) {
- System.out.format("%.3f%10.3f%10.3f%10.3f\n", xList.get(i), euilerYList.get(i), exactYList.get(i), diffList.get(i));
- }
- }
- public void pred_kor_calc() {
- double step = b - a;
- step /= (N - 1);
- xList.add(a);
- yList.add(V + 2);
- for (int i = 1; i < N; i++) {
- xList.add(xList.get(i - 1) + step);
- }
- for (int i = 1; i < xList.size(); i++) {
- double _tmpFx = stepF(xList.get(i - 1), yList.get(i - 1));
- double tmpFx = yList.get(i - 1) + step * stepF(xList.get(i - 1),yList.get(i - 1));
- double tmp = stepF(xList.get(i), tmpFx);
- double y = yList.get(i - 1) + step * (_tmpFx + tmp) / 2 ;
- yList.add(y);
- }
- for (int i = 0; i < N; i++) {
- double exactVal = origF(xList.get(i));
- exactYList.add(exactVal);
- diffList.add(Math.abs(exactVal - yList.get(i)));
- }
- System.out.format("\tx\tpred_kor y\texact y\t\tdiff\n");
- for (int i = 0; i < N; i++) {
- System.out.format("%.3f%10.3f%10.3f%10.3f\n", xList.get(i), yList.get(i), exactYList.get(i), diffList.get(i));
- }
- }
- }
- public class CauchyEuilerSolve {
- public static void main(String[] args) {
- EuilerSolve es = new EuilerSolve();
- es.calc();
- System.out.println("===========");
- EuilerSolve esImpr = new EuilerSolve();
- esImpr.calcImproved();
- EuilerSolve pred_kor = new EuilerSolve();
- pred_kor.pred_kor_calc();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment