lec14_eulerODE

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

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

View Full Document Right Arrow Icon
#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
Background image of page 1

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

View Full DocumentRight Arrow Icon
#yi=0. #xi=0.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online