Hello everyone,
In function shown below I have a vector named vf that I want to access at some specific timepoint in my function tmp!()
.
so something like this -
time vf access
1. 0.001
2. 0.002
3. 0.003
and so on
I was just wondering if there is already a way to do this because when i tried to access vf
as vf[t]
and this doesn’t work as while solving it doesn’t give exact t
values to be used as index.
I have following function:
function tmp!(du, u, p, t)
i, j = p
quack, = u
vf = [0.001, 0.002, 0.003, 0.004]
du[1] = vf[t] * i *quack
end
what do you want this to do at non t=0.5?
for this example lets say dt is 1.
Or maybe I didnt uderstand what you meant.
ODEs are continuous in time. If the function isn’t defined at all time points (within your tspan) you don’t have an ODE. Even if dt=1
, any solver than Euler will need the value at timesteps that aren’t a multiple of dt.
1 Like
example I solved one of function in my code for value of t
and I got following results.
It was for tspan = (0, 10.0), dt = 0.01
julia> sol = solve(problem, Rodas5P(), saveat = 0.01)
0.0
0.0
0.0
0.0
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(0.0,1.0)
0.006358126895828704
0.004095798393397535
0.009769306725060716
0.0042884036095586645
0.01
0.01
0.01
0.01
0.01
0.01
0.01
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(0.01,1.0)
0.07358126895828704
0.05095798393397535
0.10769306725060715
0.05288403609558664
0.10999999999999999
0.10999999999999999
0.10999999999999999
0.10999999999999999
0.10999999999999999
0.10999999999999999
0.10999999999999999
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(0.10999999999999999,1.0)
0.5280714823594383
0.3793146163686202
0.7523697751061996
0.3919791557132927
0.7675387519140539
0.7675387519140539
0.7675387519140539
0.7675387519140539
0.7675387519140539
0.7675387519140539
0.7675387519140539
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(0.7675387519140539,1.0)
1.5015098814683507
1.2403506071163937
1.8952905582926574
1.2625846188591567
1.9219213894103646
1.9219213894103646
1.9219213894103646
1.9219213894103646
1.9219213894103646
1.9219213894103646
1.9219213894103646
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(1.9219213894103646,1.0)
2.7957387521793122
2.4848198356335662
3.264547917385764
2.5112901758452213
3.2962528192253653
3.2962528192253653
3.2962528192253653
3.2962528192253653
3.2962528192253653
3.2962528192253653
3.2962528192253653
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(3.2962528192253653,1.0)
4.377133124012223
3.992537729598177
4.957033014956837
4.025280577949373
4.9962508257073965
4.9962508257073965
4.9962508257073965
4.9962508257073965
4.9962508257073965
4.9962508257073965
4.9962508257073965
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(4.9962508257073965,1.0)
6.236796668378545
5.795389589779867
6.902358244152333
5.832969148052323
6.94736923368541
6.94736923368541
6.94736923368541
6.94736923368541
6.94736923368541
6.94736923368541
6.94736923368541
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(6.94736923368541,1.0)
8.357986975483474
7.856065454319983
9.114793322134911
7.898796959923934
9.165975066676115
9.165975066676115
9.165975066676115
9.165975066676115
9.165975066676115
9.165975066676115
9.165975066676115
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(9.165975066676115,1.0)
9.696258702711948
9.507574864872261
9.980759605675049
9.523638620128922
10.0
10.0
10.0
I just want t as increment of dt
so … 0.0, 0.01, 0.02, 0.03.....
how can I get this inside function while its solving?
That doesn’t necessarily exist for an adaptive method, nor does it always go forward, nor is it at evenly-spaced points.
Take a step back: what are you actually trying to do?
1 Like
In my model I have a function (lets say Y) which returns a vector output. This vector is basically change in some variable(lets say x) at equally spaced time-points till end of my simulation time.
Eg: Say Im simulating my model for 20 timepoints and in above function i extract values at interval = 0.1. So, 0.0 => x1, 0.1 => x2, … 20.0 => x21 where xn are different values of x calculated from function Y.
Now I want to feed this information in my differential equation function for those specific time points.
(This is just an example function to show what i’m trying to do)
function tmp!(du, u, p, t)
i, = p
quack, = u
du[1] = x[t] * i * quack. ## x is variable that comes from other function but needed to be fed here at specific time points based on interval(0.1 in this example).
end
I was thinking can callback setup help in this scenario?
Either a callback or what you can do is use DataInterpolations.jl to put an interpolation over that data and evaluate it using t
I don’t think I understood what you meant here.
Based on quick look, it feels interpolation package is basically helping to get a continuous output for your given data based on some method(making it more or less smooth). I don’t understand how it helps in above query.
Also when you say data. You mean variable x
or output of tmp()
in above shown example?
Did you mean something like this ?
x_t = LinearInterpolation(x, t)
function tmp!(du, u, p, t)
i, = p
quack, = u
du[1] = x_t[?] * i * quack # it will still need some index based on current time.
end
du[1] = x_t(t) * i * quack