18-330 – Exercise 1: Finite differences via interpolation (Solution)

$ 29.99
Category:

Description

Consider the simplest forward finite-difference approximation for 𝑓′(π‘₯):
𝑓(π‘₯ + β„Ž) βˆ’ 𝑓(π‘₯)
𝑔(β„Ž) ∢=
β„Ž
When we calculate this numerically, there are two sources of error: truncation error, coming from approximating the exact Taylor expansion with a finite piece of it, and floating-point roundoff error.
1. Suppose that we perturb the input, β„Ž, by Ξ”β„Ž. Calculate (analytically) an approximation to the (absolute) error Δ𝑔 on the output to first order in Ξ”β„Ž; you should find that it grows like β„Žβˆ’1.
2. Suppose that the input perturbation size is πœ–mach; the error from [1] is then the roundoff error. Find an estimate for the value of β„Ž at which the truncation error balances with the roundoff error, and find the size of the error there. Compare this with the plot that we did in class. 3. Consider an interval [π‘Ž,𝑏] and let π‘š be the midpoint of the interval. Use Lagrange interpolation to find an analytical expression for the unique quadratic function that passes through (π‘Ž,𝑓(π‘Ž)), (π‘š,𝑓(π‘š)) and
(𝑏,𝑓(𝑏)).
4. Use your result from [3] to derive the centered difference approximation for the derivative 𝑓′(π‘‘π‘˜) in terms of equally-spaced points π‘‘π‘˜ separated by a distance β„Ž.
5. What approximation does it give for the second derivative 𝑓″(π‘‘π‘˜)?
6. Use [3] to find a backward difference expression for 𝑓′(π‘‘π‘˜) using information at nodes π‘‘π‘˜βˆ’2 and π‘‘π‘˜βˆ’1.
7. Find numerically the rate of convergence of the results from [3] and [4] for equally-spaced points separated by a distance β„Ž for the function sin(2π‘₯) at π‘₯ = πœ‹/4, for values of β„Ž between 10βˆ’6 and 10βˆ’1.
Exercise 2: Integration using Simpson’s rule
In this problem we will derive the second-order Newton–Cotes quadrature rule,
𝑏
known as Simpson’s rule, for calculating ∫ 𝑓(π‘₯)𝑑π‘₯.
π‘Ž
Suppose you are given an 𝑁-point quadrature rule with nodes (π‘‘π‘˜)π‘˜=0𝑁 and weights (π‘€π‘˜)π‘π‘˜=0 for integrating over the interval $[-1, 1]. That is, the π‘‘π‘˜ are 𝑁 + 1 points with βˆ’1 ≀ π‘‘π‘˜ ≀ 1, and the π‘€π‘˜ are given to you such that
π‘€π‘˜ 𝑓(π‘‘π‘˜)
1. Construct a new quadrature rule for integrating over a general interval
[𝑒,𝑣]. I.e., find π‘‘β€²π‘˜ and π‘€π‘˜β€² such that
𝑣 𝑁
∫ 𝑓(π‘₯)𝑑π‘₯ ≃ βˆ‘π‘€π‘˜β€²π‘“(π‘‘β€²π‘˜)
𝑒 π‘˜=0
Derive the basic second-order Newton–Cotes quadrature rule for 𝑓(π‘₯)𝑑π‘₯, as follows:
2. Use your results from [Exercise 1] to find the degree-2 polynomial 𝑝2 that agrees with 𝑓 at the three points π‘₯ = βˆ’1,0,1. (Leave your result in terms of the values 𝑓(βˆ’1), 𝑓(0) and 𝑓(1).)
3. Integrate 𝑝2 interval [βˆ’1,1] to approximate βˆ«π‘“ in terms of 𝑓(βˆ’1, 𝑓(0) and 𝑓(1). Express this result as a quadrature rule.
4. Combine your answers to [2] and [3] to write down the basic (not composite) Simpson’s rule for integrating f over [𝑒,𝑣].
5. Given an interval [π‘Ž,𝑏], subdivide it into 𝑁 equal-width subintervals, apply the basic Simpson’s rule to integrate 𝑓 over each subinterval, and sum the results to obtain the composite Simpson rule for integrating 𝑓 over [π‘Ž,𝑏]. How many samples of f does this rule require? (Be careful not to overcount).
Exercise 3: Using Newton–Cotes methods
1. Implement the composite 0th (rectangle), 1st (trapezoid), and 2nd-order (Simpson) Newton–Cotes quadrature rules for integrating an arbitrary function over an arbitrary interval with 𝑁 + 1 points. Each should be a single function like rectangle(f, a, b, N).
Note that in the case of Simpson’s rule, we are using a total of 𝑁 + 1 points; how many intervals does this correspond to?
2. Calculate (2π‘₯)𝑑π‘₯ using each method. Plot the relative error
𝐼approx(𝑁) βˆ’ 𝐼exact
𝐸(𝑁) ∢=
𝐼exact
as a function of 𝑁 for 𝑁 in the range [10,106] (or use a higher or lower upper bound depending on the computing power you have available).
Do these errors correspond with the expectations from the arguments in lectures?
3. Do the same for )𝑑π‘₯. Use the erf function from the SpecialFunctions.jl package to calculate the β€œexact” result. [Hint: Check carefully the help for that function to make sure of the definition used.]
4. We showed that the trapezium rule has error at most π’ͺ(β„Ž2). Consider the following integral of a smooth, periodic function:
2πœ‹
𝐼 = ∫ exp(cos(πœƒ))π‘‘πœƒ
0
Plot the error in the trapezium rule in this case. How fast does it decay with 𝑁? [This will be important later in the course.]
Note that this integral can be calculated exactly as 2πœ‹πΌ0(1), where 𝐼0 is a modified Bessel function, which can be evaluated at 1 using the SpecialFunctions.jl package as besseli(0, 1).
Exercise 4: Euler method for ODEs
1. Implement the Euler method in a function euler(f, x0, Ξ΄t, t_final), assuming that 𝑑0 = 0. Your code should work equally well if you put vectors in, to solve the equation xΜ‡ = f(x).
2. Use your code to integrate the differential equation π‘₯Μ‡ = 2π‘₯ from 𝑑 = 0 to 𝑑 = 5 with initial condition π‘₯0 = 0.5. Plot the exact solution and the numerical solution for values of 𝛿𝑑 = 0.01,0.05,0.1,0.5. On a different plot show the relative error as a function of time, compared to the analytical solution.
3. Do the same for π‘₯Μ‡ = βˆ’2π‘₯ with initial condition π‘₯0 = 3.
4. For the above two cases, calculate the error at 𝑑 = 5 when the time interval is split into 𝑁 pieces for 𝑁𝑏𝑒𝑑𝑀𝑒𝑒𝑛10π‘Žπ‘›π‘‘1000. Plot the error as a function of 𝑁. What is the rate of convergence as β„Ž β†’ 0?
A pendulum satisfies the ODE πœƒ +̈ sin(πœƒ) = 0, where πœƒ is the angle with the vertical.
5. Show analytically that the quantity (β€œenergy”) 𝐸(πœƒ, Μ‡ Μ‡
is conserved along a trajectory, i.e. that 𝑑𝑑𝑑 [𝐸(πœƒ(𝑑),πœƒ(𝑑)) = 0]Μ‡ , so that 𝐸(πœƒ(𝑑),πœƒ(𝑑)) = 𝐸(πœƒ(𝑑̇ 0),πœƒ(𝑑̇ 0)).
6. Solve this equation using the Euler method for initial conditions (0,1) to show that the energy is not conserved.
7. Draw the phase plane. Explain graphically what is happening in terms of what each step does.
8. Plot 𝐸 as a function of time for different values of 𝛿𝑑. How fast does it grow? Explain this in terms of what happens at each step.

Reviews

There are no reviews yet.

Be the first to review “18-330 – Exercise 1: Finite differences via interpolation (Solution)”

Your email address will not be published. Required fields are marked *