ameliorer :import numpy as np import matplotlib.pyplot as plt import plotly.express as…
- User
ameliorer :import numpy as np import matplotlib.pyplot as plt import plotly.express as px from scipy.integrate import odeint
def pendulum_triple(y, t, l, m, g): theta1, omega1, theta2, omega2, theta3, omega3 = y dydt = [omega1, -(g/l)*np.sin(theta1), omega2, -(g/l)*np.sin(theta2), omega3, -(g/l)*np.sin(theta3)] return dydt
l = 1.0 m = 1.0 g = 9.81
y0 = [np.pi/4, 0, np.pi/2, 0, np.pi/4, 0] t = np.linspace(0, 10, 100)
sol = odeint(pendulum_triple, y0, t, args=(l, m, g))
theta1 = sol[:, 0] theta2 = sol[:, 2] theta3 = sol[:, 4]
Matplotlib
plt.figure() plt.plot(t, theta1, label='Theta1') plt.plot(t, theta2, label='Theta2') plt.plot(t, theta3, label='Theta3') plt.xlabel('Time') plt.ylabel('Angle') plt.legend() plt.show()
Plotly
fig = px.line(x=t, y=np.column_stack((theta1, theta2, theta3)), labels={'x': 'Time', 'y': 'Angle'}, title='Pendulum Motion') fig.show()
- Assistant
Importing the necessary libraries
import numpy as np import matplotlib.pyplot as plt import plotly.express as px from scipy.integrate import odeint
Function to calculate the motion of a triple pendulum
def pendulum_triple(y, t, l, m, g): theta1, omega1, theta2, omega2, theta3, omega3 = y dydt = [omega1, -(g/l)*np.sin(theta1), omega2, -(g/l)*np.sin(theta2), omega3, -(g/l)*np.sin(theta3)] return dydt
Constants and initial conditions
l = 1.0 m = 1.0 g = 9.81 y0 = [np.pi/4, 0, np.pi/2, 0, np.pi/4, 0] t = np.linspace(0, 10, 100)
Solving the differential equation
sol = odeint(pendulum_triple, y0, t, args=(l, m, g))
Extracting the angles of each pendulum
theta1 = sol[:, 0] theta2 = sol[:, 2] theta3 = sol[:, 4]
Matplotlib plot
plt.figure() plt.plot(t, theta1, label='Theta1') plt.plot(t, theta2, label='Theta2') plt.plot(t, theta3, label='Theta3') plt.xlabel('Time') plt.ylabel('Angle') plt.legend() plt.title('Triple Pendulum Motion (Matplotlib)') plt.show()
Plotly plot
fig = px.line(x=t, y=np.column_stack((theta1, theta2, theta3)), labels={'x': 'Time', 'y': 'Angle'}, title='Triple Pendulum Motion (Plotly)') fig.show()
- User
systeme equations non lineaire d ordre deux avec numpy
- Assistant
import numpy as np from scipy.optimize import fsolve
def equations(vars): x, y = vars[0], vars[1] eq1 = x2 + y2 - 25 eq2 = x**2 - y return [eq1, eq2]
Guess values for the variables
initial_guess = [1, 1]
Solve the non-linear system of equations
solution = fsolve(equations, initial_guess)
print("Solution: x =", solution[0], ", y =", solution[1])