在 Julia 中的同一图形上绘制多个 ODE 解决方案的最佳方法是什么?
What is the best way to plot multiple ODE solutions on the same graphic in Julia?
我有一个 ODE,想求解它并绘制一些初始条件下的解。一开始,我决定做一个“for”来增加初始条件的值,然后绘制解决方案,一个在另一个之上。
using DifferentialEquations, Plots
for i = -2.00:0.25:2.00
f(x,p,t) = x-x^3
x0 = i
tspan = (0.00,2.00)
prob = ODEProblem(f,x0,tspan)
sol = solve(prob)
if i==-2.00
plot(sol,linewidth=4, title="Solution",
xaxis="t",yaxis="x(t)",legend=false)
else
plot!(sol,linewidth=4, title="Solution",
xaxis="t",yaxis="x(t)",legend=false)
end
end
savefig("Graphic.png")
有更好的方法吗?
您可以创建一个只有标题、轴标签等的空图,然后用解决方案填充该图:
# main plot settings
plot(linewidth=4, title="Solution",
xaxis="t",yaxis="x(t)",legend=false)
for i = -2.00:0.25:2.00
f(x,p,t) = x-x^3
x0 = i
tspan = (0.00,2.00)
prob = ODEProblem(f,x0,tspan)
sol = solve(prob)
# add to existing plot
plot!(sol)
end
我有一个 ODE,想求解它并绘制一些初始条件下的解。一开始,我决定做一个“for”来增加初始条件的值,然后绘制解决方案,一个在另一个之上。
using DifferentialEquations, Plots
for i = -2.00:0.25:2.00
f(x,p,t) = x-x^3
x0 = i
tspan = (0.00,2.00)
prob = ODEProblem(f,x0,tspan)
sol = solve(prob)
if i==-2.00
plot(sol,linewidth=4, title="Solution",
xaxis="t",yaxis="x(t)",legend=false)
else
plot!(sol,linewidth=4, title="Solution",
xaxis="t",yaxis="x(t)",legend=false)
end
end
savefig("Graphic.png")
有更好的方法吗?
您可以创建一个只有标题、轴标签等的空图,然后用解决方案填充该图:
# main plot settings
plot(linewidth=4, title="Solution",
xaxis="t",yaxis="x(t)",legend=false)
for i = -2.00:0.25:2.00
f(x,p,t) = x-x^3
x0 = i
tspan = (0.00,2.00)
prob = ODEProblem(f,x0,tspan)
sol = solve(prob)
# add to existing plot
plot!(sol)
end