Posts

Ideal Gas Laws : Coded using Python

Real gases behave in a complex way under different conditions of state variables namely Pressure, Volume and Temperature.  Chemists have first developed laws of has behavior using ideal conditions.   After the ideal laws, different scientists like Van Der Waal developed equations to incorporate the electrical forces between atoms and also space occupied by atoms. In this code, we used both these laws to calculate the pressure exerted by Argon gas due to given weight, volume and temperature.  First part of this code was to pull out unit conversions from scipy library constants to calculate value of gas constant R in different formats.  After this, we wrote three functions to calculate gas pressure by ideal gas law and using van der Waal equations. The required a,b values of different gases were stored in a dictionary and extracted when needed by using .get function. We also wrote a function, which tells us whether the calculated pressures are more or less as comp...

Coding a Physics Problem using Python

Image
The Problem:   If the position of a particle is given by x = 20^t - 5t^3, where x is in meters and t is in seconds, when, if ever, is the particle’s velocity zero? (b) When is its acceleration a zero? (c) For what time range (positive or negative) is a negative? (d) Positive?  This problem is taken from famous physics book by "Halliday". We obtained velocity by differentiating the expression with respect to time. Similar acceleration is obtained by differentiating the position expression.  Coding  Coding was done by setting up lambda functions. Setting the velocity expression to Zero, we obtained the time when velocity becomes nil.  We used list comprehensions for creating the lists of time, velocities and accelerations. Then results were printed using zip operator. The svg file is displayed below:  Code saved as SVG file  import math # 1. Kinematic Equations (Strict Python) pos = lambda t: 20 * t - 5 * t**3 vel = lambda t: 20 - 15 * t**2 acc ...

Kinematics in Code: Finding the Peak of a Motorcyclist's Journey

As a scientist, I’ve spent decades solving equations on paper. But there is a unique satisfaction in watching a physical process unfold through Python. Today, I’m looking at a classic kinematics problem: a motorcyclist moving East with a time-varying acceleration.  The Problem: A motorcyclist's acceleration is defined by a(t) = 6.1 - 1.2t m/s. For the interval 0 <= t < = 6.0, with an initial velocity of 2.7 m/s and position of 7.3 m. The code uses lambda functions for position, velocity, acceleration and also matplotlib and numpy libraries. import numpy as np import matplotlib.pyplot as plt # Physics Equations acc = lambda t: 6.1 - 1.2 * t vel = lambda t: 2.7 + 6.1 * t - 0.6 * t**2 pos = lambda t: 7.3 + 2.7 * t + (6.1 / 2) * t**2 - (1.2 / 6) * t**3 # 1. Discrete Data Generation (0 to 10 seconds) time_list = [] accel_list = [] vel_list = [] pos_list = [] print(f"{'Time(s)': 11.2f} {v:>15.2f} {a:>12.2f}") time_list.append(tm) accel_list.appe...

Kinematic Analysis of 3D Motion

This Python script performs a fundamental kinematic analysis by evaluating the position, velocity, and acceleration of a particle moving through three-dimensional space. By defining these vectors as functions of time, the program can calculate not only their magnitudes but also their direction cosines, which describe the orientation of the motion relative to the X, Y, and Z axes. Core Components Vector Decomposition: The script breaks motion down into orthogonal i, j, and k components. ​Resultant Calculation: It computes the vector magnitude using the Pythagorean theorem in 3D. ​Angular Orientation: It determines the specific angles (α,  β,  γ)  the vector makes with each primary axis. Code  import math # Function to calculate the magnitude and direction angles of a 3D vector def resulant_vec(bx, by, bz): # Magnitude: sqrt(x² + y² + z²) res = math.sqrt(bx**2 + by**2 + bz**2) # Direction Cosines: The angles the vector makes with the x, y, and z axe...

Computational Modeling of Solar Sail Trajectories using Numerical Integration

Code solves a problem given in Resnick and Halliday classic book on physics. The problem is stated as Sunjamming.  A “sun yacht” is a spacecraft with a large sail that is pushed by sunlight.Although such a push is tiny in everyday circumstances, it can be large enough to send the spacecraft outward from the Sun on a cost-free but slow trip. Suppose that the spacecraft has a mass of 900 kg and receives a push of 20 N. (a) What is the magnitude of the resulting acceleration? If the craft starts from rest, (b) how far will it travel in 1 day and (c) how fast will it then be moving?   Although they asked for a day but we have made it for any duration and also since sun's energy falls in inverse square of distance we have used a differentiation technique. Here is python code: import numpy as np import matplotlib.pyplot as plt # --- Physical Constants --- AU = 1.496e11 # 1 Astronomical Unit (m) MASS = 900 # kg F_INITIAL = 20 # Newtons at 1 AU TO...

Visualizing Particle Kinematics with Python

Understanding the relationship between position, velocity, and acceleration is fundamental to physics. However, seeing how these variables evolve simultaneously can be challenging with equations alone. In this post, we use Python, NumPy, and Matplotlib to model a particle's motion. By leveraging lambda functions and vectorized operations, we can generate a high-resolution continuous model while calculating specific discrete data points for a clear, side-by-side comparison. The Python Script import numpy as np import matplotlib.pyplot as plt # 1. Define Kinematic Functions using Lambda and NumPy # These handle both single numbers and arrays automatically pos = lambda t: 2 * np.sin(np.pi * t / 4) vel = lambda t: 2 * np.pi / 4 * np.cos(np.pi * t / 4) acc = lambda t: -2 * (np.pi / 4) ** 2 * np.sin(np.pi * t / 4) # 2. Generate Discrete Data Points time_steps = [t / 10 for t in range(21)] positions, velocities, accel = [], [], [] print(f"{'Time(s)': Conclusion As show...

Why Galaxies Don’t Fly Apart: Modeling M101 with Python

Image
In this post, I use Python to model the orbital mechanics of stars within a galaxy (using M-101 as a template). By defining a custom velocity function and using calculus to find the peak velocity point, we can visualize how star speeds change with distance and calculate the billions of years required for a single galactic orbit. Stars inside a galaxy continuously orbit to prevent falling into the center of galaxy.  The galaxy has a radius of about 90000 light years. We used numpy arrays to hold distances, speeds of stars. The data was also plotted using matplotlib.  Here is the code import numpy as np import matplotlib.pyplot as plt # Constants LY_TO_KM = 9.461e12 # More precise Light Year to KM YR_TO_SEC = 365.25 * 24 * 60 * 60 # Include leap year fraction def vel_star(x_frac): """ Models orbital speed v(x) = 350x / (1 + x^2)^(3/4) x_frac: distance in units of 10,000 ly """ return (350 * x_frac) / np.power((1 + x_frac**2), 0.75)...