Project

Comparison of the first, second and fourth-order error approximations for first-order differential equation

Title: Comparison of the first, second and fourth-order error approximations for first-order differential equation

Initially, the problem solving started with giving input variables.

There are two variables x and dx while I assumed `x=pi/3` (a constant value). 'dx' is a variable in this comparison project.

So, I have taken a bandwidth of `dx=[pi/4 to pi/4000]`.

Firstly, Solved the analytical derivative with the formula `a*n^(n-1)`

`sin(x)/x^3` becomes `(cos(x)/x^3) - ((3*sin(x))/x^4)`

Simultaneously defined three functions

  • First order approximation
  • Second order approximation
  • Fourth order approximation

Using for loop dx value will repeatedly embed into the three functions one after another through the given array

After this step, the focus should be on the error. So to found out this the function error is defined inside the for loop

abs(First_order_differencial - Analytical_derivative) similarly for second and fourth order equations.

Then I arrived at a place where I have a bunch of values of errors for three of the functions. The very next step is Plotting the graph

Firstly the graph has been plotted on normal scale but doesn't give you a picture of comparison

So, I took a 'loglog' plot compare in a smaller extent taking log on both the x and y variables of the graph

This will show the error trend of three functions

Observations:

The second graph depicts the fourth order error is minimum among all but the observable factor is the rate of change. So, this led me to calculate the slope of the functions.

Slope: To find out the slope of the function I carried out the process with the polyfit function in octave. Where polyfit gives you the coefficients of the functions and the graph shows that three functions are behaving like `y=Mx+C`. So, an array of results is generated to seperate the x coefficients of the graph

This gave the slope values

Slopes =

1.1768

7.0256

39.3472

This shows that in the fourth-order differential equation even a small change in dx can incredibly affect the error value

clear all
close all
clc

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

%Analytical Derivative of function sin(x)/x^3

Analytical_derivative = (cos(x)/x^3) - ((3*sin(x))/x^4);

for i= 1:length(dx)
  
%first order approximation through forward differencing = (f(x+dx)-f(x))/dx

First_order_differencial(i)= ((sin(x+dx(i))/(x+dx(i))^3) - (sin(x)/x^3))/dx(i)

%Error in first order approximation

First_order_error = abs(First_order_differencial - Analytical_derivative)

%second order approximation through central differencing = (f(x+dx)-f(x))/dx

second_order_differencial(i)= ((sin(x+dx(i))/(x+dx(i))^3) - (sin(x-dx(i))/(x-dx(i))^3))/(2*dx(i))

%Error in second order approximation

Second_order_error = abs(second_order_differencial - Analytical_derivative)

%Fourth order approximation 

fourth_order_differencial(i) = ((sin(x-2*dx(i))/((x-2*dx(i))^3))+ (8*(sin(x+dx(i))/((x+dx(i))^3))) - (8*(sin(x-dx(i))/((x-dx(i))^3)))-(sin(x+2*dx(i))/((x+2*dx(i))^3)))/(12*dx(i))

%Error in fourth order approximation

Fourth_order_error = abs(fourth_order_differencial - Analytical_derivative)

end

%Line diagram plotting at this specific x 
subplot(2,1,1)
plot(dx,First_order_error,'color','b')
hold on
plot(dx,Second_order_error,'color','y')
hold on
plot(dx,Fourth_order_error,'color','r')
xlabel('dx')
ylabel('error')
legend('first order error','second order error','third order error')
title('Comparison of first second and third order error')

subplot(2,1,2)
plot(dx,First_order_error,'color','b')
hold on
loglog(dx,Second_order_error,'color','y')
hold on
loglog(dx,Fourth_order_error,'color','r')
xlabel('log(dx)')
ylabel('log(error)')
legend('first order error','second order error','third order error')
title('Comparison of first second and third order error')

%slope of three graphs
Coef_1 = polyfit(dx,First_order_error,1);
Coef_2 = polyfit(dx,Second_order_error,1);
Coef_3 = polyfit(dx,Fourth_order_error,1);
Slopes = [ Coef_1(1),Coef_2(1),Coef_3(1)] 


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.