Julia运行MomentClosure示例报错:DiffEqArray无indepsym字段
解决MomentClosure.jl教程中
plot!调用timeseries_steps_mean结果时的indepsym错误 问题重现
按照MomentClosure.jl官方教程复制代码时,执行以下代码段出现错误:
using DiffEqBase.EnsembleAnalysis means_SSA = timeseries_steps_mean(sol_SSA) plot!(means_SSA, lw=2, labels=["SSA μ₁₀(t)" "SSA μ₀₁(t)"], linestyle=:dash, linecolor=[1 2], background_color_legend=nothing, legend=:bottomright)
报错信息:
type DiffEqArray has no field indepsym Stacktrace: [1] getproperty @ .\Base.jl:38 [inlined] [2] macro expansion @ C:\Users\tjrgh\.julia\packages\RecursiveArrayTools\VzH8Y\src\vector_of_array.jl:439 [inlined] [3] apply_recipe(plotattributes::AbstractDict{Symbol, Any}, VA::RecursiveArrayTools.AbstractDiffEqArray) @ RecursiveArrayTools C:\Users\tjrgh\.julia\packages\RecipesBase\z10lo\src\RecipesBase.jl:300 [4] _process_userrecipes!(plt::Any, plotattributes::Any, args::Any) @ RecipesPipeline C:\Users\tjrgh\.julia\packages\RecipesPipeline\XxUHt\src\user_recipe.jl:38 [5] recipe_pipeline!(plt::Any, plotattributes::Any, args::Any) @ RecipesPipeline C:\Users\tjrgh\.julia\packages\RecipesPipeline\XxUHt\src\RecipesPipeline.jl:72 [6] _plot!(plt::Plots.Plot, plotattributes::Any, args::Any) @ Plots C:\Users\tjrgh\.julia\packages\Plots\B5j7d\src\plot.jl:223 [7] #plot!#193 @ C:\Users\tjrgh\.julia\packages\Plots\B5j7d\src\plot.jl:213 [inlined] [8] plot!(args::Any; kw::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}}) @ Plots C:\Users\tjrgh\.julia\packages\Plots\B5j7d\src\plot.jl:202 [9] top-level scope @ In[62]:4
原因分析
这个错误源于RecursiveArrayTools包的版本兼容性问题:timeseries_steps_mean返回的DiffEqArray对象在新版本中移除了indepsym字段,但旧版的Plots/RecipesBase绘图配方仍尝试访问该字段,导致属性查找失败。
解决方案
方案1:更新相关依赖包
优先更新所有相关的Julia包,修复版本兼容问题:
using Pkg Pkg.update(["RecursiveArrayTools", "Plots", "DiffEqBase", "MomentClosure"])
更新完成后重新运行原代码,通常即可解决问题。
方案2:手动提取数据绘图(绕过DiffEqArray配方)
如果更新后仍存在问题,可手动从means_SSA中提取时间序列和均值数据,直接调用plot!:
# 提取时间轴和两个变量的均值数据 t = means_SSA.t μ₁₀ = means_SSA[1, :] μ₀₁ = means_SSA[2, :] # 手动绘制曲线 plot!(t, [μ₁₀ μ₀₁], lw=2, labels=["SSA μ₁₀(t)" "SSA μ₀₁(t)"], linestyle=:dash, linecolor=[1 2], background_color_legend=nothing, legend=:bottomright)
方案3:确认sol_SSA的正确性
确保sol_SSA是通过EnsembleProblem和solve函数生成的合法EnsembleSolution对象,前面的SSA模拟代码无错误执行。
内容的提问来源于stack exchange,提问作者random487510
相关产品推荐
相关产品推荐

