Plot sol of differentialequation

``````using Plots, DifferentialEquations
function projectile_motion(v₀, θ)
g = 9.8067  # acceleration due to gravity (m/s^2)
t_final = 2 * v₀ * sind(θ) / g  # total flight time

# Define the system of second-order ODEs
function projectile!(du, u, p, t)
x, y = u
ddu = [0 -g]
end

# Initial conditions
(u₀, du₀) = [[0.0 0.0], [v₀ * cosd(θ) v₀ * sind(θ)]]

# Time span
t_span = [0.0, 20]

# Solve the ODE
prob = SecondOrderODEProblem(projectile!, du₀, u₀, t_span)
sol = solve(prob, Tsit5(), dt=0.001)

return sol  # return the value of du u
end

# Initial velocity and launch angle
v₀ = 100.0  # initial velocity in m/s
θ = 45.0   # launch angle in degrees

# Simulate projectile motion
sol = projectile_motion(v₀, θ)
``````

the type of sol.u is a Vector

Plot the results gives：

``````plot(sol, title = "Projectile Motion", xaxis = "Time in Seconds",
yaxis = "Velocity (m/s) | Position(m)",labels = ["x Velocity" "y Velocity" "x Position" "y Position"], widen = true, xlims = (0.0, 20.0), ylims = (-500.0, 1500.0))
``````

using ` twinx()` how to display velocity and position separately？ what syntax can change the structure or type of sol.u？
by the way, I want labels box off，what syntax should be used？

Try this [edited code]:

``````using Plots, Measures
plot(sol, idxs=1, c=:blue, label=false, xlabel="", ylabel="Velocity [m/s]", margin=10mm)
plot!(sol, idxs=2, c=:blues, label=false, xlabel="", yguidefontcolor=:blue)
p2 = twinx()
plot!(p2, sol, idxs=3, c=:red, label=false, xlabel="", ylabel="Position [m]", yguidefontcolor=:red)
plot!(p2, sol, idxs=4, c=:reds, label=false, xlabel="\nTime [s]")
``````
1 Like

it works ok, yet not perfect, thanks anyway

Please check the revised code above.

It produces this plot:

1 Like