Skip to content

Commit

Permalink
Add JAVA DOC to DifferentialEquation
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdalrahman-Alhamod committed Jul 8, 2023
1 parent 8cc77ba commit d092585
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 21 deletions.
40 changes: 20 additions & 20 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 108 additions & 1 deletion src/DifferentialEquation.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
import java.util.ArrayList;
import java.util.Objects;

/**
* The DifferentialEquation class represents a differential equation and provides methods for solving it numerically.
*/
public class DifferentialEquation {
String dy;
private final String dy;

/**
* Constructs a DifferentialEquation object with the given dy/dx expression.
*
* @param dy The expression representing the derivative dy/dx.
*/
public DifferentialEquation(String dy) {
this.dy = Objects.requireNonNull(dy);
}

/**
* Evaluates the differential equation at the given values of x and y.
*
* @param x The value of x.
* @param y The value of y.
* @return The value of the differential equation at (x, y).
*/
public double getValueAt(double x, double y) {
double ans = EvaluateString.evaluate(dy, x, y);
//Rounding value back to fix floating-point precision errors
ans = Math.round(ans * 1e10) / 1e10;
return ans;
}

/**
* The Euler class provides a method for solving a differential equation using the Euler method.
*/
public static class Euler {
/**
* Solves the differential equation using the Euler method.
*
* @param eq The differential equation to solve.
* @param x0 The initial value of x.
* @param y0 The initial value of y.
* @param h The step size.
* @param x The target value of x.
* @return The approximate value of y at x.
*/
public static double solve(DifferentialEquation eq, double x0, double y0, double h, double x) {
// init yi = y0 , xi = x0, yi+1 = 0
double yi = y0, xi = x0, yi1 = 0;
Expand All @@ -36,7 +64,20 @@ public static double solve(DifferentialEquation eq, double x0, double y0, double
}
}

/**
* The Taylor class provides a method for solving a differential equation using the Taylor method.
*/
public static class Taylor {
/**
* Solves the differential equation using the Taylor method.
*
* @param eqs The list of differential equations to solve.
* @param x0 The initial value of x.
* @param y0 The initial value of y.
* @param h The step size.
* @param x The target value of x.
* @return The approximate value of y at x.
*/
public static double solve(ArrayList<DifferentialEquation> eqs, double x0, double y0, double h, double x) {
// init yi = y0 , xi = x0, yi+1 = 0
double yi = y0, xi = x0, yi1 = 0;
Expand Down Expand Up @@ -68,7 +109,21 @@ public static double solve(ArrayList<DifferentialEquation> eqs, double x0, doubl

}

/**
* The MidPoint class provides a method for solving a differential equation using the Midpoint method.
*/
public static class MidPoint {
/**
* Solves the differential equation using the Midpoint method.
*
* @param eq The differential equation to solve.
* @param x0 The initial value of x.
* @param y0 The initial value of y.
* @param h The step size.
* @param x The target value of x.
* @param a2 The coefficient for the Midpoint method.
* @return The approximate value of y at x.
*/
private static double solve(DifferentialEquation eq, double x0, double y0, double h, double x, double a2) {
double a1 = 1 - a2;
double p = 1 / (2 * a2), q = 1 / (2 * a2);
Expand Down Expand Up @@ -102,26 +157,78 @@ private static double solve(DifferentialEquation eq, double x0, double y0, doubl
return yi1;
}

/**
* The ModifiedEuler class provides a method for solving a differential equation using the Modified Euler method.
*/
public static class ModifiedEuler {
/**
* Solves the differential equation using the Modified Euler method.
*
* @param eq The differential equation to solve.
* @param x0 The initial value of x.
* @param y0 The initial value of y.
* @param h The step size.
* @param x The target value of x.
* @return The approximate value of y at x.
*/
public static double solve(DifferentialEquation eq, double x0, double y0, double h, double x) {
return MidPoint.solve(eq, x0, y0, h, x, 1);
}
}

/**
* The Heun class provides a method for solving a differential equation using the Heun method.
*/
public static class Heun {
/**
* Solves the differential equation using the Heun method.
*
* @param eq The differential equation to solve.
* @param x0 The initial value of x.
* @param y0 The initial value of y.
* @param h The step size.
* @param x The target value of x.
* @return The approximate value of y at x.
*/
public static double solve(DifferentialEquation eq, double x0, double y0, double h, double x) {
return MidPoint.solve(eq, x0, y0, h, x, 0.5);
}
}

/**
* The Ralston class provides a method for solving a differential equation using the Ralston method.
*/
public static class Ralston {
/**
* Solves the differential equation using the Ralston method.
*
* @param eq The differential equation to solve.
* @param x0 The initial value of x.
* @param y0 The initial value of y.
* @param h The step size.
* @param x The target value of x.
* @return The approximate value of y at x.
*/
public static double solve(DifferentialEquation eq, double x0, double y0, double h, double x) {
return MidPoint.solve(eq, x0, y0, h, x, ((double) 2 / 3));
}
}
}

/**
* The Runge_Kutta class provides a method for solving a differential equation using the Runge-Kutta method.
*/
public static class Runge_Kutta {
/**
* Solves the differential equation using the Runge-Kutta method.
*
* @param eq The differential equation to solve.
* @param x0 The initial value of x.
* @param y0 The initial value of y.
* @param h The step size.
* @param x The target value of x.
* @return The approximate value of y at x.
*/
public static double solve(DifferentialEquation eq, double x0, double y0, double h, double x) {
// init yi = y0 , xi = x0, yi+1 = 0
double yi = y0, xi = x0, yi1 = 0;
Expand Down

0 comments on commit d092585

Please sign in to comment.