Skip to content

Commit faeb4d7

Browse files
committed
Add OdeT! and OdeT!!, which takes a list of time values instead of an initial time value and a stopping condition
1 parent 255f096 commit faeb4d7

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

lib.ua

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ PSet ← ⍚▽⋯⇡˜ⁿ2⧻⟜¤
488488
# Import when using `Ode‼` or `Ode‼!`
489489
A‼ ←^ $"∩_(_)"+@₀⬚0⍜⧻↥₁⇌⊥10-2⊢˜⊓⊢⊏₁
490490
B‼ ↚^ $"∩_(_)"+@₀⬚0⍜⧻↥₁⇌⊥10⊢˜⊓⊢⊏₁
491+
C‼ ↚^ $"∩_(_)"+@₀⬚0⍜⧻↥₁⇌⊥10-1⊢˜⊓⊢⊏₁
491492

492493
OdeImpl‼ ↚ (
493494
# Runge-Kutta-Fehlberg 4th-5th-order variable-step-size numerical integrator
@@ -524,7 +525,11 @@ OdeImpl‼ ↚ (
524525

525526
# More configurable version of `Ode‼`
526527
#
527-
# The first of the three functions can be used to set optional arguments, namely ε, the error bound for the integration.
528+
# The first of the three functions can be used to set optional arguments:
529+
# - `ε` - error bound
530+
# - `HMin` - minimum step size
531+
# - `HMax` - maximum step size
532+
# - `H` - initial step size
528533
# The remaining two functions correspond to those of `Ode‼`.
529534
#
530535
# Example: Changing simulation precision
@@ -533,9 +538,13 @@ OdeImpl‼ ↚ (
533538
# g ← 9.81 # Gravity
534539
# L ← 1 # Pendulum length
535540
# μ ← 0.5 # Damping coefficient
536-
# Ode‼!(°⊸ε1e¯3|⋅⊃[⊣|¯+˜∩×g μ÷L∿°⊟]|<10) 0 [0 12]
541+
#
542+
# [0 12] # Initial position of 0 and velocity of 12
543+
# 0 # Initial time of 0
544+
# Ode‼!(°⊸ε 1e¯3|⋅⊃[⊣|¯+˜∩×g μ÷L∿°⊟]|<10)
537545
# ```
538546
Ode‼! ← OdeImpl‼^1^2 OdeConfig!(^0($"_New"˜▽@⊙-1⊢⋅⊢)^!^0)
547+
539548
# Numerical integrator
540549
#
541550
# Note: Due to a current bug in the interpreter, using this macro temporarily requires also importing the `A‼` macro from this library.
@@ -556,9 +565,68 @@ Ode‼! ← OdeImpl‼^1^2 OdeConfig!(^0($"_New"˜▽@⊙-1⊢⋅⊢)^!^0)
556565
# g ← 9.81 # Gravity
557566
# L ← 1 # Pendulum length
558567
# μ ← 0.5 # Damping coefficient
559-
# Ode‼(⋅⊃[⊣|¯+˜∩×g μ÷L∿°⊟]|<10) 0 [0 12]
568+
#
569+
# [0 12] # Initial position of 0 and velocity of 12
570+
# 0 # Initial time of 0
571+
# Ode‼(⋅⊃[⊣|¯+˜∩×g μ÷L∿°⊟]|<10)
560572
# ```
561573
#
562574
# Uses Runge-Kutta-Fehlberg 4th-5th-order variable-step-size numerical integration.
563575
# T X ? T₀ X₀
564576
Ode‼ ← Ode‼!∘^0^1
577+
578+
# More configurable version of `OdeT!`
579+
#
580+
# The first of the three functions can be used to set optional arguments:
581+
# - `ε` - error bound
582+
# - `HMin` - minimum step size
583+
# - `HMax` - maximum step size
584+
# - `H` - initial step size
585+
# The remaining function corresponds to that of `OdeT!`.
586+
#
587+
# Example: Simulating a damped pendulum
588+
# This sets the simulation precision threshold to a larger value (meaning less precision, with a cheaper simulation).
589+
# ```uiua
590+
# g ← 9.81 # Gravity
591+
# L ← 1 # Pendulum length
592+
# μ ← 0.5 # Damping coefficient
593+
#
594+
# [0 12] # Initial position of 0 and velocity of 12
595+
# ⍜×⇡5 10 # Time from 0 to 10 at 5 samples per second
596+
# OdeT‼(°⊸ε 1e¯3|⋅⊃[⊣|¯+˜∩×g μ÷L∿°⊟])
597+
# ```
598+
OdeT‼ ← (
599+
OdeConfig!⊃⊓C‼^0◌∘(
600+
⤙⊓C‼^0∘◌⊓C‼^0∘⊃(/↥|/↧⧈-⍆|/↧)
601+
⬚∘Ode‼!(^0⊓C‼^0∘°⊸HMax|^1|<°◌)
602+
)
603+
⟜(⤚⊏⊙⧈⊟-₁˜⨂1⊸>⌟)
604+
˜÷˜-⊙≡⊃⊢/-
605+
⊙(⍜⊙-×|°⊟⤸₁⊏|⧈⊟)
606+
)
607+
608+
# Numerical integrator
609+
#
610+
# Note: Due to a current bug in the interpreter, using this macro temporarily requires also importing the `A‼` macro from this library.
611+
#
612+
# Takes a function and two values.
613+
# The function should take a time value and a state vector and output the derivative of the state vector.
614+
# The two values should be as follows:
615+
# 1. A list of all times to output samples at
616+
# 2. The initial state vector, interpreted to be the state at time equal to the smallest value in the time value list
617+
#
618+
# Example: Simulating a damped pendulum
619+
# This simulates the motion of a damped pendulum that begins vertical with an initial angular velocity of 12 radians per second.
620+
# ```uiua
621+
# g ← 9.81 # Gravity
622+
# L ← 1 # Pendulum length
623+
# μ ← 0.5 # Damping coefficient
624+
#
625+
# [0 12] # Initial position of 0 and velocity of 12
626+
# ⍜×⇡5 10 # Time from 0 to 10 at 5 samples per second
627+
# OdeT!⋅⊃[⊣|¯+˜∩×g μ÷L∿°⊟]
628+
# ```
629+
#
630+
# Uses Runge-Kutta-Fehlberg 4th-5th-order variable-step-size numerical integration.
631+
# X ? T X₀
632+
OdeT! ← OdeT‼∘^

0 commit comments

Comments
 (0)