How to resolve the algorithm Euler method step by step in the Maxima programming language
How to resolve the algorithm Euler method step by step in the Maxima 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 Maxima programming language
Source code in the maxima programming language
euler_method(f, y0, a, b, h):= block(
[t: a, y: y0, tg: [a], yg: [y0]],
unless t>=b do (
t: t + h,
y: y + f(t, y)*h,
tg: endcons(t, tg),
yg: endcons(y, yg)
),
[tg, yg]
);
/* initial temperature */
T0: 100;
/* environment of temperature */
Tr: 20;
/* the cooling constant */
k: 0.07;
/* end of integration */
tmax: 100;
/* analytical solution */
Tref(t):= Tr + (T0 - Tr)*exp(-k*t);
/* cooling rate */
dT(t, T):= -k*(T-Tr);
/* get numerical solution */
h: 10;
[tg, yg]: euler_method('dT, T0, 0, tmax, h);
/* plot analytical and numerical solution */
plot2d([Tref, [discrete, tg, yg]], ['t, 0, tmax],
[legend, "analytical", concat("h = ", h)],
[xlabel, "t / seconds"],
[ylabel, "Temperature / C"]);
You may also check:How to resolve the algorithm Abelian sandpile model/Identity step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Recaman's sequence step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the Sidef programming language
You may also check:How to resolve the algorithm IBAN step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Leonardo numbers step by step in the Bash programming language