Untitled

 avatar
unknown
plain_text
a year ago
1.1 kB
4
Indexable
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
m = 1.0 
k1 = 2.0  
k2 = 3.0  
k3 = 1.0  
C = 7.0  

def model(y, t):
    x1, x2, v1, v2 = y
    dx1dt = v1
    dx2dt = v2
    dv1dt = (k2 * (x2 - x1) - k1 * x1) / m
    dv2dt = (-k2 * (x2 - x1) - k3 * x2) / m
    return [dx1dt, dx2dt, dv1dt, dv2dt]

# Initial conditions for three different cases
initial_conditions = [
    [C, -C, 0, 0],  # (i) x = C, y = -C
    [-C, -C, 0, 0],  # (ii) x = -C, y = -C
    [-C / 2, -C, 0, 0],  # (iii) x = -C/2, y = -C
]

# Time points
t = np.linspace(0, 10, 1000)

# Simulate and plot for each initial condition
for i, initial_condition in enumerate(initial_conditions):
    sol = odeint(model, initial_condition, t)
    
    # Plot the displacement-time graph for each mass
    plt.figure()
    plt.plot(t, sol[:, 0], label=f'Mass 1')
    plt.plot(t, sol[:, 1], label=f'Mass 2')
    
    # Set labels and title
    plt.xlabel('Time')
    plt.ylabel('Displacement')
    plt.title(f'Displacement-Time Graph - Case {i+1}')
    plt.legend()
    plt.grid(True)
    plt.show()
Editor is loading...
Leave a Comment