import numpy as np from sklearn.linear_model import LinearRegression # Generate synthetic data for a pendulum g, L = 9.81, 1.0 theta = np.linspace(-np.pi, np.pi, 100).reshape(-1, 1) # angles from -π to π acc_true = -(g/L) * np.sin(theta) # true angular acceleration # Model 1: Linear regression on raw angle (theta) model_raw = LinearRegression().fit(theta, acc_true) pred_raw = model_raw.predict(theta) # Model 2: Linear regression on sin(theta) as feature X_feat = np.sin(theta) # physics-inspired feature model_feat = LinearRegression().fit(X_feat, acc_true) pred_feat = model_feat.predict(X_feat) # Evaluate at a sample angle test_angle = np.array([[50 * np.pi / 180]]) # 50 degrees in radians print("True acceleration:", -(g/L) * np.sin(test_angle)) print("Linear model (raw theta) prediction:", model_raw.predict(test_angle)) print("Linear model (sin(theta)) prediction:", model_feat.predict(np.sin(test_angle)))