Project

Discretization error for a range of dx

objective:

Write a program to computed the first, second and fourth order approximations for a given value of x, also write the program to compare the variation of error for a range of dx and plot graph for a range of dx, plot the first order, second and fourth order error.

effect of dx on error:

objective is to plot the range of dx vs error for first , second and fourth order error

program for first order approximation for function sinx/x^3

function out=first_order_approximation(x,dx)
%analyticalderivative=sin(x)/x^3
%f'(x)=(x^3*(cos(x))-sin(x)*3*x^2)/x^6
analytical_derivative=(x^3*(cos(x))-sin(x)*3*x^2)/x^6;
%numerical derivative
forward_differencing=(sin(x+dx)/(x+dx)^3-sin(x)/(x^3))/dx;
out=abs(forward_differencing-analytical_derivative)
end

program for second order approximation for function sinx/x^3

function out=second_order_approximation(x,dx)
%analyticalderivative=sin(x)/x^3
%f'(x)=(x^3*(cos(x))-sin(x)*3*x^2)/x^6
analytical_derivative=(x^3*(cos(x))-sin(x)*3*x^2)/x^6;
%numerical derivative
central_differencing=(sin(x+dx)/(x+dx)^3-sin(x-dx)/(x-dx)^3)/(2*dx);
out=abs(central_differencing-analytical_derivative)
end

program for fourth order approximation for function sinx/x^3

function out=fourth_order_approximation(x,dx)
%analyticalderivative=sin(x)/x^3
%f'(x)=(x^3*(cos(x))-sin(x)*3*x^2)/x^6
analytical_derivative=(x^3*(cos(x))-sin(x)*3*x^2)/x^6;
%numerical derivative
central_differencing_1 = ((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(central_differencing_1-analytical_derivative)
end

explaination:

in all the above programs function command is used to determine the first order, second order and fourth order error respectively.

for the given function analytical derivative is differentiated and assigned to the variableaccordingly for the higher orders.

the effect of dx for the respective errors is found using the error formula.

abs command is used to determine the error.

main program:

clear all
close all
clc
x=pi/3;
dx=linspace(pi/3,pi/3000,20);
for i=1:length(dx)
first_order_error(i)=first_order_approximation(x,dx(i));
second_order_error(i)=second_order_approximation(x,dx(i));
fourth_order_error(i)=fourth_order_approximation(x,dx(i));
end
figure(1)
loglog(dx,first_order_error,'b')
hold on
loglog(dx,second_order_error,'r')
hold on
loglog(dx,fourth_order_error,'g')
xlabel('dx')
ylabel('error')
legend('first_order_error','second_order_error','fourth_order_error')

explaination:

assign starting value for x as pi/3 and ending value for x as pi/3000

now dx values is assued to have 20 values in between pi/3 and pi/3000

use linspace command to define dx values

to go through all the 20 values of dx in a loop for finding error, 'for loop' command used

the function (first_order_approximation, 'second_order_approximation','fourth_order_approximation) is used in the loop for i repeating array

to plot all the error in a single plot using loglog command and hold on command.

loglog plot:

conclusion:

from the above plot we can conclude that,

with increase in dx error also increases.

error drop rate fourth order is higher than second and first order errors,

thus we can conclute that fourth order approximation results are more accurate than second and first order results.

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.