This problem explores the trade-off between the truncation error in the finite d
ID: 3759079 • Letter: T
Question
This problem explores the trade-off between the truncation error in the finite difference approximation of the derivative and floating point truncation error incurred when using finite precision arithmetic. (a) Write a MATLAB script that computes the forward finite difference approximation of the derivative of sin(x) for x = pi/3. Use step sizes ranging from 10-1 to 10-w. You may find the logspace command useful. Plot the error, using a log-log scale, between your approximation and the exact derivative for each of your step sizes. (b) Based on your plot, what is the best step size to use for the finite difference step size? (c) Explain the behavior you see in the graph.Explanation / Answer
(a)
L = 2*pi; % Interval Length
N = 100; % No. of coordinate points
x = linspace(0,L,N)’; % Coordinate vector
dx = x(2) - x(1); % Coordinate step
% Two-point finite-difference representation of Derivative
D=(diag(ones((N-1),1),1)-diag(ones((N-1),1),-1))/(2*dx);
% Next modify D so that it is consistent with f(0) = f(L) = 0
D(1,1) = 0; D(1,2) = 0; D(2,1) = 0; % So that f(0) = 0
D(N,N-1) = 0; D(N-1,N) = 0; D(N,N) = 0; % So that f(L) = 0
% Three-point finite-difference representation of Laplacian
Lap = (-2*diag(ones(N,1),0) + diag(ones((N-1),1),1) ...
+ diag(ones((N-1),1),-1))/(dx^2);
% Next modify Lap so that it is consistent with f(0) = f(L) = 0
Lap(1,1) = 0; Lap(1,2) = 0; Lap(2,1) = 0; % So that f(0) = 0
Lap(N,N-1) = 0; Lap(N-1,N) = 0; Lap(N,N) = 0;% So that f(L) = 0
% To verify that D*f corresponds to taking the derivative of f
% and Lap*f corresponds to taking a second derviative of f,
% define f = sin(x) or choose your own f
f = sin(x);
% And try the following:
Df = D*f; Lapf = Lap*f;
plot(x,f,’b’,x,Df,’r’, x,Lapf,’g’);
axis([0 5 -1.1 1.1]); % Optimized axis parameters
% To display the matrix D on screen, simply type D and return ...
D % Displays the matrix D in the workspace
Lap % Displays the matrix Lap
(b)
Finite-difference methods for computing the derivative of a function with respect to an independent variable require knowledge of the perturbation step size for that variable. Although rules of thumb exist for determining the magnitude of the step size, their effectiveness diminishes for complicated functions or when numerically solving difficult optimization problems. This dissertation investigates the problem of determining the step size that minimizes the total error associated with finite-difference derivative approximations.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.