How to resolve the algorithm Euler method step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Euler method step by step in the V (Vlang) 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 V (Vlang) programming language

Source code in the v programming language

import math
// Fdy is a type for fntion f used in Euler's method.
type Fdy = fn(f64, f64) f64
 
// euler_step computes a single new value using Euler's method.
// Note that step size h is a parameter, so a variable step size
// could be used.
fn euler_step(f Fdy, x f64, y f64, h f64) f64 {
    return y + h*f(x, y)
}
 
// Definition of cooling rate.  Note that this has general utility and
// is not specific to use in Euler's method.
 
// new_cooling_rate returns a fntion that computes cooling rate
// for a given cooling rate constant k.
fn new_cooling_rate(k f64) fn(f64) f64 {
    return fn[k](delta_temp f64) f64 {
        return -k * delta_temp
    }
}
 
// new_temp_func returns a fntion that computes the analytical solution
// of cooling rate integrated over time.
fn new_temp_func(k f64, ambient_temp f64, initial_temp f64) fn(f64) f64 {
    return fn[ambient_temp,initial_temp,k](time f64) f64 {
        return ambient_temp + (initial_temp-ambient_temp)*math.exp(-k*time)
    }
}
 
// new_cooling_rate_dy returns a fntion of the kind needed for Euler's method.
// That is, a fntion representing dy(x, y(x)).
//
// Parameters to new_cooling_rate_dy are cooling constant k and ambient
// temperature.
fn new_cooling_rate_dy(k f64, ambient_temp f64) Fdy {
    // note that result is dependent only on the object temperature.
    // there are no additional dependencies on time, so the x parameter
    // provided by euler_step is unused.
    return fn[k,ambient_temp](_ f64, object_temp f64) f64 {
        return new_cooling_rate(k)(object_temp - ambient_temp)
    }
}
 
fn main() {
    k := .07
    temp_room := 20.0
    temp_object := 100.0
    fcr := new_cooling_rate_dy(k, temp_room)
    analytic := new_temp_func(k, temp_room, temp_object)
    for delta_time in [2.0, 5, 10] {
        println("Step size = ${delta_time:.1f}")
        println(" Time Euler's Analytic")
        mut temp := temp_object
        for time := 0.0; time <= 100; time += delta_time {
            println("${time:5.1f} ${temp:7.3f} ${analytic(time):7.3f}")
            temp = euler_step(fcr, time, temp, delta_time)
        }
        println('')
    }
}


  

You may also check:How to resolve the algorithm Recaman's sequence step by step in the Raku programming language
You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the Standard ML programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the LIL programming language
You may also check:How to resolve the algorithm Huffman coding step by step in the Zig programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Run BASIC programming language