Unformatted text preview: Animations in pylab (finally!) Difference equations (Chap. 5.1) Recursive equations (Chap. 5.1) Make sure you have downloaded material off my website (including HW#3). My solutions for HW#1 & HW#2 are also there. We can add a new dimension to a plot by animating the ‘evolution’ of an equation (in space, time, or some other parameter) Can do this in pylab by drawing an initial curve (line) and updating the y values at each step using line.set_ydata() command and redrawing the curve using draw(). Can view it dynamically on the screen, or output each frame to a file using savefig() command. Then convert all frames to an animated GIF or Flash file for presentations or web. This shows a sine wave with an increasing frequency (decreasing wavelength). Can also couple this with interactivity (clicking, key presses) to do some pretty neat things! step through frame by frame from pylab import * change a parameter by def mysin(x,k): return sin(k*x) pressing a key and updating x=linspace(0,4*np.pi,100) the curve #NOTE , after line! line, = plot(x,mysin(x,1), b- ) pausing an animation axis([min(x),max(x),-1.1,1.1]) More complicated example: k=linspace(0.2,30,40) for ki in k: reflecting Gaussian wavepacket line.set_ydata(mysin(x,ki)) with options to save frames draw() to a file. (output on previous slide) Typically describe the evolution of a quantity where the current value depends on the previous value (s). xn = axn −1 Many examples: compound interest, population growth, nuclear chain reactions, … These type of equations are typically simple to write down in difference form, but very difficult to write in closed form. Perfect for computer solutions! Examples we’ll look at: Fibonacci sequence logistic population growth Calculation of compound interest (in your book) Classic mathematical sequence of numbers defined by: Fn = Fn −1 + Fn − 2 F0 = 0, F1 = 1
Turns out to have interesting geometric consequences (sides of squares, spirals) Several different origins, but first invented in the West by Fibonacci in 1202 to (incorrectly) model rabbit population. def fib(n): Returns the nth fibonacci number if n==0: return 0 if n==1: return 1 f=[0,1] for i in range(2,n+1): fi=f[i-1]+f[i-2] f.append(fi) return f[-1] How could you improve the memory management of this code? A model for population growth says the population at time t (xt) depends on the population at the previous time (xt-1). Initially, the population has lots of resources and grows at a rate ρ, but eventually resources become more scarce and population approaches a maximum (M) called the carrying capacity. Write a function to compute the population vs time with: xt=0=100, ρ=1 and M=1500 from t=[0,1000] ρ xt −1 ⎞ ⎛ xt = xt −1 + xt −1 ⎜ 1 − ⎟ ⎝ 100 M⎠ Functions which call themselves with a new parameter. Must have some end condition which eventually returns a value. The value is returned ‘up the chain’ and updated with call of the function. Recursive version of def fibR(n): the Fibonacci function: Returns the nth fibonacci number These can be powerful, if n==0: return 0 but tricky (try putting a if n==1: return 1 ‘print fi’ line in to see else: fi = fibR(n-1) + fibR(n-2) intermediate values). return fi Binary searches are an efficient way to search a large sorted list of objects. Given a list S, search for a number N in the list. Start with the midpoint in the list S and see if that is the number. If not… if N is lower than the midpoint, repeat the search with the lower half of the list. If N is higher than the midpoint, repeat the search in the upper half of the list. Repeat until the number is found and return its location or return -1 if it is not found. def BinSearch(S,N,Low,High): if Low > High: print 'The number %i is not in the list.'%N return -1 Mid = int((High + Low)/2.0) if N == S[Mid]: print 'The number %i was found at position %i in the list ' % (N,M) return Mid elif N < S[Mid]: BinSearch(S,N,Low,Mid-1) else: BinSearch(S,N,Mid+1,High) import random S=[random.randint(0,100) for i in range(1000) ] S.sort() ...
View Full Document
This note was uploaded on 10/05/2010 for the course PHYS phy503 taught by Professor Gladden during the Spring '09 term at Ole Miss.
- Spring '09