FINAL INDEPENDENT PROJECT
After Derivation the MATLAB operations starts
The code are shown below
clear all
close all
clc
[A]=[1 1 1 1 1 ;0 -1 -2 -3 -4;0 1/2 2 9/2 8; 0 -1/6 -4/3 -9/2 -32/3;0 1/24 2/3 27/8 32/3];
[B]=[0; 0; 1; 0; 0];
X=linsolve(A,B);
%%Main run file
close all
clear all
clc
%%Assuming x=pi/3
x = pi/3;
%%Defining the grid size
dx=linspace(pi/10,pi/500,15);
%Calculating error
for i = 1:length(dx)
error_central(i) =central_difference(x,dx(i));
error_right(i) = skewed_right(x,dx(i));
error_left(i)=skewed_left(x,dx(i));
error_forward(i)=forward_difference(x,dx(i));
error_backward(i)=backward_difference(x,dx(i));
end
figure(1)
loglog(dx,error_central,'color', 'r');
hold on
loglog(dx,error_right,'color', 'y');
hold on
loglog(dx,error_left,'color', 'g');
xlabel('step size')
ylabel('truncation Error')
title('Comparision between different schemes for a second order derivative of 4th order accuracy')
legend('Central difference','Skewed right sided difference',' Skewed left sided difference')
%%Figure(2)
figure(2)
loglog(dx,error_central);
hold on
loglog(dx,error_right);
hold on
loglog(dx,error_left);
hold on
loglog(dx,error_forward);
hold on
loglog(dx,error_backward);
xlabel('step size')
ylabel('truncation Error')
title('Comparision between different schemes for a second order derivative of 4th order accuracy and FD and BD')
legend('CD','SRSD','SLSD','Forward difference','Backward difference')
Function code are define below:
%%Function for central difference
function [error1] = central_difference(x,dx)
%%Analytical value
analytical=-2*exp(x)*sin(x);
%%central_difference scheme (i,i-1,i-2,i+1,i+2)
central= (16*(exp(x+dx)*cos(x+dx) + exp(x-dx)*cos(x-dx)) - (exp(x+2*dx)*cos(x+2*dx) + exp(x-2*dx)*cos(x-2*dx)) - (30*exp(x)*cos(x)))/(12*dx^2);
%%Calculating the error
error1= abs(central - analytical);
end
%%Function for Skewed right sided difference
function [error2] = skewed_right(x,dx)
%%Analytical value
analytical=-2*exp(x)*sin(x);
%%Skewed right sided (i,i+1,i+2,i+3,i+4)
skewedr = (35*exp(x)*cos(x) - 104*exp(x+dx)*cos(x+dx)+ 114*exp(x+2*dx)*cos(x+2*dx) - 56*exp(x+3*dx)*cos(x+3*dx) +11*exp(x+4*dx)*cos(x+4*dx))/(12*(dx^2));
%Truncation Error
error2 = abs(skewedr-analytical);
end
%%Function for Skewed left sided difference
function [error3] = skewed_left(x,dx)
%%Analytical value
analytical=-2*exp(x)*sin(x);
%%Skewed left sided (i,i-1,i-2,i-3,i-4)
skewedl = (35*exp(x)*cos(x) - 104*exp(x-dx)*cos(x-dx)+ 114*exp(x-2*dx)*cos(x-2*dx) - 56*exp(x-3*dx)*cos(x-3*dx) + 11*exp(x-4*dx)*cos(x-4*dx))/(12*(dx^2));
%Truncation Error
error3 = abs(skewedl-analytical);
end
%%%Function for backward difference
function error5 = backward_difference(x,dx)
%%Analytical value
analytical=-2*exp(x)*sin(x);
%%Forward difference
backward= (exp(x)*cos(x)-exp(x-dx)*cos(x-dx))/dx;
%Truncation Error
error5 = abs(backward-analytical);
end
%%Function for forward difference
function error4 = forward_difference(x,dx)
%%Analytical value
analytical=-2*exp(x)*sin(x);
%%Forward difference
forward= (exp(x+dx)*cos(x+dx)-exp(x)*cos(x))/dx;
%Truncation Error
error4 = abs(forward-analytical);
end
OUTPUT are shown below
The Figure shows that central diference has the minimum error and Skewed right sided difference and left sided difference has the same error value.
figure above shows that forward and backward scheme has maximum error.