Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.util.ArrayList;
- import java.util.Collections;
- public class Algorithm {
- //steps
- private static double t1=0.05;
- private static double t2=0.1;
- //bounds
- private static final double a=3.0;
- private static final double b=4.0;
- //initial values
- private static final double t0=3.0;
- private static final double y10=3.0;
- private static final double y20=3.0;
- //
- private static double p=3.0;
- //plases for rounding
- private static final int PLASES = 10;
- //step
- private double t;
- //Algorithm
- private void doAlgorithm(double t, double num, ArrayList<Double> y1Array, ArrayList<Double> y2Array,ArrayList<Double> tjArray){
- this.t=t;
- //temp values
- double y1=y10;
- double y2=y20;
- double tj=t0;
- double a,b;
- //
- for(int j=0; j<num; j++, tj+=t){ //j=0,19
- tjArray.add(tj);
- y1Array.add(y1);
- y2Array.add(y2);
- //iteration
- b=y21(tj,y1,y2);
- a=y11(y1,y2,b);
- y1=a;
- y2=b;
- }
- }
- //////////////////////////////////////// run algorithm
- public void run(){
- //t=t1=0.05
- int num1=(int)((b-a)/t1); //(b-a)/t
- ArrayList<Double> y1Array1=new ArrayList<>();
- ArrayList<Double> y2Array1=new ArrayList<>();
- ArrayList<Double> tjArray1=new ArrayList<>();
- doAlgorithm(t1,num1,y1Array1,y2Array1,tjArray1);
- //t=t2=0.10
- int num2=(int)((b-a)/t2); //(b-a)/t
- ArrayList<Double> y1Array2=new ArrayList<>();
- ArrayList<Double> y2Array2=new ArrayList<>();
- ArrayList<Double> tjArray2=new ArrayList<>();
- doAlgorithm(t2,num2,y1Array2,y2Array2,tjArray2);
- //calculate error for t=t1=0.05
- ArrayList<Double> y1Error=new ArrayList<>();
- ArrayList<Double> y2Error=new ArrayList<>();
- calculateError(num1,tjArray1,y1Array1,y2Array1,y1Error,y2Error);
- double error = Math.max(Collections.max(y1Error),Collections.max(y2Error));
- //TEMP
- // //calculate error for t=t2=0.1
- // ArrayList<Double> y1ErrorT=new ArrayList<>();
- // ArrayList<Double> y2ErrorT=new ArrayList<>();
- // calculateError(num2,tjArray2,y1Array2,y2Array2,y1ErrorT,y2ErrorT);
- // double errorT = Math.max(Collections.max(y1ErrorT),Collections.max(y2ErrorT));
- //calculate error Runge
- ArrayList<Double> y1Diff=new ArrayList<>();
- ArrayList<Double> y2Diff=new ArrayList<>();
- calculateDiff(num2,y1Array1, y1Array2, y1Diff);
- calculateDiff(num2,y2Array1, y2Array2, y2Diff);
- double errorRunge = Math.max(Collections.max(y1Diff),Collections.max(y2Diff));
- errorRunge=round(errorRunge/(Math.pow(2.0,p)-1.0));
- //output
- print(num1,tjArray1,y1Array1,y2Array1, error, errorRunge);
- //TEMP
- // print(num2,tjArray2,y1Array2,y2Array2, errorT, 0);
- }
- //////////////////////////////////////// calculate error
- private void calculateDiff(int num, ArrayList<Double> array1,ArrayList<Double> array2, ArrayList<Double> diff){
- double d;
- for(int i=0;i<num;i++){
- d=Math.abs(array1.get(i*2)-array2.get(i));
- diff.add(d);
- }
- }
- private void calculateError(int num, ArrayList<Double> tjArray,ArrayList<Double> y1Array,ArrayList<Double> y2Array,ArrayList<Double> y1Error,ArrayList<Double> y2Error){
- double tj;
- for(int i=0;i<num;i++){
- tj=tjArray.get(i);
- y1Error.add(round(Math.abs(u(tj)-y1Array.get(i))));
- y2Error.add(round(Math.abs(du(tj)-y2Array.get(i))));
- // System.out.printf("%2s u %14s u'%14s \n",i, round(Math.abs(u(tj)-y1Array.get(i))),round(Math.abs(du(tj)-y2Array.get(i))));
- }
- }
- //////////////////////////////////////// print output
- private void print(int num, ArrayList<Double> tjArray,ArrayList<Double> y1Array,ArrayList<Double> y2Array, double error, double errorRunge){
- System.out.printf("%2s%5s%14s%14s%14s%14s\n","j", "tj","u","u'", "y1", "y2");
- double tj;
- for(int i=0;i<num;i++){
- tj=tjArray.get(i);
- System.out.printf("%2s%5s%14s%14s%14s%14s\n",i, round(tj),round(u(tj)),round(du(tj)), round(y1Array.get(i)), round(y2Array.get(i)));
- }
- System.out.printf("Error is %14s\n",error);
- System.out.printf("Error (Runge method) is %14s\n",errorRunge);
- }
- //////////////////////////////////////// round
- private double round(double value) {
- if (PLASES < 0) throw new IllegalArgumentException();
- BigDecimal bd = new BigDecimal(Double.toString(value));
- bd = bd.setScale(PLASES, RoundingMode.HALF_UP);
- return bd.doubleValue();
- }
- //////////////////////////////////////// u(t)
- public double u(double t){
- return t*t-3.0*t+3.0;
- }
- //////////////////////////////////////// u'(t)
- public double du(double t){
- return 2.0*t-3.0;
- }
- //////////////////////////////////////// y1.j+1,y2.j+1
- //y1.j+1=F(y1.j,y2.j,b)
- public double y11(double y1, double y2, double b){
- return y1+t/2.0*(y2+b);
- }
- //y2.j+1=F(tj,y1.j, y2.j)
- public double y21(double tj, double y1 , double y2){
- double A=tj+t-2.0;
- double numerator=y2*A*A+t/2.0*f2(tj,y1,y2)*A*A+t/2.0*(tj+t+2.0)-2.0*t*y1-t*t*y2;
- double denumenator=A*A-3.0/2.0*t*A+t*t;
- return numerator/denumenator;
- }
- //////////////////////////////////////// f1,f2
- //f1=y2.j
- public double f1(double y2){
- return y2;
- }
- //f2=...
- public double f2(double tj, double y1, double y2){
- double numerator=tj+2.0+3.0*(tj-2.0)*y2-4.0*y1;
- double denumenator=(tj-2.0)*(tj-2.0);
- return numerator/denumenator;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement