Hello,
I am using the (fantastic) DifferentialEquation.jl
package to run some simulation and would like to plot the solution, but after applying a modifying function on the solution.
What I mean is that I would like to apply a function, for instance sin
on all the solution’s points.
In the past, I would make this work by simply not using the dedicated Plots
recipes for DifferentialEquations
and do something like this (I know the following is rather trivial):
# Define system of equation
function f(du, u, p, t)
du[1] = u[1]
end
# Parameter for system (in this case there are none)
p = []
# Initial conditions
u0 = [0.5]
# Time points to integrate
times = 0:0.01:4
# Define ODE problem
prob = ODEProblem(
f, # system to integrate
u0, # initial conditions
(times[1], times[end]), # time bounds to integrate in
p # parameters for the system
)
# Solve problem
sol = solve(
prob, # problem to solve for
saveat = times # times to get an answer for, but shifted by the transient time
)
# Plot trajectories
modifier_function(x) = sin(x)
newdata = modifier_function.(sol[1, :])
plot(sol.t, newdata)
Although this works, I feel like there must be a more systematic way of achieving the same thing. Perhaps, modifying the recipe for DifferentialEquations
?
A more “mathy” alternative would be to change the differential equations themselves to obtain the desired output, but systems of nonlinearly coupled equations it becomes a little messy.
Thanks in advance for any help!