I am solving a stochastic differential equation using EnsembleProblem
.
When I then simply plot a summary of the solution, I get a reasonable result.
However, when I calculate the mean myself using timeseries_point_meanvar
I find that the same solution is unstable.
Here is the ensemble summary
And here is the calculated mean with timeseries_point_meanvar
The code I am running for the 1st plot is:
using DifferentialEquations
using Plots
function rayleigh(du,u,p,t)
T, M, m, L, num_bath_particles = p
Utag(x) = -T * (L - 2* x) / (x*(L-x))
Gamma(x) = num_bath_particles * (1/x + 1/(L-x)) * sqrt(8*Ď * m * T)
du[1] = u[2]
du[2] = (-Utag(u[1]) - Gamma(u[1]) * u[2]) / M
end
function Ď_rayleigh(du,u,p,t)
T, M, m, L, num_bath_particles = p
Gamma(x) = num_bath_particles * (1/x + 1/(L-x)) * sqrt(8*Ď * m * T)
du[1] = 0
du[2] = sqrt(2* abs(Gamma(u[1])) * T) / M
end
T = 1.
M = 10^6
m = 1.
L = 10.
num_bath_particles = 1
p = [T, M, m, L, num_bath_particles]
x0 = 2.5
v0 = 0.0
prob_sde_rayleigh = SDEProblem(rayleigh, Ď_rayleigh, [x0, v0], (0.0,10^6), p)
ensembleprob = EnsembleProblem(prob_sde_rayleigh)
dt = 1000.
sim = solve(ensembleprob, EnsembleThreads(), trajectories=1000, saveat=dt)
using DifferentialEquations.EnsembleAnalysis
summ = EnsembleSummary(sim, 0:dt:10^6)
plot(summ,labels="Middle 95%", idxs=1)
and for the 2nd plot is
dt = 1000
t_to_run = 10^6
t_arr = 1:dt:t_to_run
m_series, v_series = timeseries_point_meanvar(sim, 1:dt:t_to_run)
x_mean= zeros(length(dt:dt:t_to_run))
x_var= zeros(length(dt:dt:t_to_run))
v_mean= zeros(length(dt:dt:t_to_run))
v_var = zeros(length(dt:dt:t_to_run))
for i=1:length(dt:dt:t_to_run)
x_var[i] = v_series.u[i][1]
x_mean[i] = m_series.u[i][1]
v_var[i] = v_series.u[i][2]
v_mean[i] = m_series.u[i][2]
end
plot(x_mean)
Why is there such inconsistency?