Project

Week 3.4 - Discretization for range of dx

Week 3.4 - Discretization for range of dx

Aim : To compute the first, second and fourth order approximations for a range of 'dx', for a given function and is a continuation of the previous program.

Firstly, in order to evaluate the approximations, a particular value of 'x' and a "range of dx" is defined. For instance, to calculate the first order approximation, a function with an output of error (i.e. out) is defined which has to inputs, x and dx. This output is nothing but an absolute difference between "first order numerical derivative (forward differencing) and the analytical derivative of a given function". Likewise, error output for second order approximation is is calculated within a function by difference of "central differencing and the analytical derivative", and similarly for the fourth order approximation by difference of its respective numerical derivative and analytical one.

As all these functions of approximations' have two inputs, namely, 'x' which is fixed and more than one 'dx' (as it is from pi/4000 to pi/40, with 20 values), 'for' command is used which repeatedly computes the errors for corresponding approximations.

The linear output of the errors, starting from 1 to all dx, is represented using 'loglog' command, by 'hold on', so all the results are captured in one figure.

Now, from the log plot's output, it is clear that, the slope of lines gradually increase from first order to fourth order approximation, which implies that, the value in error for the first orfer approximation dips slowly with decrement in dx, it is a slightly faster for the second order approximation, whilst, for the fourth order approximation, the value reduces rapidly as dx falls.

Program

close all
clear all
clc

x = pi/3;
dx = linspace(pi/4000,pi/40,20)

for i = 1:length(dx)

first_order_error(i) = first_order_approximations(x,dx(i));
second_order_error(i) = second_order_approximations(x,dx(i));
fourth_order_error(i) =fourth_order_approximations(x,dx(i));

end 

figure(1)

loglog(dx,first_order_error,'m')
hold on
loglog(dx,second_order_error,'r')
hold on
loglog(dx,fourth_order_error,'g')
grid on
xlabel('Range of dx')
ylabel('Error in approximations')
legend('First order error','Second order error','Fourth order error')

% Function for 1st order approximation
function out = first_order_approximations(x,dx)

analytical_derivative = ((x^3*(cos(x))-sin(x)*3*x^2))/x^6;
forward_differencing = ((sin(x+dx)/(x+dx)^3)-(sin(x)/(x)^3))/dx;

out = abs(forward_differencing - analytical_derivative);

end


% Function for 2nd order approximation
function out = second_order_approximations(x,dx)

analytical_derivative = ((x^3*(cos(x))-sin(x)*3*x^2))/x^6;
central_differencing = ((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx);

out = abs(central_differencing - analytical_derivative);

end


% Function for 3rd order approximation
function out = fourth_order_approximations(x,dx)

analytical_derivative = ((x^3*(cos(x))-sin(x)*3*x^2))/x^6;
fourth_order_approxiation= ((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);

out = abs(fourth_order_approxiation - analytical_derivative);

end

Plots:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.