Programming Assignment 4 Solutions - UCB Math 128A Spring...

This preview shows page 1 - 3 out of 4 pages.

UCB Math 128A, Spring 2014: Programming Assignment 4Solutions1.To apply Newton’s method on the backward Euler methodwi+1=wi+hf(ti+1, wi+1),defineF(wi+1) =wi+1-wi-hf(ti+1, wi+1)and solveF(wi+1) = 0 usingw(0)i+1=wiw(k)i+1=w(k-1)i+1-F(w(k-1)i+1)F0(w(k-1)i+1)=w(k-1)i+1-w(k-1)i+1-wi-hf(ti+1, w(k-1)i+1)1-hfy(ti+1, w(k-1)i+1)2.The following MATLAB function implements the backward Euler method:function [t,w]=backeuler(f,dfdy,a,b,alpha,N,maxiter,tol)h=(b-a)/N;t=(a:h:b);w=0*t;w(1)=alpha;for i=1:Nw0=w(i);wk=w0;for k=1:maxiterdw=(wk-w0-h*f(t(i+1),wk))/(1-h*dfdy(t(i+1),wk));wk=wk-dw;fprintf(’%d %g\n’,k,abs(dw));if abs(dw)<=tol, break; endendfprintf(’\n’);if abs(dw)>tol, error(’No Newton convergence.’); endw(i+1)=wk;end3.a)The step sizehmust approximately satisfy||=h2.7853, which gives the numberof time stepsN= 2000/h718.b)The following lines:[email protected](t,y) y^2*(1-y);[email protected](t,y) 2*y-3*y^2;a=0; b=2000; alpha=0.9;[t1,w1]=rk4(f,a,b,alpha,646);[t2,w2]=rk4(f,a,b,alpha,790);plot(t1,w1,t2,w2)
solves the problem withN= 6460.9·718 andN= 790

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture