Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Parameters for function #103

Closed
galbarel opened this issue Jun 20, 2016 · 2 comments
Closed

Parameters for function #103

galbarel opened this issue Jun 20, 2016 · 2 comments
Labels

Comments

@galbarel
Copy link

Hi,

My intergration is over the function:

function f(t,v,p):
(x,y) = v
dx_dt = p[1]_x - x_y
dy_dt = p[2]_x_y -y
return [dx_dt,dy_dt]
end

But ode45 does not accept my function f (as it needs to get another argument p)

If I define f without p, then how can I pass p to the function?

I need to run ode45 many times, each time with different values for p.
I've tried doing something like this:

function run_ode(f,t,v,pars)
p = pars
(times,data) = ode45(f,t,v) #here f is defined with only 2 arguments f(t,v)
return data
end

but p does not change and it keeps running with the same values.

I've also tried setting
f1(t,v) = f(t,v,p) and then running ode45(f1,t,v) but it didn't work either
(this was following the suggestion in SciML/Sundials.jl#3)

Can you please suggest how to pass the arguments in p?

@acroy acroy added the question label Jun 20, 2016
@mauro3
Copy link
Contributor

mauro3 commented Jun 20, 2016

for p in paras
g = (t,v) -> f(t,v,p)
(times,data) = ode45(g,t,v) #here f is defined with only 2 arguments f(t,v)
end

should work. (Also use backticks ` to quote code, tripple for multiline, single for in-line)

@ChrisRackauckas
Copy link
Member

For now, it's probably easiest to use closures like @mauro3 mentioned. That will have good performance and will work right now. Just to repeat:

g = (t,v) -> f(t,v,p)

will take a function with parameters f and give you g which doesn't have them, and then you can use that g with odexx. That's the and accepted answer for now and will work. I will close this since this issue is answered.


Probably should just give the full response if you're curious. This got an extensive answer in SciML/Roadmap#2. You could also use call overloading like:

type  TestFunc
         a::Float64
         b::Float64
end
f = TestFunc(0.0,0.0) # Parameters are specified here, can make a new function with the same constructor
(p::TestFunc)(t,v) = begin
         (x,y) = v
         dx_dt = p.a*x - x*y
         dy_dt = p.b*x*y -y
         return [dx_dt,dy_dt]
end

Specifying a function in this form is made easier with ParameterizedFunctions.jl. However, the one that ParameterizedFunctions.jl specifies requires a slightly different function API (an in-place update, will be more performant) which will be introduced when #49 is merged (i.e. it won't work with ODE.jl right now, but it will when ODE.jl updates). Note that this extra functionality will not necessarily be needed for ODE.jl, but will be compatible with it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants