# Can't make the animation work

**URL:** <https://discourse.julialang.org/t/cant-make-the-animation-work/101934>\
**Category:** Visualization\
**Tags:** question\
**Created:** [July 22, 2023, 4:09pm UTC](https://discourse.julialang.org/t/cant-make-the-animation-work/101934 "2023-07-22T16:09:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zff3141592](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zff3141592/32/51704_2.png) [@zff3141592](https://discourse.julialang.org/u/zff3141592)\
**Post date:** [July 22, 2023, 4:09pm UTC](https://discourse.julialang.org/t/cant-make-the-animation-work/101934/1 "2023-07-22T16:09:40Z")

</div>

```
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[1] = u[2]
    du[2] = -u[2]/x + (μ * g) / (x * w^2) * sqrt(1 + u[2]^2)
end

function rotation!(du, u, p, t)
    w = p[1]
    x = u[1]
    z = u[2]
    du[1] = -w * sin(w * t)
    du[2] = 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.  
Thank you, in advance!

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [July 22, 2023, 8:52pm UTC](https://discourse.julialang.org/t/cant-make-the-animation-work/101934/2 "2023-07-22T20:52:35Z")

</div>

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

```julia
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:

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![zff3141592](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zff3141592/32/51704_2.png) [@zff3141592](https://discourse.julialang.org/u/zff3141592)\
**Post date:** [July 23, 2023, 10:42am UTC](https://discourse.julialang.org/t/cant-make-the-animation-work/101934/3 "2023-07-23T10:42:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [July 24, 2023, 10:16am UTC](https://discourse.julialang.org/t/cant-make-the-animation-work/101934/4 "2023-07-24T10:16:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zff3141592](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zff3141592/32/51704_2.png) [@zff3141592](https://discourse.julialang.org/u/zff3141592)\
**Post date:** [July 24, 2023, 10:26am UTC](https://discourse.julialang.org/t/cant-make-the-animation-work/101934/5 "2023-07-24T10:26:56Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [July 24, 2023, 3:05pm UTC](https://discourse.julialang.org/t/cant-make-the-animation-work/101934/6 "2023-07-24T15:05:59Z")

</div>

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

```julia
using DifferentialEquations

function rotchain!(du, u, p, x)
    # Differential equation of the rotating chain in 3D
    w, g, μ = p # Unpack the parameters
    du[1] = u[2]
    du[2] = -u[2]/x + (μ * g) / (x * w^2) * sqrt(1 + u[2]^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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![zff3141592](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zff3141592/32/51704_2.png) [@zff3141592](https://discourse.julialang.org/u/zff3141592)\
**Post date:** [July 24, 2023, 3:28pm UTC](https://discourse.julialang.org/t/cant-make-the-animation-work/101934/7 "2023-07-24T15:28:09Z")

</div>

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[1] and dy/dx = u[2]. 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?

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [July 24, 2023, 7:23pm UTC](https://discourse.julialang.org/t/cant-make-the-animation-work/101934/8 "2023-07-24T19:23:24Z")

</div>

Oh, yes!!! It is so common `t`, that I didn’t think at all that x could be the independent variable.  
Rotating a curve about z-axis:

```julia
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))`

---

<div class="post-metadata">

**Author:** ![zff3141592](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zff3141592/32/51704_2.png) [@zff3141592](https://discourse.julialang.org/u/zff3141592)\
**Post date:** [July 25, 2023, 11:44am UTC](https://discourse.julialang.org/t/cant-make-the-animation-work/101934/9 "2023-07-25T11:44:54Z")

</div>

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!
