Knowlet

Unit 2: Computer Programming (Lab: PHYDSC354P)

1. Laboratory Objectives

The objective of this unit is to utilize C or Python to solve complex physical problems that cannot be easily addressed analytically. You will focus on algorithm development, numerical stability, and the visualization of experimental data.

2. Python for Physics: Core Concepts

Python is widely used in physics due to its readability and powerful libraries like NumPy (for numerical arrays) and SciPy (for scientific constants and functions).

Key Advantage: In Python, you don't need to worry about memory management or low-level variable declarations, allowing you to focus on the Physics Logic.

3. Finding Roots of Equations

In many physics problems, we need to find the value of x where f(x) = 0.

Newton-Raphson Method

An iterative method that uses the derivative of a function to find its roots quickly.

def newton_raphson(f, df, x0, tol): while abs(f(x0)) > tol: x0 = x0 - f(x0)/df(x0) return x0

4. Numerical Integration

This is essential when the integral of a function has no closed-form solution.

  • Trapezoidal Rule: Approximates the area under a curve as a series of trapezoids.
  • Simpson’s 1/3 Rule: Approximates the curve using parabolas, providing higher accuracy for smooth functions.

5. Matrix Operations and Linear Equations

Physics systems often lead to simultaneous linear equations. You will use Gauss-Elimination or LU Decomposition to solve these. In Python, NumPy provides the numpy.linalg module for this.

import numpy as np A = np.array([[3, 1], [1, 2]]) B = np.array([9, 8]) X = np.linalg.solve(A, B) # Solves AX = B

6. Data Visualization with Matplotlib

A critical part of any physics lab is plotting results. You will learn to create high-quality graphs with labels, legends, and grid lines.

import matplotlib.pyplot as plt plt.plot(time, velocity) plt.xlabel('Time (s)') plt.ylabel('Velocity (m/s)') plt.title('Projectile Motion') plt.show()

Lab Exam Focus Corner

Frequently Asked Questions

  • Why is Simpson's rule generally better than the Trapezoidal rule? Because it approximates the function with a quadratic polynomial rather than a linear one, leading to a smaller error term for the same number of intervals.
  • What is a 'convergence criterion'? It is the tolerance value (e.g., 10-6) used to stop an iterative process when the difference between successive results is small enough.

Common Mistakes

  • Indentation (Python): Python uses indentation to define code blocks. A single misplaced space will cause an IndentationError.
  • Zero Division: In the Newton-Raphson method, if the derivative f'(x) becomes zero at any point, the program will crash. Always include a check for this.

Practical Tips

Tip: When writing a C program for numerical methods, always use the double data type instead of float to ensure sufficient precision for physics calculations.

Did this help you understand better?

Your feedback improves the quality of this resource for everyone.