-
Notifications
You must be signed in to change notification settings - Fork 114
/
001.Animated_3D_Visualization_of_Moment_of_Inertia_Problem_(Center_of_Mass).py
69 lines (59 loc) · 1.64 KB
/
001.Animated_3D_Visualization_of_Moment_of_Inertia_Problem_(Center_of_Mass).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
import sympy as sp
import plotly.express as px
import pandas as pd
# Define symbols
m, R, d = sp.symbols('m R d')
# Define equation
equation = sp.Eq(m * d**2, (13 * m * R**2))
# Solve the equation
solution = sp.solve(equation, d)
# Convert solution to a numerical function
R_value = 1.0 # Assign a value to R
solution_func = sp.lambdify(R, solution[0], 'numpy')
solution_value = solution_func(R_value)
# Create a grid of points for visualization
grid_size = 20
x_vals = np.linspace(-R_value, R_value, grid_size)
y_vals = np.linspace(-R_value, R_value, grid_size)
x_grid, y_grid = np.meshgrid(x_vals, y_vals)
z_grid = np.zeros_like(x_grid)
# Create a list of DataFrames for animation frames
frames_data = []
for frame_num in range(grid_size):
frame = pd.DataFrame({
'x': [0, solution_value],
'y': [0, 0],
'z': [0, 0]
})
frame['x_obj'] = x_vals[frame_num]
frames_data.append(frame)
# Create the animated scatter plot using Plotly Express
fig = px.scatter_3d(
frames_data[0],
x='x',
y='y',
z='z',
text=["Center of Mass", "Object Position"],
labels={'x': 'X-axis', 'y': 'Y-axis', 'z': 'Z-axis'},
animation_frame='x_obj',
title="Animated 3D Visualization of Moment of Inertia Problem"
)
# Add a surface representing the object's position
fig.add_surface(
x=x_grid,
y=y_grid,
z=z_grid,
colorscale='Viridis',
showscale=False,
opacity=0.7
)
# Customize the scene to add a line connecting the points
fig.update_layout(
scene=dict(
aspectmode="cube",
camera=dict(eye=dict(x=-1, y=-1, z=1.5))
)
)
# Show the interactive animated plot
fig.show()