Develop a Matlab code to calculate the numerical differentiation at ALL the foll
ID: 3935000 • Letter: D
Question
Develop a Matlab code to calculate the numerical differentiation at ALL the following data points, and produce the following plot. To ensure high accuracy calculations, the equations given below MUST BE USED. First Derivative Error f'(x_i) = f(x_i + 1) - f(x_i)/h O(h) f'(x_i) = -f(x_i + 2) + 4f(x_i + 1) - 3f(x_i)/2h O(h^2) Backward difference First Derivative Error f'(x_i) = f(x_i) - f(x_i - 1)/h O(h) f'(x_i) = 3f(x_i) - 4f(x_i - 1) + f(x_i - 2)/2h O(h^2) Centered difference First Derivative Error f'(x_i) = f(x_i + 1) - f(x_i - 1)/h O(h^2) f'(x_i) = -f(x_i + 2) + 8f(x_i + 1) - 8f(x_i - 1) + f(x_i - 2)/12h O(h^4)Explanation / Answer
Following is the required matlab code, equations are numbered in order given in the question:
x = [0,1,2,3,4,5,6,7,8];
fx =[0.5, 4, 5.1, 9.8, 10.3, 9.4, 8.7, 6.6, 5.4 ];
%Equation numbered according to the given order in question
%Equation 1
dfx = zeros(8,1);
for i=1:8
dfx(i,1) = fx(i+1) - fx(i);
end
figure;
plot( [0,1,2,3,4,5,6,7,8 ], fx, 'r', [0,1,2,3,4,5,6,7 ], dfx , 'black' );
ylabel( 'Amplitude' );
xlabel( 'x' );
legend('f(x)','df(x)');
title('Equation 1');
%Equation 2
dfx = zeros(7,1);
for i=1:7
dfx(i,1) = (-fx(i+2) + 4*fx(i+1) - 3*fx(i) )/2.0;
end
figure;
plot( [0,1,2,3,4,5,6,7,8 ], fx, 'r', [0,1,2,3,4,5,6 ], dfx , 'black' );
ylabel( 'Amplitude' );
xlabel( 'x' );
legend('f(x)','df(x)');
title('Equation 2');
%Backward Difference
%Equation 3
dfx = zeros(8,1);
for i=2:9
dfx(i-1,1) = fx(i) - fx(i-1);
end
figure;
plot( [0,1,2,3,4,5,6,7,8 ], fx, 'r', [ 1,2,3,4,5,6,7,8 ], dfx , 'black' );
ylabel( 'Amplitude' );
xlabel( 'x' );
legend('f(x)','df(x)');
title('Equation 3');
%Equation 4
dfx = zeros(7,1);
for i=3:9
dfx(i-2,1) = ( fx(i-2) - 4*fx(i-1) + 3*fx(i) )/2.0;
end
figure;
plot( [ 0,1,2,3,4,5,6,7,8 ], fx, 'r', [ 2,3,4,5,6,7,8 ], dfx , 'black' );
ylabel( 'Amplitude' );
xlabel( 'x' );
legend('f(x)','df(x)');
title('Equation 4');
%Centerd Difference
%Equation 5
dfx = zeros(7,1);
for i=2:8
dfx(i-1,1) = (fx(i+1) - fx(i-1))/2;
end
figure;
plot( [0,1,2,3,4,5,6,7,8 ], fx, 'r', [ 1,2,3,4,5,6,7 ], dfx , 'black' );
ylabel( 'Amplitude' );
xlabel( 'x' );
legend('f(x)','df(x)');
title('Equation 5');
%Equation 6
dfx = zeros(5,1);
for i=3:7
dfx(i-2,1) = ( -fx(i+2) + 8*fx(i+1) - 8*fx(i-1) + fx(i-2) )/12.0;
end
figure;
plot( [ 0,1,2,3,4,5,6,7,8 ], fx, 'r', [ 2,3,4,5,6 ], dfx , 'black' );
ylabel( 'Amplitude' );
xlabel( 'x' );
legend('f(x)','df(x)');
title('Equation 6');
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.