Hello everyone, I am new to julia and try to use it to solve my problem, which involves solving coupled differential equations. I will show my problem below:
Here is my attempted solution:
using DifferentialEquations, LinearAlgebra
# Constants
ħ = 1.0 # Reduced Planck's constant (in natural units)
m = 1.0 # Particle mass
k_max = 5.0 # Maximum momentum
N = 50 # Number of momentum points
B_span = (0.0, 10.0) # Range of B (flow time)
# Define the momentum grid
k_grid = range(-k_max, k_max, length=N)
dk = step(k_grid)
# Define the energy function E_k
E_k = k -> ħ^2 * k^2 / (2m)
# Define the ODE system for dg_{k',k}/dB
function dg_matrix_dB(g_vec, B)
N = 50
g_matrix = reshape(g_vec, (N, N)) # Reshape vector to matrix
dg_matrix = zeros(N, N)
for i in 1:N # Iterate over k'
for j in 1:N # Iterate over k
E_1 = g_matrix[i,i]
E_2 = g_matrix[j,j]
# First term
term1 = -((E_1 - E_2)^2) * g_matrix[i, j]
# Summation term
term2 = 0.0
for p in 1:N
E_p = g_matrix[p,p]
term2 += (E_1 + E_2 - 2 * E_p) * g_matrix[i, p] * g_matrix[p, j]
end
dg_matrix[i, j] = term1 + term2 # * dk # Include dk for integration
end
end
return reshape(dg_matrix, N^2) # Return as vector
end
# Define the initial condition for g_{k',k}
c = 1.0 # Constant part of g_{k',k}
g_initial = zeros(N, N)
for i in 1:N
for j in 1:N
if i == j
g_initial[i, j] = c + E_k(k_grid[i]) # Diagonal: constant + energy
else
g_initial[i, j] = c # Off-diagonal: constant
end
end
end
# Reshape the initial condition for ODE solver
g0 = reshape(g_initial, N^2)
prob = ODEProblem(dg_matrix_dB, g0, B_span)
sol = solve(prob, Tsit5())
But it seems that the error indicates I use some sqrt function, which I do not explicitly use it, so I am very confused. My guess was it had something to do with vector to matrix conversion?
MethodError: no method matching sqrt(::Tuple{Int64}) Closest candidates are: sqrt(!Matched::Union{Float32, Float64}) at math.jl:590 sqrt(!Matched::StridedMatrix{T}) where T<:Union{Real, Complex} at /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/dense.jl:853 sqrt(!Matched::Union{Static.StaticBool{N}, Static.StaticFloat64{N}, Static.StaticInt{N}}) where N at ~/.julia/packages/Static/IYKUj/src/Static.jl:442 …