Project

Week 3 - Taylor table method and Matlab code

function y=f(x);

y=exp(x)*cos(x);

end

% Taylors Table Derivative

% Analytical Way

clc
clear all
close all
x=pi/3;
analytical=-2*exp(x)*sin(x);
n=10;
dx=linspace(0.01,1,10);
% Using Forward Difference Scheme
for i=1:length(dx )
forward(i)=((35*f(x))-(104*f(x+dx(i)))+(114*f(x+(2*dx(i))))-(56*f(x+(3*dx((i)))))+(11*f(x+(4*dx(i)))))/(12*(dx(i))^2);
backward(i)=((35*f(x))-(104*f(x-dx(i)))+(114*f(x-(2*dx(i))))-(56*f(x-(3*dx((i)))))+(11*f(x-(4*dx(i)))))/(12*(dx(i))^2);
cds(i)=((-30*f(x))+(16*f(x-dx(i)))+(16*f(x+(dx(i))))-(f(x+(2*dx((i)))))-(f(x-(2*dx(i)))))/(12*(dx(i))^2);
error_forward(i)=abs(forward(i)-analytical);
error_backward(i)=abs(backward(i)-analytical);
error_cds(i)=abs(cds(i)-analytical);
end
loglog(dx,error_forward,\'-kd\',\'markerfacecolor\',\'r\',\'markersize\',7)
grid on
hold on
loglog(dx,error_backward,\'-ko\',\'markerfacecolor\',\'b\',\'markersize\',7)
hold on
loglog(dx,error_cds,\'-ks\',\'markerfacecolor\',\'g\',\'markersize\',7)
fprintf(\' \\n Forward scheme= %g\',forward(n));
fprintf(\' \\n backward scheme = %g\',backward(n));
fprintf(\' \\n cds scheme = %g\',cds(n));
fprintf(\' \\n Actual derivative = %g\',analytical);
fprintf(\' \\n Forward Error = %g\',error_forward(n));
fprintf(\' \\n Backward Error = %g\',error_backward(n));
fprintf(\' \\n CDS Error= %g\',error_cds(n));
xlabel(\'step size\')
ylabel(\'error\')
legend(\'SKEWED RIGHT\',\'SKEWED LEFT\',\'CDS\',\'location\',\'northwest\')
title(\'step size vs relative error\');

fname={\'forward\',\'backward\',\'cds\'};
figure(2)
x=[1:3];
y=[error_forward(n) error_backward(n) error_cds(n)];
bar(x,y)
set(gca,\'xTick\',1:length(fname),\'xTickLabel\',fname);
xlabel(\'order\')
ylabel(\'error\')
legend(\'error prediction using 1 2 4 order approximation\')

Results:

Forward scheme= 47.5215
backward scheme = -2.59705
cds scheme = -5.17395
Actual derivative = -4.93575
Forward Error = 52.4573
Backward Error = 2.3387
CDS Error= 0.238207>>

a. Provide a plot that compares the absolute error between the above mentioned schemes

Answer:Explanation:1 We cab see that forward scheme results least accurate as error is higher. We can see from bar graph.

\"\"

b. How do these errors compare with FDS and BDS approximations

Output: Error is least with cds scheme.

\"\"

c. In your own words, describe why a skewed scheme is useful? What can a skewed scheme do that a CD scheme cannot do?

When we have values for end points then skewed schemes are useful. For such case we cant use CDS scheme.

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.