Project

Discretization basics for range of dx using function

Aim: To write a code that compares the approximation error of first, second and fourth order approximations of first derivative against range of dx

Description: The function that I'm using is f(x)=sin(x)x3">f(x)=sin(x)x3f(x)=sin(x)x3 by taking the first derivative at x=π3">x=pi/3.

In this i will show how the output of 1st, 2nd and 4th order approximations varies with respect to grid size (dx) and ploting the outputs to know the error using a log graph.

Procedure:

We can consider any value for dx (where dx is grid spacing in approximation equations)

let us consider some range of dx (ie..pi/30 to pi/3000) with 30 values of dx in between the range.

firstly, create a function to be called

function [first_order_error,second_order_error,fourth_order_error]= approximation(x,dx)

first_order_approximation=( (sin(x)/x^3)- (sin(x-dx)/(x-dx)^3))/dx ;
second_order_approximation= ((sin(x+dx)/(x+dx)^3)- (sin(x-dx)/(x-dx)^3))/(2*dx);
fourth_order_approximation= ((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);
analytical_answer= (x^3*(cos(x)) - sin(x)*3*x^2)/x^6;
first_order_error = abs(analytical_answer-first_order_approximation)
second_order_error = abs(analytical_answer-second_order_approximation)
fourth_order_error = abs(analytical_answer-fourth_order_approximation)

end

code to solve the function

close all
clear all
clc


dx=linspace(pi/3,pi/5000,30);
x=pi/3;


for i=1:length(dx)
    [first_order_error(i),second_order_error(i),fourth_order_error(i)]= approximation(x,dx(i))
end

loglog(dx,first_order_error,'-r')
hold on 
loglog(dx,second_order_error,'-b')
hold on 
loglog(dx,fourth_order_error,'-m')
legend('First order error','Second order error','Fourth order error','location','northwest')
xlabel('Range of dx')
ylabel('Error magnitude')
grid on

Output:

Observations from output graph:

1) Slope of the first order error curve is the lowest and the slope of the fourth order error curve is the highest.

2)As the grid size dx and approximation is increasing, these curves are bending towards the origin.

Result:

Small change in dx value will produce a large change in the error values.

The errors are reducing with increase in the value of dx (lower values of grid size)

For the same value of dx, the errors in fourth order approximation are less

Conclusion:

Higher order approximations of a function will produce minimal errors and the solution obtained is closer to the analytical solution.

Higher the order of approxiamtion ∝ larger range of dx ∝ minimal error

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.

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.