How to resolve the algorithm Euler method step by step in the Java programming language
How to resolve the algorithm Euler method step by step in the Java programming language
Table of Contents
Problem Statement
Euler's method numerically approximates solutions of first-order ordinary differential equations (ODEs) with a given initial value. It is an explicit method for solving initial value problems (IVPs), as described in the wikipedia page. The ODE has to be provided in the following form: with an initial value To get a numeric solution, we replace the derivative on the LHS with a finite difference approximation: then solve for
y ( t + h )
{\displaystyle y(t+h)}
: which is the same as The iterative solution rule is then: where
h
{\displaystyle h}
is the step size, the most relevant parameter for accuracy of the solution. A smaller step size increases accuracy but also the computation cost, so it has always has to be hand-picked according to the problem at hand.
Example: Newton's Cooling Law Newton's cooling law describes how an object of initial temperature
T (
t
0
)
T
0
{\displaystyle T(t_{0})=T_{0}}
cools down in an environment of temperature
T
R
{\displaystyle T_{R}}
: or
It says that the cooling rate
d T ( t )
d t
{\displaystyle {\frac {dT(t)}{dt}}}
of the object is proportional to the current temperature difference
Δ T
( T ( t ) −
T
R
)
{\displaystyle \Delta T=(T(t)-T_{R})}
to the surrounding environment. The analytical solution, which we will compare to the numerical approximation, is
Implement a routine of Euler's method and then to use it to solve the given example of Newton's cooling law with it for three different step sizes of: and to compare with the analytical solution.
A reference solution (Common Lisp) can be seen below. We see that bigger step sizes lead to reduced approximation accuracy.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Euler method step by step in the Java programming language
The provided Java code demonstrates the Euler method for numerically solving ordinary differential equations (ODEs). The Euler method is a first-order numerical integration method that approximates the solution of an ODE by using the derivative of the solution at the current time step to estimate the solution at the next time step.
The euler
method in the code takes a callable f
, an initial value y0
, a starting time a
, an end time b
, and a step size h
as parameters. It then iteratively applies the Euler method to solve the ODE, printing the solution at each time step. The DONE
message is printed once the solution has been computed for all time steps.
In the main
method, the euler
method is used to solve the Newton cooling equation, which describes the rate of change of the temperature of an object as it cools. The cooling equation is given by:
dT/dt = -k(T - T_a)
where:
T
is the temperature of the objectT_a
is the ambient temperaturek
is a positive constant
In the code, the Cooling
class implements the callable interface and provides an implementation of the compute
method that returns the derivative of the Newton cooling equation. The step size is varied to demonstrate the effect of step size on the accuracy of the solution.
Here is a step-by-step explanation of the code:
- The
euler
method takes a callablef
, an initial valuey0
, a starting timea
, an end timeb
, and a step sizeh
as parameters. - It initializes the time
t
toa
and the solutiony
toy0
. - It enters a loop that continues until
t
is greater than or equal tob
. - Inside the loop, it prints the current time
t
and the current solutiony
. - It updates the time
t
by adding the step sizeh
. - It updates the solution
y
by adding the step sizeh
multiplied by the derivative of the solution at the current timet
, which is computed by calling thecompute
method of the callablef
. - Once the loop has completed, it prints the message "DONE" to indicate that the solution has been computed for all time steps.
In the main
method:
- A callable
cooling
is created that implements the Newton cooling equation. - An array
steps
is created that contains the step sizes to be used. - A loop is entered that iterates over the step sizes in the
steps
array. - For each step size, the
euler
method is called to solve the Newton cooling equation with the given step size. The initial temperature is set to 100.0, the starting time is set to 0, the end time is set to 100, and the step size is set to the current value in thesteps
array. - The solution is printed to the console.
Source code in the java programming language
public class Euler {
private static void euler (Callable f, double y0, int a, int b, int h) {
int t = a;
double y = y0;
while (t < b) {
System.out.println ("" + t + " " + y);
t += h;
y += h * f.compute (t, y);
}
System.out.println ("DONE");
}
public static void main (String[] args) {
Callable cooling = new Cooling ();
int[] steps = {2, 5, 10};
for (int stepSize : steps) {
System.out.println ("Step size: " + stepSize);
euler (cooling, 100.0, 0, 100, stepSize);
}
}
}
// interface used so we can plug in alternative functions to Euler
interface Callable {
public double compute (int time, double t);
}
// class to implement the newton cooling equation
class Cooling implements Callable {
public double compute (int time, double t) {
return -0.07 * (t - 20);
}
}
You may also check:How to resolve the algorithm Perfect numbers step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Test integerness step by step in the C# programming language
You may also check:How to resolve the algorithm Power set step by step in the Quackery programming language
You may also check:How to resolve the algorithm Comments step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the Phix programming language