#Learning MTK - Derivatives
1 messages · Page 1 of 1 (latest)
###
@parameters t m[1:3]
###
@variables θ(t) ϕ(t) r(t)
###
begin
@variables dθ dϕ dr
[dθ, dϕ, dr] .~ Differential(t).([θ, ϕ, r])
end
###
begin
@variables ddθ ddϕ ddr
[ddθ, ddϕ, ddr] .~ Differential(t).([dθ, dϕ, dr])
end
###
expand_derivatives(ddθ)
(here's my code)
Mostly things similar to the code I just sent, but organized in different ways -
###Particle Symbolic Position
@parameters t; @variables θ(t) ϕ(t) r(t)
@variables p[1:3]; p~[θ, ϕ, r] # Position Vector
### Particle Symbolic Velocity Components
begin
dθ~D(θ)
dϕ~D(ϕ)
dr~D(r)
end
### Velocity Vector
@variables v(θ,ϕ,r,t)[1:3]; v~[r*dθ, r*sin(θ)*dϕ, dr]
### Symbolic Dipole Vector Potential
@variables A[1:3]; A ~ (μ₀ / (4π * r^3)) .* cross(m, p) # Dipole vector potential
### Symbolic Lagrangian
@variables L(θ, ϕ, r, dθ, dϕ, dr); L ~ KE-PE
KE = 0.5 * sum(collect(v .^ 2))
PE = q*dot(A, v)
### Solving Symbolic Equations of Motion
eqs=[
D(Ddθ(L))~Dθ(L)
D(Ddϕ(L))~Dϕ(L)
D(Ddr(L))~Dr(L)
]
This is some code from a previous Pluto notebook where I organized things differently
(I abandoned this and started from scratch, since I was getting confused on how things related to one another)
Since I made this post I made some jumps in understanding some things- I'll post some finished stuff tomorrow
Here's where I am right now. I went through the Julia discourse on the relevant packages and the documentation before getting here, but I might've missed something in either of those places. Here's all of my code:
using ModelingToolkit, LinearAlgebra
#### Parameters (Independent): Magnetic Vacuum Permeability, Time, Magnetic Moment, Particle Mass, Charge
@parameters μ₀, t, m[1:3], mₚ, qₚ
#### Variables (dependent): Positions
@variables θ(t), ϕ(t), r(t)
p = [θ, ϕ, r]
d=sqrt(sum(collect(p) .^ 2))
#### Differential Operators
dt = Differential(t)
#### Velocity Definitions
vθ = r * dt(θ)
vϕ= r*sin(θ)*dt(ϕ)
vr=dt(r)
v=[vθ, vϕ, vr]
#### Defining the Lagrangian
KE = 1/2 * mₚ * sum(collect(v) .^ 2)
A = μ₀/(4π*d^3) * cross(m, v)
PE = qₚ*dot(A, v)
L = KE - PE
#### Euler-Lagrange Template Function
generate_euler_lagrange(L, q) = (expand_derivatives(dt(Differential(q)(L)) - Differential(q)(L)) ~ 0)
#### Defining the Euler-Lagrange Equations
equations_of_motion = generate_euler_lagrange.(L, [θ, ϕ, r])
you don't have to isolate, just give the differential algebraic equations and let structural simplify handle them
This works great- thanks for the advice!!
Im still wondering if there's a better way to simplify these expressions though, they seem much more complicated then they should be
I derived these by hand to check against and they aren't supposed to be nearly as large
(Maybe there are easier to run? I dont know)