









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to computational science, focusing on the topics of derivatives, integrals, and root finding. The document also includes python code examples for numerical approximations of derivatives, integrals, and root finding. Not a programming course, but rather a resource for scientists and engineers who need to use these techniques in their research.
Typology: Assignments
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Type 1: throwing a ball into the air - time to reach ground? a)Newton’s laws, analytic solution (neglect air resistance, model as point particle) b)Real world: put in air resistance, spin of ball, can integrate the equations numerically with high accuracy.
Type 2: Show that water freezes when cooled. Features:
Errors: finite simulation time. Finite number of particles.
Want a numerical approx to (^) dxdf Why? Perhaps derivative too hard to compute analytically. Or perhaps f(x) produced by some other procedure at only a finite set of points. How? Taylor expand:
f (x + h) = f (x) + f 1 (x)h + f 2 (x)h^2 /2 +...
Many possibilities:
f 1 ∼ f (x + h) − f (x) h
or
f 1 ∼ f (x + h) − f (x − h) 2 h
Improve?
f 1 ∼ f− 2 − f 2 − 8 f− 1 + 8f 1 12 h
eg. df /dx for f = x^2 at x = 1
from math import * x=1. h=0. print (exp(x+h)-exp(x-h))/(2*h)
Rather than type in the code from scratch each time I want to change h,x can create function
from math import * def deriv(x,h): print (exp(x+h)-exp(x-h))/(2*h)
Execute: eg deriv(1,0.1), deriv(1,0.01) etc
Use quadratic approx to f in small interval
f (x) = f 0 + (f 1 − f− 1 ) 2 h
x+ f 1 − 2 f 0 + f− 1 2 h^2
x^2 +... ∫ (^) h −h
f (x)dx ∼ h 3
(f 1 + 4f 0 + f− 1 )
Leading to Simpson’s rule:
∫ (^) b
a
f dx = h 3
(f (a) + 4f (a + h) + 2f (a + 2h) +... +... 4 f (b − h) + f (b))
Eg. integrate ex^ over 0 → 1
def int(a,b,h): x=a integral=exp(a)h/2. while x<(b-h): x=x+h integral=integral+hexp(x)
integral=integral+exp(b)*h/2. print integral
Eg. solve ex^ − 0 .5 = 0 in range − 1 → 0
def root(a,b,tol): l=a u=b fl=exp(a)-0. fu=exp(b)-0. while (u-l) > tol: m=(u+l)/2. fm=exp(m)-0. if(fm*fu>0.0): u=m fu=fm else: l=m fl=fm
print m