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.