You arc given a function f(x) = x^5 + 8x^4 on the interval [-1, 3] and you want
ID: 3108706 • Letter: Y
Question
You arc given a function f(x) = x^5 + 8x^4 on the interval [-1, 3] and you want to calculate the first derivative of this function at the points on this interval. Write a MATLAB program that: Given the number N of subintervals, calculates the first derivative of the function, f'_i, at each point x_i, including the boundary points, with the first-order accuracy (this can be done as a self-written MATLAB function). You can use any formula of O(h) of your choice. Given the number N of subintervals, calculates the first derivative of the function, f'_i, at each point x_i, including the boundary points, with the second-order accuracy (this can be done as a self-written MATLAB function). You can use any formula of O(h^2) of your choice. Calculates two measures of error defined as E_L_infinity = max_i|f'_i - f'_true (x_i)| E_L_2 = Squareroot sigma_i = 0^N (f'_i - f'_true (x_i))^2/N + 1 The first error is called the error in infinity norm (max_i stands for the maximum value among all the points), and the second error is called the error in L_2 norm. Here f'_true (x_i) is the true value of derivative at the given point x_i. Run your program with the number of intervals N equal to 8, 16, 32, 64 and document the E_L_infinity and E_L_2 errors for each run. Plot E_L_infinity versus N, in log-log plot, for both the O(h) and O(h^2) derivatives on the same plot. Also, plot E_L_2 versus N, in log-log plot, for both the O(h) and O(h^2) derivatives (on the same plot, but different from E_L_infinity errors). Estimate the order of accuracy for each derivative from the two plots. Discuss if you obtain the expected order of convergence, for both error measures.Explanation / Answer
N = input('Enter number of subintervals: ');
n = linspace(-1, 3, N+1);
disp('Intervals: ');
disp(n);
h = 1e-6;
%% Part (a)
derivatives_1 = zeros(1, length(n));
for i = 1:1:length(n)
x = n(i);
derivatives_1(i) = (func(x+h)-func(x))/h; % First-Order
end
disp('Derivatives (First-Order Accuracy):');
disp(derivatives_1);
%% Part (b)
derivatives_2 = zeros(1, length(n));
for i = 1:1:length(n)
x = n(i);
derivatives_2(i) = (func(x+h)-func(x-h))/(2*h); % Second-Order
end
disp('Derivatives (Second-Order Accuracy):');
disp(derivatives_2);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.