1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! # Calculus of Variations Module
//!
//! The `calculus_of_variations` module provides tools for solving problems in the calculus of variations.
//! This field of mathematical analysis deals with finding functions that maximize or minimize functionals
//! (integrals of a function and its derivatives).
//!
//! The core functionality of this module is the derivation and solving of the Euler-Lagrange equation,
//! which is a fundamental equation in this field.
use Arc;
use cratedifferentiate;
use crateExpr;
use cratesolve_ode;
use cratesimplify;
/// # Euler-Lagrange Equation
///
/// Computes the Euler-Lagrange equation for a given Lagrangian functional.
///
/// The Euler-Lagrange equation is the fundamental equation of the calculus of variations.
/// For a functional $S = \int L(t, q, \dot{q}) dt$, the condition for $S$ to be stationary is:
///
/// $$\frac{d}{dt} \left( \frac{\partial L}{\partial \dot{q}} \right) - \frac{\partial L}{\partial q} = 0$$
///
/// where:
/// - $L$ is the Lagrangian (the integrand).
/// - $t$ is the independent variable.
/// - $q(t)$ is the dependent variable (generalized coordinate).
/// - $\dot{q} = dq/dt$ is the generalized velocity.
///
/// ## Arguments
/// * `lagrangian` - An [`Expr`] representing the Lagrangian $L$.
/// * `func` - The name of the dependent function $q$ as a string.
/// * `var` - The name of the independent variable $t$ as a string.
///
/// ## Returns
/// An [`Expr`] representing the left-hand side of the Euler-Lagrange equation.
///
/// ## Example: Free Particle
/// The Lagrangian for a free particle of mass $m$ is $L = \frac{1}{2} m \dot{x}^2$.
/// ```rust
/// use rssn::symbolic::calculus_of_variations::euler_lagrange;
/// use rssn::symbolic::core::Expr;
/// use std::sync::Arc;
///
/// let m = Expr::new_variable("m");
///
/// let x = Expr::new_variable("x");
///
/// let t = Expr::new_variable("t");
///
/// let x_prime = Expr::new_derivative(x.clone(), "t".to_string());
///
/// // L = 1/2 * m * (x')^2
/// let lagrangian = Expr::new_mul(
/// Expr::new_mul(Expr::Constant(0.5), m),
/// Expr::new_pow(&x_prime, Expr::Constant(2.0)),
/// );
///
/// let eq = euler_lagrange(&lagrangian, "x", "t");
/// // Result should be simplified to: m * d^2x/dt^2
/// ```
/// # Solve Euler-Lagrange Equation
///
/// Automatically generates and attempts to solve the Euler-Lagrange equation for a system.
///
/// This is a convenience function that computes the Euler-Lagrange equation as an ODE
/// and immediately passes it to the `solve_ode` engine.
///
/// ## Arguments
/// * `lagrangian` - The Lagrangian functional.
/// * `func` - The name of the function to solve for.
/// * `var` - The independent variable.
///
/// ## Returns
/// An [`Expr`] representing the general or particular solution to the system's motion.
/// # Hamilton's Principle (Least Action)
///
/// Derives the equations of motion for a physical system using the principle of stationary action.
///
/// Hamilton's Principle states that for a conservative system, the actual path $q(t)$ taken
/// by the system makes the action $S = \int L dt$ stationary.
///
/// This function is an alias for [`euler_lagrange`], providing the terminology used in physics.
///
/// ## Arguments
/// * `lagrangian` - The Lagrangian $L = T - V$ (Kinetic - Potential energy).
/// * `func` - The generalized coordinate $q$.
/// * `var` - The time variable $t$.