MATLAB for Numerical Analysis
MACM 316 – Computing Assignment 6
Due Date: Monday March 19th, at 11pm.
You must create ONE .pdf file that contains the following: Page 1 will be your report - the TA will only read and mark page 1 - if there is additional writing on any other page, it will be ignored and you will get no credit for it. Pages two (and maybe 3) will be your Matlab code that you used in the assignment. The TA may choose to run this code if your report does not match the code you submit. You will get a Crowdmark link that will allow you to upload your completed assignment.The assignment is due at 11:00pm. I have set the due time in Crowdmark to 11:05pm and if Crowdmark indicates that you submitted late, you will be given 0 on the assignment. Your computing report portion of your submission must be exactly 1 page.
• Please read the Guidelines for Assignments first.
• Keep in mind that Canvas discussions are open forums.
• Acknowledge any collaborations and assistance from colleagues/TAs/instructor.
Computing Assignment – Finite Difference Method
Required submission: 1 page PDF document and Matlab code uploaded to Crowdmark.
Numerical differentiation is the heart of the Finite Difference Method. Here, derivatives of func- tions are approximated by the evaluation of the function at points near the value where we want to compute this derivative, divided by some function of the distance between these points. For example, if we want to approximate the value of y′(x), we can write
y′(x) ≈ y(x+ h)− y(x) h
.
This quotient gives an approximation to y′(x) for small values of h > 0.
As discussed in class, more than one approximation can be easily derived for y′(x):
y′(x) ≈ y(x+ h)− y(x) h
, y′(x) ≈ y(x)− y(x− h) h
, y′(x) ≈ y(x+ h)− y(x− h) 2h
.
It is known that the first two approximations are O(h) while the last one is O(h2). If x0, x1, . . . , xN is an equally-spaced grid of an interval [a, b] with grid size h, we can approximate y′(xk) for k = 1, . . . , N − 1 as
y′(xk) ≈ y(xk+1)− y(xk−1)
2h .
Putting these equations together, we have a 2nd order approximation of y′(xk):
y′ = 1
2h By,
1
where y′ := (y′(x1), . . . , y ′(xN−1))
t, y := (y(x0), y(x1), . . . , y(xN−1), y(xN )) t and the matrix B is
B =
−1 0 1 . . .
...
0 . . .
. . . . . .
. . . ...
... . . .
. . . . . .
. . . 0 ...
. . . −1 0 1
.
Higher derivatives can be also approximated. In fact, the 2nd order finite difference approximation of y′′(x) is
y′′(x) ≈ y(x+ h)− 2y(x) + y(x− h) h2
.
This formula is O(h2). We can approximate y′′ at the points x1, . . . , xN−1 by using the formula above:
y′′(xk) ≈ y(xk+1)− 2y(xk) + y(xk−1)
h2 , k = 1, . . . , N − 1.
Defining the vector y′′ := (y′′(x1), . . . , y ′′(xN−1))
t, we can write
y′′ ≈ 1 h2 Ay,
where the matrix A is written as
A =
1 −2 1 . . .
...
0 . . .
. . . . . .
. . . ...
... . . .
. . . . . .
. . . 0 ...
. . . 1 −2 1
.
Note that A and B have N + 1 columns and N − 1 rows. In this assignment we want to use these formulas to solve a simple problem. Consider the differential equation
y′′(x) + 0.5y′(x) = sin(x), x ∈ (0, 2π), y(0) = 0, y(2π) = 0,
Assume we want to approximate the solutions of this problem by using the Finite Difference Method, that is, using the matrices A and B as defined above.
First, consider an equally-spaced grid x0, . . . , xN of [0, 2π], and using the correct formulas, find an O(h2) formula of the term y′′(x) + 0.5y′(x) at the grid points xk, k = 1, . . . , N − 1. Note that the end points x0 and xN are not included.
Second, implement the matrices A and B which give an O(h2) to the term y′′(x) + 0.5y′(x) at the grid points on [0, 2π]. Then, find the solution y at the grid points. Note that the values of y at the grid points can be stored in the vector (y(x0), y(x1), . . . , y(xN ))
t. Note that the value of y at the end points of [0, 2π] provide a simplification to this approach and the system to be solved has only N − 1 equations and N − 1 unknowns. Your conclusions should be explained in a one-page report. Your report must include the following:
2
(a) 2nd order formula of y′′(x) + 0.5y′(x) at the grid points xk, k = 1, . . . , N − 1.
(b) A plot of the matrices A and B for N = 20. The command spy might be useful. What is the structure of the matrices?
(c) Plot the approximated y for N = 5, 10, 20, and the true solution y(x) = 0.4(1 − cos(x)) − 0.8 sin(x). Note that you only know the solution at the grid points. What happens with the approximated solution when N increases?
(d) Make sure you answer all the questions in the document.
3