Project

Program to compare the effect of dx on error in numerical approximations

Main program:

clear allclose allclcx=pi/3;dx=linspace(pi/30,pi/4000,10);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));endfigure(1)loglog(dx,first_order_error)hold onloglog(dx,second_order_error,'r')hold onloglog(dx,fourth_order_error,'g')legend('first order error','second order error','fourth order error')xlabel('dx')ylabel('Error')title('Log plot showing variation of error with dx')

First order approximation:

function out = first_order_approximation(x,dx)analytical_derivative = (x^3*cos(x)-sin(x)*3*x^2)/(x^6);approximation_first_order = (sin(x+dx)/((x+dx)^3)-sin(x)/(x^3))/dxout = abs(approximation_first_order-analytical_derivative);end

Second order approximation:

function out = second_order_approximation(x,dx)analytical_derivative = (x^3*cos(x)-sin(x)*3*x^2)/(x^6);approximation_second_order = ((sin(x+dx)/((x+dx)^3))-(sin(x-dx)/((x-dx)^3)))/(2*dx)out = abs(approximation_second_order-analytical_derivative);end

Fourth order approximation:

function out = fourth_order_approximation(x,dx)analytical_derivative = (x^3*cos(x)-sin(x)*3*x^2)/(x^6);approximation_fourth_order = ((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(approximation_fourth_order-analytical_derivative);end

The graph obtained is shown below:

From the above graph we can conclude that:

  • As the value of dx decreases the error decreases for first,second and fourth order derivatives.
  • The curve of fourth order approximation has the maximum slope, which means it shows the maximum change in error value with change of dx.
  • The curve of first order approximation has the minimum slope, which means it shows the least change in error value with change of dx.
  • The curve of second order approximation has a slope in between that of first order and fourth order approximation.
  • Thus, with higher order approximations the value of error with respect to dx changes more drastically as compared to a lower order derivative.
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.