Home > Python > Introduction to Python

Ch. 1.7 User Defined Function

Chapters Index
Program 1

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Define \( F(x) = x^2 \)

Program 2

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Define \( F(x) = x^2 \)

Program 3

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 4

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 5

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 6

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 7

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 8

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 9

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 10

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 11

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 12

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 13

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 14

User Defined Function

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Theoretical Principles & Computational Physics Guide

User Defined Functions: Modular Programming & Force Law Abstraction

Dr. Alorika Chatterjee & Dr. Anirban Shaw Science & Physics Learners Python 3 / NumPy / SciPy

1. Core Principles & Theoretical Framework

Modular Abstraction: Encapsulates physical force laws, potentials, and numerical algorithms into reusable functions.
Pure Functions: Clean functional design where outputs depend strictly on arguments prevents global variable bugs.
Higher-Order Functions: Pass physical force functions directly into general ODE solvers (Euler, RK4).

2. Mathematical Formulation & Algorithmic Pipeline

Force law function mapping state coordinates to physical acceleration:

$$ \mathbf{f}: (\mathbf{r}, \mathbf{v}, t) \mapsto \mathbf{a} = \frac{\mathbf{F}(\mathbf{r}, \mathbf{v}, t)}{m} $$

3. Numerical Mechanics & Implementation Logic

  • Signature Design: Specify explicit physical arguments (e.g. def lennard_jones(r, eps=1.0, sigma=1.0):).
  • Docstrings & Units: State physical units (SI, atomic units, normalized) clearly in function docstrings.
  • Multiple Returns: Return composite quantities (energy, force, status) as clean tuples.

4. Relevant Computational Practice Exercises

Exercise 1
Write a function computing the Lennard-Jones interatomic force $F(r) = -dV/dr$ and another for its potential $V(r)$.
Exercise 2
Create a general numerical derivative function <code>diff(f, x, h=1e-5)</code> using the central difference stencil.
Exercise 3
Build a modular function that solves for the terminal velocity of a falling sphere in viscous fluid with Stokes drag.
Standard Literature & References:
  • Hans Petter Langtangen, "A Primer on Scientific Programming with Python", Springer (2016).