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.