def F(C): return (9.0/5)*C + 32 def F(C): return (9.0/5)*C + 32 a = 10 F1 = F(a) # call temp = F(15.5) # call print F(a+1) # call sum_temp = F(10) + F(20) # two calls Fdegrees = [F(C) for C in [0, 20, 40]] # multiple calls def yfunc(t, v0): g = 9.81 return v0*t - 0.5*g*t**2 # sample calls: y = yfunc(0.1, 6) y = yfunc(0.1, v0=6) y = yfunc(t=0.1, v0=6) y = yfunc(v0=6, t=0.1) def yfunc(t, v0): g = 9.81 return v0*t - 0.5*g*t**2 v0 = 5 t = 0.6 y = yfunc(t, 3) def yfunc(t): g = 9.81 return v0*t - 0.5*g*t**2 t = 0.6 yfunc(t) v0 = 5 yfunc(0.6) def yfunc(t): print '1. local t inside yfunc:', t g = 9.81 t = 0.1 print '2. local t inside yfunc:', t return v0*t - 0.5*g*t**2 t = 0.6 v0 = 2 print yfunc(t) print '1. global t:', t print yfunc(0.3) print '2. global t:', t def yfunc(t): g = 9.81 global v0 # now v0 can be changed inside this function v0 = 9 return v0*t - 0.5*g*t**2 v0 = 2 # global variable print '1. v0:', v0 print yfunc(0.8) print '2. v0:', v0 def yfunc(t, v0): g = 9.81 y = v0*t - 0.5*g*t**2 dydt = v0 - g*t return y, dydt # call: position, velocity = yfunc(0.6, 3) def f(x): return x, x**2, x**4 s = f(2) s type(s) def L(x, n): x = float(x) # ensure float division below s = 0 for i in range(1, n+1): s += (1.0/i)*(x/(1+x))**i return s x = 5 from math import log as ln print L(x, 10), L(x, 100), ln(1+x) def L2(x, n): x = float(x) s = 0 for i in range(1, n+1): s += (1.0/i)*(x/(1+x))**i value_of_sum = s first_neglected_term = (1.0/(n+1))*(x/(1+x))**(n+1) from math import log exact_error = log(1+x) - value_of_sum return value_of_sum, first_neglected_term, exact_error # typical call: x = 1.2; n = 100 value, approximate_error, exact_error = L2(x, n) def table(x): print '\nx=%g, ln(1+x)=%g' % (x, log(1+x)) for n in [1, 2, 10, 100, 500]: value, next, error = L2(x, n) print 'n=%-4d %-10g (next term: %8.2e '\ 'error: %8.2e)' % (n, value, next, error) print table(10) def somefunc(arg1, arg2, kwarg1=True, kwarg2=0): print arg1, arg2, kwarg1, kwarg2 somefunc('Hello', [1,2]) # drop kwarg1 and kwarg2 somefunc('Hello', [1,2], kwarg1='Hi') somefunc('Hello', [1,2], kwarg2='Hi') somefunc('Hello', [1,2], kwarg2='Hi', kwarg1=6) somefunc(kwarg2='Hello', arg1='Hi', kwarg1=6, arg2=[2]) from math import pi, exp, sin def f(t, A=1, a=1, omega=2*pi): return A*exp(-a*t)*sin(omega*t) v1 = f(0.2) v2 = f(0.2, omega=1) v2 = f(0.2, 1, 3) # same as f(0.2, A=1, a=3) v3 = f(0.2, omega=1, A=2.5) v4 = f(A=5, a=0.1, omega=1, t=1.3) v5 = f(t=0.2, A=9) v6 = f(t=0.2, 9) # illegal: keyword arg before positional def C2F(C): """Convert Celsius degrees (C) to Fahrenheit.""" return (9.0/5)*C + 32 def line(x0, y0, x1, y1): """ Compute the coefficients a and b in the mathematical expression for a straight line y = a*x + b that goes through two points (x0, y0) and (x1, y1). x0, y0: a point on the line (floats). x1, y1: another point on the line (floats). return: a, b (floats) for the line (y=a*x+b). """ a = (y1 - y0)/(x1 - x0) b = y0 - a*x0 return a, b from math import * # in main def f(x): # in main e = exp(-0.1*x) s = sin(6*pi*x) return e*s x = 2 # in main y = f(x) # in main print 'f(%g)=%g' % (x, y) # in main def diff2(f, x, h=1E-6): r = (f(x-h) - 2*f(x) + f(x+h))/float(h*h) return r def g(t): return t**(-6) # make table of g''(t) for 13 h values: for k in range(1,14): h = 10**(-k) print 'h=%.0e: %.5f' % (h, diff2(g, 1, h)) def f(x): return x**2 - 1 f = lambda x: x**2 - 1 def g(t): return t**(-6) dgdt = diff2(g, 2) print dgdt dgdt = diff2(lambda t: t**(-6), 2) print dgdt from math import sin, pi def f(x): if 0 <= x <= pi: return sin(x) else: return 0 print f(0.5) print f(5*pi) def N(x): if x < 0: return 0 elif 0 <= x < 1: return x elif 1 <= x < 2: return 2 - x elif x >= 2: return 0 def f(x): return (sin(x) if 0 <= x <= 2*pi else 0) def double(x): # some function return 2*x def test_double(): # associated test function """Call double(x) to check that it works.""" x = 4 # some chosen x value expected = 8 # expected result from double(x) computed = double(x) success = computed == expected # boolean value: test passed? msg = 'computed %s, expected %s' % (computed, expected) assert success, msg def double(x): # some function return 2*x def test_double(): # associated test function tol = 1E-14 # tolerance for float comparison x_values = [3, 7, -2, 0, 4.5, 'hello'] expected_values = [6, 14, -4, 0, 9, 'hellohello'] for x, expected in zip(x_values, expected_values): computed = double(x) msg = '%s != %s' % (computed, expected) assert abs(expected - computed) < tol, msg def quadratic_polynomial(x, a, b, c): value = a*x*x + b*x + c derivative = 2*a*x + b return value, derivative # function call: x = 1 p, dp = quadratic_polynomial(x, 2, 0.5, 1) p, dp = quadratic_polynomial(x=x, a=-4, b=0.5, c=0) def f(x, A=1, a=1, w=pi): return A*exp(-a*x)*sin(w*x) def Simpson(f, a, b, n=500): """ Return the approximation of the integral of f from a to b using Simpson's rule with n intervals. """ h = (b - a)/float(n) sum1 = 0 for i in range(1, n/2 + 1): sum1 += f(a + (2*i-1)*h) sum2 = 0 for i in range(1, n/2): sum2 += f(a + 2*i*h) integral = (b-a)/(3*n)*(f(a) + f(b) + 4*sum1 + 2*sum2) return integral def Simpson(f, a, b, n=500): if a > b: print 'Error: a=%g > b=%g' % (a, b) return None # Check that n is even if n % 2 != 0: print 'Error: n=%d is not an even integer!' % n n = n+1 # make n even h = (b - a)/float(n) sum1 = 0 for i in range(1, n/2 + 1): sum1 += f(a + (2*i-1)*h) sum2 = 0 for i in range(1, n/2): sum2 += f(a + 2*i*h) integral = (b-a)/(3*n)*(f(a) + f(b) + 4*sum1 + 2*sum2) return integral def h(x): return (3./2)*sin(x)**3 from math import sin, pi def application(): print 'Integral of 1.5*sin^3 from 0 to pi:' for n in 2, 6, 12, 100, 500: approx = Simpson(h, 0, pi, n) print 'n=%3d, approx=%18.15f, error=%9.2E' % \ (n, approx, 2-approx) application() def test_Simpson(): # rule: no arguments """Check that quadratic functions are integrated exactly.""" a = 1.5 b = 2.0 n = 8 g = lambda x: 3*x**2 - 7*x + 2.5 # test integrand G = lambda x: x**3 - 3.5*x**2 + 2.5*x # integral of g exact = G(b) - G(a) approx = Simpson(g, a, b, n) success = abs(exact - approx) < 1E-14 # tolerance for floats msg = 'exact=%g, approx=%g' % (exact, approx) assert success, msg