That’s because sol
is a special data structure (I think it should be plot(sol.t,sol[1,:])
).
In my case, I have played with 2 (or 3?) utility functions:
# Utility functions
# -- map solution function to output
function sol_2_out(sol,out,time=sol.t)
return [out(s) for s in sol.(time)]
end
# -- convert vector of samples to vector of timeseries
equal_lengths(v) = all(x->length(x)==length(first(v)), v)
#
function vec2vec(v)
equal_lengths(v) || error("The element vectors must have equal lengths.")
return [getindex.(v,i) for i in firstindex(v[1]):lastindex(v[1])]
end
;
This makes it simple to compute algebraic combinations of the solution of differential equations (without using DAE solvers):
# Output
out = s -> [s[1] + s[2],s[1]*s[2]]
#
y = sol_2_out(sol,out) |> vec2vec;
#
plot(sol.t,y)
Here, y
doesn’t have the special data structure that sol
has. If I want to use another sampling rate:
tm = range(0.,10,length=10)
y = sol_2_out(sol,out,tm) |> vec2vec
plot(tm,y)
This is convenient for, e.g., control engineers who want to plot the feedback control signal or an output, or if one wants to know, e.g., the reaction rate in a chemical reactor where the rate is a function of the states.
Maybe there is a simpler way to do this.