Practical File Physics
Practical File Physics
Aim:
To write a Python program using the `matplotlib` library to generate and display:
1. A line plot to visualize the trend of a given dataset.
2. A histogram to visualize the distribution of a given dataset.
Requirements:
Python 3, matplotlib, sample datasets for line plot and histogram, text editor/IDE.
Procedure:
Output:
Observation: Line plot reveals data trends; histogram shows data frequency
distribution.
Procedure:
Output:
Procedure:
import numpy as np
import matplotlib.pyplot as plt
population_distribution = 'uniform'
population_parameters = {'low': 0, 'high': 1}
population_mean = np.mean([population_parameters['low'],
population_parameters['high']])
population_std = np.std(np.random.uniform(population_parameters['low'],
population_parameters['high'], 100000))
num_samples = 1000
sample_size = [5, 30, 100]
fig, axes = plt.subplots(len(sample_size), 1, figsize=(10, 5 * len(sample_size)))
fig.suptitle(f'Demonstration of Central Limit Theorem (Population:
{population_distribution.capitalize()})', fontsize=16)
for i, n in enumerate(sample_size):
sample_means = []
for _ in range(num_samples):
if population_distribution == 'uniform':
sample = np.random.uniform(population_parameters['low'],
population_parameters['high'], n)
elif population_distribution == 'exponential':
sample = np.random.exponential(1/population_mean, n)
elif population_distribution == 'poisson':
sample = np.random.poisson(population_mean, n)
else:
raise ValueError("Unsupported population distribution")
sample_means.append(np.mean(sample))
axes[i].hist(sample_means, bins=30, density=True, alpha=0.6, color='skyblue')
mean_of_means = np.mean(sample_means)
std_of_means = population_std / np.sqrt(n)
x = np.linspace(min(sample_means), max(sample_means), 100)
axes[i].plot(x, 1/(std_of_means * np.sqrt(2 * np.pi)) * np.exp( - (x -
mean_of_means)**2 / (2 * std_of_means**2) ),
'r', linewidth=2, label=f'N({mean_of_means:.2f},
{std_of_means:.2f}^2)')
axes[i].set_xlabel('Sample Mean')
axes[i].set_ylabel('Density')
axes[i].set_title(f'Sample Size = {n}')
axes[i].legend()
axes[i].grid(True, linestyle='--', alpha=0.6)
Output:
Precaution: Ensure numpy and matplotlib are installed. Experiment with different
population distributions and sample sizes.
Procedure:
import numpy as np
import matplotlib.pyplot as plt
initial_height = 100
initial_velocity = 0
gravity = 9.81
air_resistance_coefficient = 0.01
mass = 0.1
time_step = 0.01
total_time = 10
time = [0]
height = [initial_height]
velocity = [initial_velocity]
current_time = 0
current_height = initial_height
current_velocity = initial_velocity
plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
plt.plot(time, height)
plt.xlabel("Time (s)")
plt.ylabel("Height (m)")
plt.title("Height vs. Time (Free Fall with Air Resistance)")
plt.grid(True)
plt.subplot(2, 1, 2)
plt.plot(time, velocity)
plt.xlabel("Time (s)")
plt.ylabel("Velocity (m/s)")
plt.title("Velocity vs. Time (Free Fall with Air Resistance)")
plt.grid(True)
plt.tight_layout()
plt.show()
Output:
Procedure:
import numpy as np
import matplotlib.pyplot as plt
initial_velocity = 30
launch_angle_degrees = 45
gravity = 9.81
time_step = 0.01
launch_angle_radians = np.deg2rad(launch_angle_degrees)
x = [0]
y = [0]
vx = initial_velocity * np.cos(launch_angle_radians)
vy = initial_velocity * np.sin(launch_angle_radians)
time = [0]
current_time = 0
plt.figure(figsize=(8, 6))
plt.plot(x, y)
plt.xlabel("Horizontal Distance (m)")
plt.ylabel("Vertical Height (m)")
plt.title("Projectile Trajectory")
plt.grid(True)
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.show()
Output: