How to resolve the algorithm Euler method step by step in the Mathematica / Wolfram Language programming language
How to resolve the algorithm Euler method step by step in the Mathematica / Wolfram Language 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 Mathematica / Wolfram Language programming language
The provided Wolfram code solves a differential equation using the Explicit Euler method and calculates the temperature (T
) at a specific value (val
) of the independent variable (t
). Here's a detailed explanation of the code:
-
euler[step_, val_] :=
: This is a function definition that takes two arguments:step
(the step size) andval
(the value oft
at which we want to calculate the temperature). It returns the temperature value calculated using the Explicit Euler method. -
NDSolve[...]:
: This is the main differential equation solver function provided by Wolfram. It takes several arguments:{T'[t] == -0.07 (T[t] - 20), T[0] == 100}
: This is the differential equation we want to solve. It represents the change in temperature (T
) over time (t
). The specific equation given here describes the cooling of an object with an initial temperature of 100 degrees and a cooling rate proportional to the temperature difference between the object and the ambient temperature (20 degrees).T
: The dependent variable, which is the temperature.{t, 0, 100}
: The independent variable (t
) and its range. We are solving fort
values from 0 to 100.Method -> "ExplicitEuler"
: This specifies the method to use for solving the differential equation. In this case, we are using the Explicit Euler method.StartingStepSize -> step
: This sets the initial step size for the Explicit Euler method. The step size determines the accuracy and speed of the numerical solution.
-
[[1, 1, 2]][val]
: This part of the code extracts the temperature value at the specifiedval
from the solution obtained byNDSolve
.[[1, 1, 2]]
: This retrieves the second element ([[2]]
) of the first element ([[1]]
) of the first element ([[1]]
) of the solution returned byNDSolve
. In this case, the first element is a list of solutions for different step sizes, the second element is a list of time points, and the third element is a list of temperature values.[val]
: This evaluates the temperature value at the specifiedval
oft
.
In summary, this code defines a function (euler
) that solves a differential equation using the Explicit Euler method and calculates the temperature at a given value of the independent variable. It can be used to model cooling processes or other scenarios described by differential equations.
Source code in the wolfram programming language
euler[step_, val_] := NDSolve[
{T'[t] == -0.07 (T[t] - 20), T[0] == 100},
T, {t, 0, 100},
Method -> "ExplicitEuler",
StartingStepSize -> step
][[1, 1, 2]][val]
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the RPL programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the Asymptote programming language
You may also check:How to resolve the algorithm Greyscale bars/Display step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Vedit macro language programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Vala programming language