8.2 A Differential Equation Class In general, a first-order differential equation is a function that involves not only the variable x but also the variable y. In other words, y ' = f ( x, y ) for some function f. For example, the differential equation
with the initial condition y (0) = -1 has the solution
which we can confirm by taking the derivative of the solution and then performing some algebraic manipulations:
Listing 8-0a shows class DifferentialEquation in package numbercruncher. mathutils , which is analogous to the class numbercruncher.mathutils.Function introduced in Chapter 5. We'll use this class for solving initial value problems in this chapter, and so the class includes an initialCondition instance variable. It also has the instance variable solutionLabel , which is a string label for the true, analytical solution function. Listing 8-0a Class DifferentialEquation .package numbercruncher.mathutils; import java.util.Hashtable; /** * The base class for functions that can have derivatives. * Initialize the static function table with some sample functions. */ public abstract class DifferentialEquation implements Evaluatable { /** initial condition */ private DataPoint initialCondition; /** solution function label */ private String solutionLabel; /** * Constructor. * @param initialCondition the initial condition data point * @param solutionLabel the solution function label */ public DifferentialEquation(DataPoint initialCondition, String solutionLabel) { this.initialCondition = initialCondition; this.solutionLabel = solutionLabel; } /** * Return the initial condition data point. * @return the initial condition */ public DataPoint getInitialCondition() { return initialCondition; } /** * Return the solution label. * @return the label */ public String getSolutionLabel() { return solutionLabel; } /** * Return the value of the differential equation at x. * (Implementation of Evaluatable.) * @param x the value of x * @return the solution value */ public abstract float at(float x); /** * Return the value of the differential equation at (x, y). * @return the solution value */ public float at(float x, float y) { return at(x); } /** * Return the value of the solution at x. * @return the solution value */ public abstract float solutionAt(float x); } The class implements interface numbercruncher.mathutils.Evaluatable (see Chapter 5), and so it has a method at() that takes a single x parameter. The class also has a method at() that takes both x and y parameters, which we'll use to evaluate the differential equation, and a method solutionAt() to evaluate the true solution. Listing 8-0b shows parts of the class DiffEqsToSolve , which is analogous to class numbercruncher.rootutils.RootFunctions . This class enters into its static table the differential equations, initial conditions, and analytical solutions that we'll use in this chapter. Table 8-1 shows these contents. Table 8-1. The differential equations and initial conditions used in this chapter as entered by the class DiffEqsToSolve .
Listing 8-0b A partial listing of class DiffEqsToSolve .package numbercruncher.program8_1; import java.util.Hashtable; import numbercruncher.mathutils.DataPoint; import numbercruncher.mathutils.DifferentialEquation; /** * Load into a global table the differential equations * we want to solve. */ public class DiffEqsToSolve { /** global function table */ private static Hashtable TABLE = new Hashtable(32); // Enter the differential equations into the global table. static { enterDifferentialEquations(); } /** * Return the differential equation with the given hash key * @param key the hash key * @return the differential equation */ public static DifferentialEquation equation(String key) { return (DifferentialEquation) TABLE.get(key); } /** * Enter the differential equations into the global table. */ private static void enterDifferentialEquations() { ... // Differential equation f(x, y) = 6x^2 - 20x + 11 // Initial condition y(0) = -5 // Solution y = 2x^3 - 10x^2 + 11x - 5 TABLE.put( "6x^2 - 20x + 11", new DifferentialEquation(new DataPoint(0, -5), "2x^3 - 10x^2 + 11x - 5") { public float at(float x) { return 6*x*x - 20*x + 11; } public float solutionAt(float x) { return 2*x*x*x - 10*x*x + 11*x - 5; } }); // Differential equation f(x, y) = 2xe^2x + y // Initial condition y(0) = 1 // Solution y = 3e^x - 2e^2x + 2xe^2x TABLE.put( "2xe^2x + y", new DifferentialEquation(new DataPoint(0, 1), "3e^x - 2e^2x + 2xe^2x") { public float at(float x) { return (float) (2*x*Math.exp(2*x) + solutionAt(x)); } public float at(float x, float y) { return (float) (2*x*Math.exp(2*x) + y); } public float solutionAt(float x) { return (float) (3*Math.exp(x) + 2*Math.exp(2*x)*(x - 1)); } }); ... } } |
Top |