# Can't make the animation work

using DifferentialEquations
using Plots

function rotchain!(du, u, p, x)
# Differential equation of the rotating chain in 3D
w, g, μ = p  # Unpack the parameters
du = u
du = -u/x + (μ * g) / (x * w^2) * sqrt(1 + u^2)
end

function rotation!(du, u, p, t)
w = p
x = u
z = u
du = -w * sin(w * t)
du = w * cos(w * t)
end

function rotchain_solve(L, w, g, μ, num_points)
xspan = (0.1, L)
u0 = [1.0, 1.0]   # Initial values for u and du/dx at x=0

# Solving the differential equation
prob1 = ODEProblem(rotchain!, u0, xspan, [w, g, μ])
sol1 = solve(prob1, Tsit5(), saveat = L / (num_points - 1) * (0:num_points-1))        # saveat for
equally scattered points to compute the numerical u(x)

return sol1
end

function rotation_solve(w, L, num_points)
tspan = (0.1, L)
u0 = [1.0, 0.0]

prob2 = ODEProblem(rotation!, u0, tspan, [w])
sol2 = solve(prob2, Tsit5(), saveat = L / (num_points - 1) * (0:num_points-1))

return sol2
end

#x_values = sol1.t .* sol2[1, :]
#y_values = sol1[1, :]
#z_values = sol1.t .* sol2[2, :]

gr()

function plot_Rot_Chain_3D(sol1, sol2)
anim = @animate for i in 1:length(sol2.t)
x_vals = sol1.t[i] .* sol2[1, i]
y_vals = sol1[1, i]
z_vals = sol1.t[i] .* sol2[2, i]

plot([x_vals], [y_vals], [z_vals], mode="markers", name="Rotating Chain")
end

gif(anim, "rotating_chain.gif", fps = 5)
end

μ = 10
L = 1
w = π / 2
g = 9.81
num_points = 100

# Solution and plotting
sol1 = rotchain_solve(L, w, g, μ, num_points)
sol2 = rotation_solve(w, L, num_points)
plot_Rot_Chain_3D(sol1, sol2)


I wrote this code to solve the problem, in which we hang down a chain and rotate it qroud this axis(y-axis). At the end I tried to make an animation, in which the chain rotates around the y-axis, while its shape is determined, but the actual animation part did not really worked. For so much time I gave me different kind of errors, like Assertion or Method errors. Now it generates an animation, but it does not look like as it should. Acctually there is nothing on the plot. I would really appriciate if anyone could help me.

Defining the function trajectory, and modifying your function plot_Rot_Chain_3D, as below, the animation works:

function trajectory(sol1, sol2)
xv= sol1.t .* getindex.(sol2.u, 1)
yv= getindex.(sol1.u, 1)
zv = sol1.t .* getindex.(sol2.u, 2)
return [xv, yv, zv]
end

function plot_Rot_Chain_3D(xv, yv, zv)
anim = @animate for i in 1:length(xv)
plot!(xv[1:i], yv[1:i], zv[1:i],  lw = 2, linecolor = :blue,
xlims=extrema(xv), ylims=extrema(yv),
zlims=extrema(zv), title = "Rotating chain",
legend=false)

end
gif(anim, "rotating_chain.gif", fps = 8)
end
μ = 10
L = 1
w = π / 2
g = 9.81
num_points = 100

# Solution and plotting
sol1 = rotchain_solve(L, w, g, μ, num_points)
sol2 = rotation_solve(w, L, num_points);
plot_Rot_Chain_3D(trajectory(sol1, sol2)...)


If you prefer plotting markers, instead of trajectory, then define the animation as follows:

anim = @animate for i in 1:length(xv)
scatter3d!([xv[i]], [yv[i]], [zv[i]], mode="markers", color=:blue, markersize=2,
legend=false, xlims=extrema(xv), ylims=extrema(yv), zlims=extrema(zv))
end
gif(anim, "rotating_chain.gif", fps = 8)


I did not wanted to plot and animate this, motion. Maybe I was misunderstandable. My main aim is to create an animation in which there is an axis, let this axis be the y-axis. At one point in the y axis the chain is hang up and it rotates round and round. The rotchain functions solves the shape of the chain, which becomes onn account of the rotation. The rotation functions meant to make the x and z coordinates to rotate time dependentedly. At the plot_Rot_Chain_3D function I wanted to generate the points of the rotation of that certain shaped chain. Sadly it didn’t work. Sorry for the unconvinience.

I’ve just animated the points generated in your original function plot_Rot_Chain_3D. Since you called plot, not plot!, the points were not preserved from a frame to another.
My only modification of your code was to preserve the position of previously displayed points, and the computation of all points at the begining, instead step by step, but within for loop of @animate thy are released one by one.

How is meant y -axis in your consideration? In Plots it is not the vertical axis, but included in the horizontal plane.

As you can see, I am new to julia and to tell the truth to programming. I am a physics student who had enough that some Differential Equations cannot be solved. In my consideration the plot is the original cartisan co-ordinate. x-axis pointing to the right, y-axis pointing up, and z-axis pointing from us to the “paper”.

This ODE defined by the function rotchain! depends on x, but I cannot detect in your code its value. , why isn’t it included as a coordinate of the parameter vector, p?
The following code leads to a solution that suddenly increases from 1.xx to +40000

using DifferentialEquations

function rotchain!(du, u, p, x)
# Differential equation of the rotating chain in 3D
w, g, μ = p  # Unpack the parameters
du = u
du = -u/x + (μ * g) / (x * w^2) * sqrt(1 + u^2)
end
function rotchain_solve(L, w, g, μ, num_points)
xspan = (0.1, L)
u0 = [1.0, 1.0]   # Initial values for u and du/dx at x=0

# Solving the differential equation
prob1 = ODEProblem(rotchain!, u0, xspan, [w, g, μ])
sol1 = solve(prob1, Tsit5(), saveat = L / (num_points - 1) * (0:num_points-1))        # saveat for
#equally scattered points to compute the numerical u(x)

return sol1
end

μ = 10
L = 1
w = π / 2
g = 9.81
num_points = 100
sol1 = rotchain_solve(L, w, g, μ, num_points)


Look at these values:

u: 90-element Vector{Vector{Float64}}:
[1.0013200891749168, 1.646003720367785]
[1.1974570842264627, 71.93947784201363]
[7.387964075147161, 2096.4798878244983]
[155.03170989833123, 46638.3141887749]
[2932.7359485525685, 824257.3218376525]
[45539.1506614695, 1.1949296124022432e7]
[592444.7009345402, 1.4574480454835212e8]
[6.598026775340614e6, 1.527658747567294e9]
[6.4012664751832835e7, 1.3997520253526678e10]


I’m wodering how this solution was computed without the missing x value.

In the first, rotchain! function x was the variable, which respect we constructed the derivatives. It is like in a time dependent problem the t. As you move along the x axis, what shape the chain must have. So y is a function of x, y(x) = u and dy/dx = u. My main problem is exactly that I cannot, or I don’t know how to contain the time dependece in the rotchain! function. I tried to construct a different function for the rotation, rotation!, which is indeed a function of time.
So my main aim is that if I have a plot, it can be any plot, how can I make a 3d rotation of that plot around the y, or the upwards pointing axis?

Oh, yes!!! It is so common t, that I didn’t think at all that x could be the independent variable.

using Plots, Rotations
t = range(0, 4π, 200)
x = cos.(t)
y = sin.(t)
z = 0.75*t
trot = range(0, 2π, 36)
anim = @animate for θ∈trot
posc=RotZ(θ) *permutedims(hcat(x, y, z))
plot(posc[1, :], posc[2,:], posc[3, :],  lw = 3, linecolor = :blue,
legend=false)
end
gif(anim, "rotating-curve.gif", fps = 8)


hcat(x,y,z) is a matrix of three columns, and permutedims(hcat(x,y,z) is its transposed matrix.
The matrix of rotation of angle \theta, about z-axis is 3x3 matrix, so we can rotate all curve points at once by the multiplication RotZ(θ)*permutedims(hcat(x, y, z))

Oh my god!! It’s actually just linear algebra. So the RotZ(θ) “function” is actually the transformation matrix. Okay, then I will proceed as you showed with my problem. Thank you so much!