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 k1,k2,k3,q1,q2,q3;
- //
- for(int j=0; j<num; j++, tj+=t){ //j=0,19
- tjArray.add(tj);
- y1Array.add(y1);
- y2Array.add(y2);
- //iteration
- k1=k1(y2);
- q1=q1(tj,y1,y2);
- k2=k2(y2,q1);
- q2=q2(tj,y1,y2,k1,q1);
- k3=k3(y2,q1,q2);
- q3=q3(tj,y1,y2,k1,q1,k2,q2);
- y1=y11(y1,k1,k2,k3);
- y2=y21(y2,q1,q2,q3);
- }
- }
- //////////////////////////////////////// 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,k1,k2,k3)
- public double y11(double y1 , double k1, double k2, double k3){
- return y1+t/6.0*(k1+4.0*k2+k3);
- }
- //y2.j+1=F(y2.j,q1,q2,q3)
- public double y21(double y2 , double q1, double q2, double q3){
- return y2+t/6.0*(q1+4.0*q2+q3);
- }
- //////////////////////////////////////// k1,q1
- //k1=f1(tj,y1.j,y2.j)
- public double k1(double y2){
- return y2;
- }
- //q1=f2(tj,y1.j,y2.j)
- public double q1(double tj, double y1, double y2){
- return f2(tj,y1,y2);
- }
- //////////////////////////////////////// k2,q2
- //k2=f1(tj+1/2t,y1.j+1/2tk1,y2.j+1/2tq1)
- public double k2(double y2, double q1){
- return y2+0.5*t*q1;
- }
- //q2=f2(tj+1/2t,y1.j+1/2tk1,y2.j+1/2tq1)
- public double q2(double tj, double y1, double y2, double k1,double q1){
- return f2(tj+0.5*t,y1+0.5*t*k1,y2+0.5*t*q1);
- }
- //////////////////////////////////////// k3,q3
- //k3=f1(tj+t,y1.j-tk1+2tk2,y2.j-tq1+2tq2)
- public double k3(double y2, double q1, double q2){
- return y2-t*q1+2.0*t*q2;
- }
- //q3=f2(tj+t,y1.j-tk1+2tk2,y2.j-tq1+2tq2)
- public double q3(double tj, double y1, double y2, double k1,double q1, double k2, double q2){
- return f2(tj+t,y1-t*k1+2.0*t*k2,y2-t*q1+2.0*t*q2);
- }
- //////////////////////////////////////// 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