1

I am fairly new to Julia, so apologies in advance. I am trying to simultaneously solve two differential equations in Julia and having trouble. I have a MWE in Python, which is:

import numpy as np
from scipy import solve_ivp

def derivatives(T,y):
    
    T = y[0]
    t = y[1]
    
    dtdT = T**2
    dTdT = T**2
    
    return dTdT, dtdT

def solver(Ti, Tf, n):
    
    t_eval = np.geomspace(Ti, Tf, n)
    t_span = [Ti, Tf]
    y0 = [Ti, 0]

    TSolved = solve_ivp(derivatives, t_span, y0, t_eval)
    
    return TSolved.t, TSolved.y[0], TSolved.y[1]

This works and behaves how I would like it to. I've tried doing the following in Julia (same problem), and it gives me trouble:

using DifferentialEquations

function Derivatives(T,y)

   T = y[1]
   t = y[2]
 
   dtdT = T^2
   dTdT = T^2
   
   return dTdT, dtdT
 
end

function Solver(Ti, Tf, n)
   
    saveat = vec(10 .^ range(log(Ti), log(Tf), length=n))
    TSpan = (Ti, Tf)
    T0 = (Ti, 0)
    DiffEq = ODEProblem(Derivatives, TSpan, T0)
    Solution = solve(DiffEq, saveat=saveat)
    
    return Solution

end

No matter how I edit it, I am getting a MethodError in Julia. I believe the problem is with how I am handling/inputting vectors and arrays, but I'm not at a bit of a loss. TIA!

Aidan
  • 21
  • 6
  • Have you actually tried to run the Python version? I get an error trying it (Python 3.9.7) – Bill May 26 '22 at 17:17

1 Answers1

0

"Solving IVP with vectors", the issue is that you aren't using any vectors here. return dTdT, dtdT isn't a vector, it's a tuple.

function Derivatives(T,y)

   T = y[1]
   t = y[2]
 
   dtdT = T^2
   dTdT = T^2
   
   return [dTdT, dtdT]
 
end

will be fine. Well, also make sure the ODEProblem is defined as ODEProblem(f,u0,tspan): you flipped the time span definition and initial condition (DiffEq = ODEProblem(Derivatives, T0, TSpan))

Chris Rackauckas
  • 18,645
  • 3
  • 50
  • 81