Programming Assignment for Numerical Analysis | MATH 128A, Assignments of Mathematical Methods for Numerical Analysis and Optimization

Material Type: Assignment; Class: Numerical Analysis; Subject: Mathematics; University: University of California - Berkeley; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 10/01/2009

koofers-user-hq6
koofers-user-hq6 🇺🇸

5

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UCB Math 128A, Spring 2009: Programming Assignment 4
Due May 6
Consider the initial-value problem
y0=f(t, y), a tb, y(a) = α. (1)
The Backward Euler method is given by
w0=α(2)
wi+1 =wi+hf(ti+1, wi+1) (3)
Since fdepends on the unknown wi+1, (3) is an equation that must be solved at each time step, and
in general fmight be a non-linear function. In this assignment, we will implement the backward
Euler method in MATLAB using Newton’s method for solving (3). The solver will then be used to
solve a stiff differential equation.
1. Write down Newton’s method for solving (3) for wi+1, using the initial guess w(0)
i+1 =wi. The
iterations will depend on the partial derivative ∂f/∂y =fy(t, y ).
2. Implement a MATLAB function backeuler.m of the form
function [t,w] = backeuler(f,dfdy,a,b,alpha,N,maxiter,tol)
which implements the backward Euler method using the Newton’s method from 1. Here, fis
the function f(t, y), dfdy is the function fy(t, y), Nis the number of time steps, maxiter is the
maximum number of iterations in the Newton solver (the code should break if this is exceeded
without convergence), and tol is the tolerance between two successive Newton iterations. At
each time step, print the value of the Newton updates to monitor the convergence (similar to
the newton table.m function on the web page).
Use the following simple test to check your solver:
f=@(t,y) -y*sin(y);
df=@(t,y) -sin(y)-y*cos(y);
a=0; b=3; alpha=1; N=20; maxiter=20; tol=1e-12;
[t1,w1]=backeuler(f,df,a,b,alpha,N,maxiter,tol);
[t2,w2]=rk4(f,a,b,alpha,N);
plot(t1,w1,t2,w2)
legend(’Backward Euler’,’Runge-Kutta 4’)
The two curves should be close to each other, and if the number of steps Nis increased they
should get even closer (the RK4 method is much more accurate and can be considered the
true solution). Do not include this test in your report, it is only meant as a verification of
your backward Newton method.
Turn page
pf2

Partial preview of the text

Download Programming Assignment for Numerical Analysis | MATH 128A and more Assignments Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

UCB Math 128A, Spring 2009: Programming Assignment 4

Due May 6

Consider the initial-value problem

y′^ = f (t, y), a ≤ t ≤ b, y(a) = α. (1)

The Backward Euler method is given by

w 0 = α (2) wi+1 = wi + hf (ti+1, wi+1) (3)

Since f depends on the unknown wi+1, (3) is an equation that must be solved at each time step, and in general f might be a non-linear function. In this assignment, we will implement the backward Euler method in MATLAB using Newton’s method for solving (3). The solver will then be used to solve a stiff differential equation.

  1. Write down Newton’s method for solving (3) for wi+1, using the initial guess w i(0)+1 = wi. The iterations will depend on the partial derivative ∂f /∂y = fy(t, y).
  2. Implement a MATLAB function backeuler.m of the form

function [t,w] = backeuler(f,dfdy,a,b,alpha,N,maxiter,tol)

which implements the backward Euler method using the Newton’s method from 1. Here, f is the function f (t, y), dfdy is the function fy(t, y), N is the number of time steps, maxiter is the maximum number of iterations in the Newton solver (the code should break if this is exceeded without convergence), and tol is the tolerance between two successive Newton iterations. At each time step, print the value of the Newton updates to monitor the convergence (similar to the newton table.m function on the web page). Use the following simple test to check your solver:

f=@(t,y) -ysin(y); df=@(t,y) -sin(y)-ycos(y); a=0; b=3; alpha=1; N=20; maxiter=20; tol=1e-12; [t1,w1]=backeuler(f,df,a,b,alpha,N,maxiter,tol); [t2,w2]=rk4(f,a,b,alpha,N); plot(t1,w1,t2,w2) legend(’Backward Euler’,’Runge-Kutta 4’)

The two curves should be close to each other, and if the number of steps N is increased they should get even closer (the RK4 method is much more accurate and can be considered the true solution). Do not include this test in your report, it is only meant as a verification of your backward Newton method.

Turn page −→

  1. Now consider a simple combustion model equation:

y′^ = y^2 (1 − y), 0 ≤ t ≤ 2000 , y(0) = 0. 9. (4)

a) Predict the number of steps N that are required to solve (4) using RK4. Since y(t) ≈ 1, the stability can be estimated from the test equation y′^ = λy where λ = ∂f /∂y = 2 y − 3 y^2 ≈ −1. Note that the stability region of RK4 crosses the real axis at hλ = − 2

b) Verify your estimate of N in part a numerically, by solving with N about 10% below and 10% above your prediction. Plot the solutions and check if they give the expected value y(2000) ≈ 1. c) Show that the backward Euler method is A-stable. What does this tell about the number of steps N required for stability? d) Solve (4) using your backeuler solver with N = 1, N = 5, and N = 10. Use the same values of maxiter and tol as in the example above. Plot the solutions. Does the method appear to be stable?

Reporting Your report should contain the answers to the questions, the MATLAB codes, the MATLAB commands, the plots in 3b,d, and the convergence of the Newton solver in 3d.