Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

a=-1;b=1;c=3;

%Define constants a, b ,c with respective values of -1,1,and 3.


x=-10:1:10;
%Define x vector from -10 to 10 with increment of 1.
%This is to be the step size of function.
y=(a*(x.^3))-(4*b*(x.^2))+(3*x)+(8*c);
%Define the third order equation.
dydxf = diff(y)./diff(x);
%Define a difference function for forward derivative technique.
dydxf(length(x))=NaN;
%Assign a 'not a number' placeholder for the last element.
dydxb=diff(y)./diff(x);
%Define a difference function for backward derivative technique.
dydxb=[NaN,dydxb];
%Assign a 'not a number' placeholder for the first element.
dydxc=gradient(y,x);
%Define a difference function for central derivative technique.
%The first and the last element are forward and backward differences respectively.
%But all interior elements are central differences.
figure
plot(x,dydxf,'-gx',x,dydxb,'-ko',x,dydxc,'-.r')
%Plot all three functions on the same graph.
hold on
%Hold on to graph so it won't reset for the next plot
dydx=(3*a*(x.^2))-(8*b*x)+3;
%define the analytical derivative function.

error_percentagef= abs((dydx
error_percentageb= abs((dydx
error_percentagec= abs((dydx
%calculate error percentages

-dydxf)./dydx)*100;
-dydxb)./dydx)*100;
-dydxc)./dydx)*100;
for all three techniques with respect to analytical va

plot(x,dydx,'b')
%plot the analytical derivative function on the same graph with blue line
title({'Forward, Backward, and Central Difference Approximation of';'the derivative
xlabel('x');ylabel('dydx')
%put down the title of the graph and also label axis x as x and axis y as the deriv
legend('dydxf','dydxb','dydxc','dydx')
%add legend for all line curves generated.

%Do the numerical results closely approximate the analytical solution?


%Explain why or why not (think about error and step size)?
%If not, what can be done to make the approximation more accurate?
%Output your response to the screen using fprintf.

%%Generate tables of differentiation techniques with error values included.


table1=[x;dydx;dydxf;error_percentagef];
disp('Forward Difference Approximation')
disp('x
dy/dx (analytical) dy/dx(f) %error')
fprintf('%8.4f\t%8.4f\t%8.4f\t%8.4f\n',table1)
table2=[x;dydx;dydxb;error_percentageb];
disp('Backward Difference Approximation')
disp('x
dy/dx (analytical) dy/dx(b) %error')
fprintf('%8.4f\t%8.4f\t%8.4f\t%8.4f\n',table2)
table3=[x;dydx;dydxc;error_percentagec];
disp('Central Difference Approximation')
disp('x
dy/dx (analytical) dy/dx(c) %error')
fprintf('%8.4f\t%8.4f\t%8.4f\t%8.4f\n',table3)
fprintf('From the tables generated, errors for forward and backward techniques \nar
fprintf('\nLarge time step size would generate huge error \nbetween the numerical a
fprintf('\nIn order to increase the accuracy of numerical technique,\nthe step size
Forward Difference Approximation
x
dy/dx (analytical) dy/dx(f) %error
-10.0000 -217.0000 -192.0000 11.5207
-9.0000 -168.0000 -146.0000 13.0952
-8.0000 -125.0000 -106.0000 15.2000

-7.0000 -88.0000 -72.0000 18.1818


-6.0000 -57.0000 -44.0000 22.8070
-5.0000 -32.0000 -22.0000 31.2500
-4.0000 -13.0000 -6.0000 53.8462
-3.0000
0.0000
4.0000
Inf
-2.0000
7.0000
8.0000 14.2857
-1.0000
8.0000
6.0000 25.0000
0.0000
3.0000 -2.0000 166.6667
1.0000 -8.0000 -16.0000 100.0000
2.0000 -25.0000 -36.0000 44.0000
3.0000 -48.0000 -62.0000 29.1667
4.0000 -77.0000 -94.0000 22.0779
5.0000 -112.0000 -132.0000 17.8571
6.0000 -153.0000 -176.0000 15.0327
7.0000 -200.0000 -226.0000 13.0000
8.0000 -253.0000 -282.0000 11.4625
9.0000 -312.0000 -344.0000 10.2564
10.0000 -377.0000
NaN
NaN
Backward Difference Approximation
x
dy/dx (analytical) dy/dx(b) %error
-10.0000 -217.0000
NaN
NaN
-9.0000 -168.0000 -192.0000 14.2857
-8.0000 -125.0000 -146.0000 16.8000
-7.0000 -88.0000 -106.0000 20.4545
-6.0000 -57.0000 -72.0000 26.3158
-5.0000 -32.0000 -44.0000 37.5000
-4.0000 -13.0000 -22.0000 69.2308
-3.0000
0.0000 -6.0000
Inf
-2.0000
7.0000
4.0000 42.8571
-1.0000
8.0000
8.0000
0.0000
0.0000
3.0000
6.0000 100.0000
1.0000 -8.0000 -2.0000 75.0000
2.0000 -25.0000 -16.0000 36.0000
3.0000 -48.0000 -36.0000 25.0000
4.0000 -77.0000 -62.0000 19.4805
5.0000 -112.0000 -94.0000 16.0714
6.0000 -153.0000 -132.0000 13.7255
7.0000 -200.0000 -176.0000 12.0000
8.0000 -253.0000 -226.0000 10.6719
9.0000 -312.0000 -282.0000
9.6154
10.0000 -377.0000 -344.0000
8.7533
Central Difference Approximation
x
dy/dx (analytical) dy/dx(c) %error
-10.0000 -217.0000 -192.0000 11.5207
-9.0000 -168.0000 -169.0000
0.5952
-8.0000 -125.0000 -126.0000
0.8000
-7.0000 -88.0000 -89.0000
1.1364
-6.0000 -57.0000 -58.0000
1.7544
-5.0000 -32.0000 -33.0000
3.1250
-4.0000 -13.0000 -14.0000
7.6923
-3.0000
0.0000 -1.0000
Inf
-2.0000
7.0000
6.0000 14.2857
-1.0000
8.0000
7.0000 12.5000
0.0000
3.0000
2.0000 33.3333

1.0000 -8.0000 -9.0000 12.5000


2.0000 -25.0000 -26.0000
4.0000
3.0000 -48.0000 -49.0000
2.0833
4.0000 -77.0000 -78.0000
1.2987
5.0000 -112.0000 -113.0000
0.8929
6.0000 -153.0000 -154.0000
0.6536
7.0000 -200.0000 -201.0000
0.5000
8.0000 -253.0000 -254.0000
0.3953
9.0000 -312.0000 -313.0000
0.3205
10.0000 -377.0000 -344.0000
8.7533
From the tables generated, errors for forward and backward techniques
are larger compared to central technique.
By then, central technique values closely resemble to that analytical values.
Conversely, forward and backward techniques have values deviated more to that analy
Large time step size would generate huge error
between the numerical and analytical solution.
Better results between the numerical and analytical solution
can be produced by decreasing the step size values
In order to increase the accuracy of numerical technique,
the step size is to be made smaller and smaller so that
the numerical solution comes closer to the true analytical solution.

Published with MATLAB R2014b

You might also like