Please do in Matlab script! Problem 1 In this problem we will calculate the firs
ID: 3209913 • Letter: P
Question
Please do in Matlab script!
Problem 1 In this problem we will calculate the first and second derivatives of a function and examine the effect of method and step size. Turn in (parts b-d): a table of the results organized by methods and step size. value of the numerical derivative percent error from the true (analytical) derivative * Comment on the effects of method and step size. (a) Write three different function files to calculate the derivative: using the backward difference, forward difference, and central difference formulae. These functions should be general enougih that you can use them in future problems (Probs. 2-4 below). (b) Calculate and plot the derivative of x3 over the range [2, 3] for all three methods, along with the actual value of the derivative. Try at least two different step sizes. In your table, report the error for each method and each step size at x = 2.5. [watch out: the arrays for the derivatives are different lengths, so make sure you're reporting the derivatives at the right point in the array] (c) Using the derivatives you calculated in part (b), calculate and plot the second derivative using all three methods. (For the second derivative using backward difference, use the first derivative you calculated using backward difference, and so on for the others. Also make a table for the error of the second derivate of the value at x = 2.5 (d) Another way to calculate the second derivative directly is to use the formula + Ar)-2)+-Ar) Ary Calculate and plot the second derivative using the function above. Calculate the error and add this result to your table.Explanation / Answer
Here are the functions for calculating first derivative using forward,backward, and central difference methods
function first_der_fwd = fwd_diff(f,x,delta_x)
first_der_fwd = (f(x+delta_x)-f(x))/delta_x;
end
function first_der_bwd = bwd_diff(f,x,delta_x)
first_der_bwd = (f(x) - f(x-delta_x))/delta_x;
end
function first_der_central = central_diff(f,x,delta_x)
first_der_central = (f(x+delta_x) - f(x-delta_x))/(2*delta_x);
end
Here is the script for part b of the question
% step size = 0.1
delta_x = 0.1;
x = 2:delta_x:3;
f = @(x)(x.^3);
first_der_fwd = fwd_diff(f,x,delta_x);
first_der_bwd = bwd_diff(f,x,delta_x);
first_der_central = central_diff(f,x,delta_x);
actual_der = 3*x.^2;
error_fwd = abs((actual_der - first_der_fwd)./actual_der);
error_bwd = abs((actual_der - first_der_bwd)./actual_der);
error_central = abs((actual_der - first_der_central)./actual_der);
plot(x,actual_der,'r*',x,first_der_fwd,x,first_der_bwd,x,first_der_central)
legend('actual derivative','fwd diff','bwd diff','central diff')
fprintf('step size = %f x = %f error_fwd = %f error_bwd = %f error_central = %f ',...
delta_x,2.5,error_fwd(x == 2.5),error_bwd(x==2.5),error_central(x==2.5));
% step size = 0.25
delta_x1 = 0.25;
x1 = 2:delta_x1:3;
f1 = @(x)(x.^3);
first_der_fwd1 = fwd_diff(f,x1,delta_x1);
first_der_bwd1 = bwd_diff(f,x1,delta_x1);
first_der_central1 = central_diff(f,x1,delta_x1);
actual_der1 = 3*x1.^2;
error_fwd1 = abs((actual_der1 - first_der_fwd1)./actual_der1);
error_bwd1 = abs((actual_der1 - first_der_bwd1)./actual_der1);
error_central1 = abs((actual_der1 - first_der_central1)./actual_der1);
figure;
plot(x1,actual_der1,'r*',x1,first_der_fwd1,x1,first_der_bwd1,x1,first_der_central1)
legend('actual derivative','fwd diff','bwd diff','central diff')
fprintf('step size = %f x = %f error_fwd = %f error_bwd = %f error_central = %f ',...
delta_x1,2.5,error_fwd1(x1 == 2.5),error_bwd1(x1==2.5),error_central1(x1==2.5));