CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual use to large groups and classes! Also, H100 GPUs starting at $2/hour.

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual use to large groups and classes! Also, H100 GPUs starting at $2/hour.

**Views:**

^{4}

**Visibility:**Unlisted (only visible to those who know the link)

**Image:**ubuntu2204

**Kernel:**Python 3 (system-wide)

# Python Lanchester's laws simulation

The Lanchester equations are a set of mathematical formulas used to model and analyze the outcome of military engagements between two opposing forces. Developed by Frederick W. Lanchester, a British engineer, during World War I, these equations provide a way to predict the attrition and effectiveness of two battling forces based on their size, firepower, and attrition rates.

The Lanchester equations come in two primary forms: linear and square. The linear form is also known as Lanchester's Law of Primitive Warfare, while the square form is known as Lanchester's Law of Modern Warfare. Here we will only consider the Linear Law (Primitive Warfare). In this model, the effectiveness of each force is assumed to be proportional to its size. It is mostly applicable to ancient or primitive warfare, where engagements were hand-to-hand combats or involved simple projectile weapons like arrows or spears. The linear equations are as follows:

$\frac{dA} {dt} = - \beta B$

$\frac{dB} {dt} = - \alpha A$

The given equations are first-order linear differential equations and can be solved using the method of separation of variables. The equations are of the form:

dx/dt = -ax

Rearrange to isolate terms involving x and t on separate sides of the equation:

dx/x = -a dt

Then integrate both sides with respect to their individual variables:

âˆ« dx/x = âˆ« -a dt

which gives:

ln|x| = -at + C

Solving for x by exponentiating both sides yields:

|x| = e^(-at + C)

Which simplifies to:

x(t) = C e^(-at)

where C is an arbitrary constant that would be determined by an initial condition. The absolute value bars are dropped because C can take any real value, including negative values.

(c) 2022 Marcin "szczyglis" SzczygliÅ„ski

GitHub page: https://github.com/szczyglis-dev/python-lanchester

Email: [email protected]

Version: 1.0.0

This file is licensed under the MIT License.

License text available at https://opensource.org/licenses/MIT

# R = red force numbers R[t] is the Red force at time = t

# B = blue force numbers B[t] os the Blue force at time = t

# b_s = offensive power of the Blue force

# r_s = offensive power of the Red force

# t = elapsed time

The differential equation for the linear case $\frac{dR}{dt}= - \beta \times B$ becomes a difference equation; R(t)-R(t-1) = - $\beta \times B(t-1) \delta t$ or R(t) = R(t-1) - $\beta \times B(t-1) \delta t$

This means the Red force strength, R, at time t-1 is reduced by the 'efficiency factor' $\beta \times$ the Blue force strength B at time t-1. This same equation is used for the reduction in the Blue force.

R.append(Rnew) is a built-in python function that places the value Rnew at the current end of the list R. for t in np.arange(0, T, dt): will loop through the indented statements using the value t that changes during each loop the changes in t are described by the numPy function np.arange(0,T,dt). t will vary from 0 to T in steps of dt. In this case T may be defined as 100 and dt as .t and t would then be, 0, .1 .2 and so on until it reached 100. the loop would then exit to the next statement.

In the next cell we will implement the linear law in python

A number of variations of these equations have been produced and make for interesting reading and exercises. The purpose of this example is to introduce a simple model and subsequent simulation.

What happens if we run this a number of time with a random value of b_1