-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModEuler.java
95 lines (82 loc) · 2.34 KB
/
ModEuler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
public class ModEuler
{
//data
double ykMin2; //y sub k- 2
double xkMin2;
double stepSize;
double ykMin1; //y sub -1
double xkMin1;
double yk; //y sub k
double xk;
double minOfXk = 3;
//constructor
public ModEuler(double x, double y, double z)
{
ykMin2= y;
xkMin2 = x;
stepSize = z;
xk = xkMin2;
System.out.println("initial value of x = " + x);
System.out.println("///////");
System.out.println("initial value of y = " + y);
System.out.println("///////");
}
//methods
public double yPrime(double yVal, double xVal)
{
return(yVal*xVal +2*yVal);
}
public double[] DiffrentialEquation()
{
//takes in the diffrential Equation
double yprime = yPrime(ykMin2, xkMin2);
double yDoublePrime = ykMin2+ xkMin2*yprime + 2*yprime;
double yTriplePrime = yprime + yprime + xkMin2*yDoublePrime + 2*yDoublePrime;
double [] arr = {yprime, yDoublePrime, yTriplePrime};
return arr;
}
public void findYsubOne()
{
//finds new Y sub one and x sub one
double [] arr = DiffrentialEquation();
ykMin1 = ykMin2 + arr[0]*stepSize + ((arr[1]*(stepSize*stepSize))/2) +((arr[2]*(stepSize*stepSize*stepSize)/6));
xk = stepSize+ xk;
System.out.println("value of X:" + xk);
System.out.println("Y Sub One is: " + ykMin1);
System.out.println("Y sub one prime is: " + yPrime(ykMin1, xk)); System.out.println("///////");
}
public double predictedYPrime()
{
double newY = ykMin2 + 2*(stepSize)*yPrime(ykMin1, xk);
System.out.println("predicted y = " + newY);
System.out.println("///////");
double predictedYPrime = yPrime(newY,xk +stepSize);
System.out.println("predicted y prime = " + predictedYPrime);
System.out.println("///////");
return predictedYPrime;
}
public void correctedY() //find corrected value of Y
{
yk = ykMin1 + (stepSize)*((yPrime(ykMin1, xk) + predictedYPrime())/2);
xk = stepSize+ xk;
System.out.println("value of X:" + xk);
System.out.println(" new Corrected value of Y: " + yk);
System.out.println("////////////////*******////////////////");
}
public void ModEulerMeth()
{
this.findYsubOne();
while(xk<minOfXk)
{
correctedY();
ykMin2 = ykMin1;
xkMin2 = xkMin1;
ykMin1 = yk;
}
}
public static void main(String args[])
{
ModEuler ME = new ModEuler(/**initial x value**/ 0, /**initial Y value**/ 1, /**stepSize**/1);
ME.ModEulerMeth();
}
}