COMPUTATION OF FIRST ORDER DERIVATIVE USING TAYLOR SERIES APPROXIMATION Write a program that compares the first, second and fourth order approximations of the first derivative against the analytical or exact derivative. 1. The function that you will compute if f(x) = sin(x)/x^3 2. You will compute the first derivative at x = pi/3 2a. You will compute the derivative both numerically and analytically. Make sure you compute 1st, 2nd and 4th order derivatives. 3. Define an error parameter as the absolute difference in numerical and exact derivative at x = pi/3 4. Plot a bar graph that shows the error 5. You need to have appropriate labels and legends on your plot 6. Make sure you upload your file Formula for the fourth order approximation of the first derivative The following program illustrates the comparison of 1st order, 2nd order and 4th order Taylor series approximations to compute the first order derivative. --> In the process we actually calculate the error generated in the Taylor series approximations by calculating analytical derivative and subtracting it from Taylor series approximation. This way we define the error term for each method. The error in the Taylor series approximation exists because of the Truncation terms. Taylor series is a sum of infinite series. When we calculate the 1st order derivative, we approximate the sum for a few terms only depending on the order of approximation. [order of 'dx' or increment of x] → y=f(x) f(x) = sin(x)/x^3 And we need to approximate the first order derivative at x= pi/3. → Taylor’s series with small increment f(x+dx) = f(x) + (dx)*f'(x) + (dx ^2 / 2!) *f''(x) + (dx ^3)/3! *f'''(x) + ......... infinity → Taylor series formula for 1st order approximation f'(x) = (f(x+dx)-f(x))/dx This formula is known as forward differencing method. Taylor series formula for 2nd order approximation f'(x) = (f(x+dx)-f(x-dx))/(2*dx) this formula is known as central differencing method. Taylor series formula for 4th order approximation f'(x) = (f(x-2*dx)-8*f(x-dx) +8*f(x+dx)-f(x+2*dx))/(12*dx) this formula is central differencing for fourth order approximation of 1st order derivative. → In all the above formulae we have a truncation error due to approximation. The program that we write computes the error in different approximation methods. → generally, for higher order scheme the error should be low as compared to lower order scheme. This is due to the fact that the largest truncation term has a higher order[exponent] of 'dx' term. As dx is a small value of increment, this small value raised to power order is relatively small value and thus error reduces as we increase the order of approximation. The same is illustrated in the following programme. MATLAB CODE PART 1 function out1 = first_order_approximation(x,dx) % eavluating the derivative of the function f(x) % f(x)=sin(x)/x^3; % Analytical derivative analytical_derivative = ((x^3)*cos(x) - sin(x)*3*(x^2))/x^6; % numerical derivative % forward differencing % forward_differencing = (f(x+dx)-f(x))/dx % first order taylor method (f(x+dx)-f(x))/dx forward_differencing = (sin(x+dx)/(x+dx)^3 - sin(x)/x^3)/dx; out1 = abs(forward_differencing - analytical_derivative); end function out2 = second_order_approximation(x,dx) % eavluating the derivative of the function f(x) % f(x)=sin(x)/x^3; % Analytical derivative analytical_derivative = ((x^3)*cos(x) - sin(x)*3*(x^2))/x^6; % numerical derivative % central differencing method % central_differencing = (f(x+dx)-f(x-dx))/(2*dx) % second order taylors method central_differencing = (sin(x+dx)/(x+dx)^3 - sin(x-dx)/(x-dx)^3)/(2*dx); out2 = abs(central_differencing - analytical_derivative); end function out4 = fourth_order_approximation(x,dx) % eavluating the first order derivative of the function f(x) % fourth order approximation % f(x)=sin(x)/x^3 % Analytical derivative analytical_derivative = ((x^3)*cos(x) - sin(x)*3*(x^2))/x^6; % % % % numerical derivative central differencing fourth order approximation central_differencing = (f(x-(2*dx))-8*f(x-dx)+8*f(x+dx)-f(x+(2*dx)))/(12*dx) % fourth order approximation for first order derivative using central differencing method central_differencing = ((sin(x-(2*dx))/(x-(2*dx))^3) - (8*(sin(x-dx)/(x-dx)^3)) + (8*(sin(x+dx)/(x+dx)^3)) - ((sin(x+(2*dx))/(x+(2*dx))^3)))/(12*dx); out4 = abs(central_differencing - analytical_derivative); end MATLAB CODE PART 2 clear all close all clc % f(x) = sin(x)/(x^3) x = pi/3; dx = pi/400; % calculating error of 4th order approximation for the 1st order derivative relative to % the analytical derivative. fourth_order_error = fourth_order_approximation(x,dx) % calculating error of 2nd order approximation for the 1st order derivative relative to % the analytical derivative. second_order_error = second_order_approximation(x,dx) % calculating error of 1th order approximation for the 1st order derivative relative to % the analytical derivative. first_order_error = first_order_approximation(x,dx) b = [fourth_order_error 0 0;0 second_order_error 0;0 0 first_order_error]; figure(1) c=bar(b) set(gca,'xticklabel',{'4th order approx','2nd order approx','1st order approx'}) ylabel('error of approximation') legend('4th order error','2nd order error','1st order error','location','northwest') Results 1. 2. Discretization for range of dx For this program you need to do the following. 1. For a range of dx, plot the first order, second and fourth order error. 2. Make sure that you plot dx vs error in a way that makes sense. 3. Explain the results in the space provided. What can you say about the slope of the curves in this plot? ERROR ANALYSIS OF 1ST ORDER DERIVATIVE FOR A RANGE OF INCERMENT [dx] We compute the first order derivative [f'(x)] using the 1st order, 2nd order and 4th order Taylor series approximation method. In this process we calculate the error that is generated in all the approximation methods. We calculate the error for a range of values assigned to the increment [dx]. → We first write down our code for the function of our 1st order 2nd order and 4th order approximation. → 1st order forward differencing approximation Taylor series f'(x) = [f(x+dx) - f(x)]/dx → 2nd order central differencing approximation Taylor series f'(x) = [f(x+dx)-f(x-dx)]/2*(dx) → 4th order central differencing approximation Taylor series f'(x) = [f(x-dx) - 8*f (x - 2*dx) + 8*f (x + 2*dx) - f(x+dx)]/12*(dx) The above formula is derived from the Taylor series. Depending upon the order of approximation the above equations are derived. The input values for the function are the 'x' and 'dx'. The function gives an output of the error in the Taylor series for the respective order of approximation. → function: f(x) = sin(x)/x^3 we have to calculate at [x=pi/3] Analytical derivative --- f'(x) = ((x^3) *cos(x) - sin(x)*3*(x^2))/x^6 → Now we select a range of values for increment 'dx'. Starting value of dx is pi/4 and ending value is pi/4000. We take 50 values of x. this is accomplished by a command known as linspace command. → We use for loop to evaluate all the values of error [generated in Taylor series] in an array. Then we plot this (respective) array of error with respect to the value of 'dx'. We also use log vs log plot to make the comparison between different approximation methods clearer. → comments about the plot 1. The results of the plot show that the value of error of Taylor series approximation reduces as the value of dx decreases. If the value of dx increases the error in the approximation also increases. For a reasonably small value of 'dx' the error for 4th order is the smallest and 2nd order is smaller than 1st order. 2. comparison of error order error -----> 4th order error < 2nd order error < 1st This is due to the fact that error for 4th order is of the order [dx]^4 (the order of largest term of the truncation error terms). As dx tends to smaller value then the error reduces by 1/4th power exponentially. Therefore, as the value of dx tends to smaller values, 4th order error is the lowest. In 2nd order error reduces with the exponent power of 1/2nd. 3. slope of the curve {θ}--> slope of 4th order (θ4) > slope of 2nd order (θ2) > slope of 1st order (θ1) slope of the curve represents the rate of change in error with respect to change in the value of 'dx' [increment]. For 4th order, slope is the largest because the error reduces (changes) more significantly than it does for 1st and 2nd order slopes as explained above. Therefore 4th order approximation slope is the largest and this trend is followed in 2nd order for 1st order slope. [θ4 > θ2 > θ1] MATLAB CODE PART 1 function out1 = first_order_approximation(x,dx) % eavluating the derivative of the function f(x) % f(x)=sin(x)/x^3; % Analytical derivative analytical_derivative = ((x^3)*cos(x) - sin(x)*3*(x^2))/x^6; % numerical derivative % forward differencing % forward_differencing = (f(x+dx)-f(x))/dx % first order taylor method (f(x+dx)-f(x))/dx forward_differencing = (sin(x+dx)/(x+dx)^3 - sin(x)/x^3)/dx; out1 = abs(forward_differencing - analytical_derivative); end function out2 = second_order_approximation(x,dx) % eavluating the derivative of the function f(x) % f(x)=sin(x)/x^3; % Analytical derivative analytical_derivative = ((x^3)*cos(x) - sin(x)*3*(x^2))/x^6; % numerical derivative % central differencing method % central_differencing_formulae_taylor_series = (f(x+dx)-f(x-dx))/(2*dx) % second order taylor method central_differencing = (sin(x+dx)/(x+dx)^3 - sin(x-dx)/(x-dx)^3)/(2*dx); out2 = abs(central_differencing - analytical_derivative); end function out4 = fourth_order_approximation(x,dx) % eavluating the first order derivative of the function f(x) % fourth order approximation % f(x)=sin(x)/x^3 % Analytical derivative analytical_derivative = ((x^3)*cos(x) - sin(x)*3*(x^2))/x^6; % % % % numerical derivative central differencing fourth order approximation central_differencing = (f(x-(2*dx))-8*f(x-dx)+8*f(x+dx)-f(x+(2*dx)))/(12*dx) % fourth order approximation for first order derivative using central differencing method central_differencing = ((sin(x-(2*dx))/(x-(2*dx))^3) - (8*(sin(x-dx)/(x-dx)^3)) + (8*(sin(x+dx)/(x+dx)^3)) - ((sin(x+(2*dx))/(x+(2*dx))^3)))/(12*dx); out4 = abs(central_differencing - analytical_derivative); end MATLAB CODE PART 2 clear all close all clc x = pi/3; dx = linspace(pi/4,pi/4000,50); for i=1:length(dx) for j=1:length(dx) for k=1:length(dx) fourth_order_error(k) = fourth_order_approximation(x,dx(k)); second_order_error(j) = second_order_approximation(x,dx(j)); first_order_error(i) = first_order_approximation(x,dx(i)); end end end figure(1) loglog(dx,first_order_error,'r') hold on loglog(dx,second_order_error,'b') hold on loglog(dx,fourth_order_error,'k') legend('forward differencing first order','central differencing second order','central differencing fourth order','location','northwest') xlabel('length of increment dx') ylabel('error = analytical term - approximate term') Results Deriving 4th order approximation of a 2nd order derivative using Taylor Table method Derive the following 4th order approximations of the second-order derivative. 1. Central difference 2. Skewed right-sided difference 3. Skewed left-sided difference Also, prove that your skewed schemes are fourth-order accurate. Once you have done this, write a program in MATLAB to evaluate the second-order derivative of the analytical function exp(x)*cos(x) and compare it with the 3 numerical approximations that you have derived. a. Provide a plot that compares the absolute error between the above-mentioned schemes b. In your own words, describe why a skewed scheme is useful? What can a skewed scheme do that a CD scheme cannot do? DERIVATION OF THE 2ND ORDER DERIVATIVE BY 4TH ORDER APPROXIMATION We find the 2nd order derivative [f''(x)] for 4th order approximation of the truncation terms by using Taylor’s table method. We find the derivative for right sided skewed scheme, left sided skewed scheme and central difference methods. [1] RIGHT SIDED SKEWED FORWARD DIFFERENCE → dx^2*[f''(x)] = a*f(i) + b*f(i+1) + c*f(i+2) + d*f(i+3) + e*f(i+4) + g*f(i+5) Now we are going to find the values of the constants a, b, c, d, e, g by using Taylor table method. f(i) dx*f'(i) dx^2*f''(i) dx^3*f'''(i) dx^4*f''''(i) dx^5*f’''''(i) a*f(i) b*f(i+1) c*f(i+2) d*f(i+3) e*f(i+4) g*f(i+5) a 0 0 0 0 0 b b b/2 b/6 b/24 b/120 c 2c 4c/2 8c/6 16c/24 32c/120 d 3d 9d/2 27d/6 81d/24 243d/120 e 4e 16e/2 64e/6 256e/24 1024e/120 g 5g 25g/2 125g/6 625g/24 3125g/120 sum of the columns 0 0 1 0 0 0 TAYLOR SERIES TERMS NUMERICAL STENCIL Now we are going to solve the system of linear equations to determine the values of constants using MATLAB. clear all close all clc A=[ 1 0 0 0 0 0 1 1 1/2 1/6 1/24 1/120 1 2 4/2 8/6 16/24 32/120 1 3 9/2 27/6 81/24 243/120 1 4 16/2 64/6 256/24 1024/120 1 5 25/2 125/6 625/24 3125/120 ]; B=[0 0 1 0 0 0]'; C=inv(A)*B The {C} column matrix is the solution of the system of the linear equation. Following are the values of coefficients of the skewed right sided forward differencing formula. f''(x) = [a*f(i) + b*f(i+1) + c*f(i+2) + d*f(i+3) + e*f(i+4) + g*f(i+5)]/dx^2 The following formula is derived using Taylor table method for 4th order approx. and for 2nd order derivative. f''(x) = [3.7500*f(i) - 12.8333*f(i+1) + 17.8333*f(i+2) - 13.0000*f(i+3) + 5.0833*f(i+4) - 0.8333*f(i+5)]/dx^2 [2] LEFT SIDED SKEWED BACKWARD DIFFERENCE → dx^2*[f''(x)] = a*f(i) + b*f(i-1) + c*f(i-2) + d*f(i-3) + e*f(i-4) + g*f(i-5) Now we are going to find the values of the constants a, b, c, d, e, g by using Taylor table method. f(i) dx*f'(i) dx^2*f''(i) dx^3*f'''(i) dx^4*f''''(i) dx^5*f’''''(i) a 0 0 0 0 0 b b b/2 -b/6 b/24 -b/120 c 2c 4c/2 -8c/6 16c/24 -32c/120 d 3d 9d/2 -27d/6 81d/24 -243d/120 e 4e 16e/2 -64e/6 256e/24 -1024e/120 TAYLOR SERIES TERMS NUMERICAL STENCIL a*f(i) b*f(i-1) c*f(i-2) d*f(i-3) e*f(i-4) g*f(i-5) g 5g 25g/2 -125g/6 625g/24 -3125g/120 sum of the columns 0 0 1 0 0 0 Now we are going to solve the system of linear equations to determine the values of constants using MATLAB. clear all close all clc P=[ 1 0 0 0 0 0 1 -1 1/2 -1/6 1/24 -1/120 1 1 -2 -3 4/2 9/2 -8/6 -27/6 16/24 81/24 -32/120 -243/120 1 -4 16/2 -64/6 256/24 -1024/120 1 -5 25/2 -125/6 625/24 -3125/120 ]; R=[0 0 1 0 0 0]'; J = inv(P)*R The {J} column matrix is the solution of the system of the linear equation. Following are the values of coefficients of the skewed left sided backward differencing formula. f''(x) = [a*f(i) + b*f(i-1) + c*f(i-2) + d*f(i-3) + e*f(i-4) + g*f(i-5)]/dx^2 The following formula is derived using Taylor table method for 4th order approx. and for 2nd order derivative. f''(x) = [3.7500*f(i) - 12.8333*f(i-1) + 17.8333*f(i-2) - 13.0000*f(i-3) + 5.0833*f(i-4) - 0.8333*f(i-5)]/dx^2 [3.] CENTRAL DIFFERENCE TECHNIQUE → dx^2*[f''(x)] = a*f(i) + b*f(i+1) + c*f(i+2) + d*f(i-1) + e*f(i-2) Now we are going to find the values of the constants a, b, c, d, e by using Taylor table method. f(i) dx*f'(i) dx^2*f''(i) dx^3*f'''(i) dx^4*f''''(i) dx^5*f’''''(i) a*f(i) b*f(i+1) c*f(i+2) d*f(i-1) e*f(i-2) a 0 0 0 0 0 b b b/2 b/6 b/24 b/120 c 2c 4c/2 8c/6 16c/24 32c/120 d -d d/2 -d/6 d/24 -d/120 e -2e 4e/2 -8e/6 16e/24 -32e/120 sum of the columns 0 0 1 0 0 0 TAYLOR SERIES TERMS NUMERICAL STENCIL Now we are going to solve the system of linear equations to determine the values of constants using MATLAB. clear all close all clc P=[ 1 0 0 0 0 1 1 1/2 1/6 1/24 1 2 4/2 8/6 16/24 1 -1 1/2 -1/6 1/24 1 -2 4/2 -8/6 16/24 ]; R=[0 0 1 0 0]'; S = inv(P)*R The {S} column matrix is the solution of the system of the linear equation. Following are the values of coefficients of the central differencing formula [for 5 nodes] using Taylor table. f''(x) = [a*f(i) + b*f(i+1) + c*f(i+2) + d*f(i-1) + e*f(i-2)]/dx^2 The following formula is derived using Taylor table method for 4th order approx. and for 2nd order derivative. f''(x) = [-2.5000*f(i) + 1.3333*f(i+1) - 0.0833*f(i+2) + 1.3333*f(i-1) 0.0833*f(i-2)]/dx^2 → Analytical function f(x) = exp(x)*cos(x) We have to compute the 2nd order derivative of the analytical function for all the schemes derived above and we have to compute the absolute form of error for all the schemes. MATLAB CODE function out1 = fourth_order_approximation_skewed_forward_difference(x,dx) % eavluating 2nd order derivative of the function f(x) % f(x)=exp(x)*cos(x); % Analytical derivative analytical_derivative = -2*exp(x)*sin(x); % % % - numerical derivative forward differencing forward_differencing f''(x) = f''(x) = [3.7500*f(i) - 12.8333*f(i+1) + 17.8333*f(i+2) 13.0000*f(i+3) + 5.0833*f(i+4) - 0.8333*f(i+5)]/dx^2 % first order taylor method skewed_forward_differencing = (3.7500*exp(x)*cos(x) - (77/6)*exp(x+dx)*cos(x+dx) + (107/6)*exp((x+(2*dx)))*cos((x+(2*dx))) - 13.0000*exp((x+(3*dx)))*cos((x+(3*dx))) + (61/12)*exp((x+(4*dx)))*cos((x+(4*dx))) - (5/6)*exp((x+(5*dx)))*cos((x+(5*dx))))/(dx^2); out1 = abs(skewed_forward_differencing - analytical_derivative); end function out2 = fourth_order_approximation_skewed_backward_difference(x,dx) % eavluating 2nd order derivative of the function f(x) % f(x)=exp(x)*cos(x); % Analytical derivative analytical_derivative = -2*exp(x)*sin(x); % numerical 2nd order derivative of f(x) by 4th order approximation % skewed left-sided differencing % backward_differencing f"(x) = [3.7500*f(i) - 12.8333*f(i-1) + 17.8333*f(i-2) 13.0000*f(i-3) + 5.0833*f(i-4) - 0.8333*f(i-5)]/dx^2 % first order taylor method skewed_backward_differencing = (3.7500*exp(x)*cos(x) - (77/6)*exp((x-dx))*cos((x-dx)) + (107/6)*exp((x-(2*dx)))*cos((x-(2*dx))) - 13.0000*exp((x-(3*dx)))*cos((x-(3*dx))) + (61/12)*exp((x-(4*dx)))*cos((x-(4*dx))) - (5/6)*exp((x-(5*dx)))*cos((x-(5*dx))))/(dx^2); out2 = abs(skewed_backward_differencing - analytical_derivative); end function out3 = fourth_order_approximation_central_difference(x,dx) % eavluating the 2nd order derivative of the function f(x) % f(x)=exp(x)*cos(x); % Analytical derivative analytical_derivative = -2*exp(x)*sin(x) ; % numerical derivative for 2nd order derivative % central differencing for 4th order approximation from taylor table method % central differencing f''(x) = [-2.5000*f(i) + 1.3333*f(i+1) - 0.0833*f(i+2) + 1.3333*f(i-1) - 0.0833*f(i-2)]/dx^2 % 4th order approximation using taylor table method central_differencing = (-2.5000*exp(x)*cos(x) + (4/3)*exp((x+dx))*cos((x+dx)) (1/12)*exp((x+(2*dx)))*cos((x+(2*dx))) + (4/3)*exp((x-dx))*cos((x-dx)) - (1/12)*exp((x(2*dx)))*cos((x-(2*dx))))/(dx^2) ; out3 = abs(central_differencing - analytical_derivative); end clear all close all clc x = pi/3; dx = linspace(pi/4,pi/400,500); for i=1:length(dx) for j=1:length(dx) for k=1:length(dx) skewed_forward_differencing_error(k) = fourth_order_approximation_skewed_forward_difference(x,dx(k)); skewed_backward_differencing_error(j) = fourth_order_approximation_skewed_backward_difference(x,dx(j)); skewed_central_differencing_error(i) = fourth_order_approximation_central_difference(x,dx(i)); end end end figure(1) loglog(dx,skewed_forward_differencing_error,'r') hold on loglog(dx,skewed_backward_differencing_error,'b') hold on loglog(dx,skewed_central_differencing_error,'k') legend('skewed right sided difference for 4th order ','skewed left sided difference for 4th order ','central differencing of 4th order ','location','northwest') xlabel('length of increment dx') ylabel('error = analytical term - approximate term') Advantages of skewed scheme over central difference scheme → The skewed scheme takes information from either right or left side of the numerical stencil (domain) and in central difference scheme information is utilized from both left side and right side of the point of consideration. This makes skewed scheme more useful in problems where information is flowing in one direction, for example heat transfer in fluid flow problem. If information is flowing in right side direction, we require a one-sided skewed scheme which will give more accurate results.