How to resolve the algorithm Euler method step by step in the Java programming language

Published on 12 May 2024 09:40 PM

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 object
  • T_a is the ambient temperature
  • k 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:

  1. The euler method takes a callable f, an initial value y0, a starting time a, an end time b, and a step size h as parameters.
  2. It initializes the time t to a and the solution y to y0.
  3. It enters a loop that continues until t is greater than or equal to b.
  4. Inside the loop, it prints the current time t and the current solution y.
  5. It updates the time t by adding the step size h.
  6. It updates the solution y by adding the step size h multiplied by the derivative of the solution at the current time t, which is computed by calling the compute method of the callable f.
  7. 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:

  1. A callable cooling is created that implements the Newton cooling equation.
  2. An array steps is created that contains the step sizes to be used.
  3. A loop is entered that iterates over the step sizes in the steps array.
  4. 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 the steps array.
  5. 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