lec14_eulerODE

# lec14_eulerODE - #Lecture 14 Code#Exponential Decay f from...

This preview shows pages 1–3. Sign up to view the full content.

#Lecture 14 Code # #Exponential Decay # from pylab import * f def forwardeuler(f,u0,T,n): dt=T/float(n) t=linspace(0,T,n+1) u=zeros(n+1) u[0]=u0 for k in range(n): u[k+1]=u[k]+dt*f(u[k]) return u, t def f(u): return -b*u #exponential decay #return 0.5*u*(1.0-u/800.) #logistic population growth #set up parameters nsteps=500 u0=5.0 Tfinal=5.0 b=2.0 b #compute solution and exact for comparison u,t = forwardeuler(f,u0,Tfinal,nsteps) uexact=u0*exp(-b*t) u #Generate plots plot(t,u,'o',label='Euler') plot(t,uexact,'-',lw=2,label='Exact') xlabel('Time (s)') ylabel('u(t)') title('Euler solution with %i time steps'%nsteps) legend() l ########################## # Projectile motion of a ball with air drag. # Uses euler and euler-cromer methods and plots results with theory (no drag) ########################## # #from pylab import * # #def euler(rc,vc,acc,tau): # v_new = vc + tau* acc # #r_new = rc+tau*vc # r_new = rc+tau*v_new # return r_new,v_new # #def leapfrog(rc,vc,acc,tau): # pass # # #close('all') # ##Initial conditions

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
#yi=0. #xi=0.
This is the end of the preview. Sign up to access the rest of the 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.

### Page1 / 3

lec14_eulerODE - #Lecture 14 Code#Exponential Decay f from...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online