You are on page 1of 8

KCEP2105 Numerical Methods & Statistics Dr Liew HL (2013/14)

Assignment 1

[CK] W. Cheney, D. Kincaid. Numerical Mathematics and Computing, 6th edn,
Thomson Brooks/Cole, 2008.
[GS] A. Gilat, V. Subramaniam. Numerical Methods An Introduction with
Applications Using Matlab, 2nd edn, Wiley, 2011.

1. [CK-1.2.6a] Determine the first two nonzero terms of the series expansion
about zero for e
cos x
.

2. [CK-1.2.8] Determine how many terms are needed to compute e correctly
to 15 decimal places (rounded) using Taylor series for e
x
. (The max error
must be
1
2
10
15
.)

The following Matlab exercises (#4-7) are from E. Darve, H. Le. CME 102
Matlab Workbook, Stanford, 2009. Some guidelines on problems involving Matlab:

(a) You must turn in the Matlab code you write to solve problems.
(b) Graphs must be properly annotated.

3. #1.3.2

4. #1.4.2

5. #1.5.2

6. #2.1.2

7. [GV-1.40] The Taylors series expansion for sin (x) is:

sin =

3
3!
+

5
5!

7
7!
+ = (Fill in the blank

=0
).

Write a user-defined function that determines sin (x) using Taylors series
expansion. For function name and arguments, use y=sinTaylor(x).
Use a loop for adding the terms. If a
n
is the n-th term in the series, then
sum S
n
of the n terms is S
n
= S
n-1
+a
n
. In each pass, calculate the error
given by E = |

1
|. Stop adding terms when E 0.000001. Use
sinTaylor for calculating: (a) sin65, and (b) sin195. How many terms
are required?




1. Let f(x) = e
cos x

c=0
f(x) = e
cos x
f(0) = 2.718
f(x) = -(sin x)e
cos x
f(0) = 0
f(x) = (sin
2
x cos x) e
cos x
f(0) = -2.718

The first two non-zero terms is 2.718 and -2.718
2. f(x) = e
x
, f(x) = e
x


3. a) CODE from M-File:
t = 0 : 0.1 : 9;
g = 9.8;
v0 = 50.75;
theta0 = (5*pi/12)-0.255;
y0 = 0;
x0 = 0;
y = y0 - 0.5 * g * t.^2 + v0*sin(theta0).*t;
x = x0 + v0*cos(theta0).*t;
figure;
plot(x,y);
title('y(t) vs. x(t)');
xlabel('Horizontal Distance (m)');
ylabel('Altitude (m)');
grid on;

FIGURE



3. b) CODE from M-File:
t = 0 : 0.1 : 9;
g = 9.8;
v0 = 50.75;
theta0 = (5*pi/12)-0.425;
y0 = 0;
x0 = 0;
y = y0 - 0.5 * g * t.^2 + v0*sin(theta0).*t;
x = x0 + v0*cos(theta0).*t;
figure;
plot(x,y);
title('y(t) vs. x(t)');
xlabel('Horizontal Distance (m)');
ylabel('Altitude (m)');
grid on;


FIGURE




4. a) CODE from M-File:
x = -pi/2 : pi/30 : pi/2;
plot(x,atan(x),x,acot(x));
title('y = atan(x) and y = acot(x)');
xlabel('x');
ylabel('y');
legend('y = atan(x)','y = acot(x)');
grid on;

FIGURE


4. b) CODE from M-File:
x = -pi/2 : pi/30 : pi/2;
plot(x,atan(x));
title('y = atan(x) and y = acot(x)');
xlabel('x');
ylabel('y');
grid on;
hold on;
plot(x,acot(x),'r');
legend('y = atan(x)','y = acot(x)');

FIGURE

4. c) CODE from M-File:
ezplot('(2/3)*sin(9*pi*x)',[0,2*pi]);
title('High Frequency Sine Function');
xlabel('x');
ylabel('y');
grid on;

FIGURE

5. a) CODE from M-File:
n = 1:10:500;
plot(n,n.*log(n),n,n.^(0.5),n,log(n))
title('Big-O characteristics of Algorithms: Linear Plot')
ylabel('Estimate of Running Time')
xlabel('n (number of elements)')
legend('O(n*ln(n))','O(n^0.5)', 'O (ln(n))')
grid on;

FIGURE

5. b) CODE from M-File:
n = 1:10:500;
semilogy(n,n.*log(n),'b',n,n.^(0.5),'r',n,log(n),'g')
title('Big-O characteristics: Logarithmic Plot')
ylabel('Estimate of Running Time')
xlabel('n (number of elements)')
legend('O(n*ln(n))','O(n^0.5)', 'O (ln(n))')

FIGURE

6. a) CODE from M-File:
clear;
x = 2;
% Iterate 12 terms for our approximation.
EXP_Approx12 = 0;
for j=0:12
EXP_Approx12 = EXP_Approx12 + x^(j)/factorial(j);
end
EXP_Error12 = abs(EXP_Approx12 - exp(x));
% Iterate 15 terms for our approximation.
EXP_Approx15 = 0;
for j=0:15
EXP_Approx15 = EXP_Approx15 + x^(j)/factorial(j);
end
EXP_Error15 = abs(EXP_Approx15 - exp(x));
fprintf('\nError with 12 terms:\n')
fprintf ('--------------------------\n')
fprintf ( 'exp(2): %g\n',EXP_Error12 )
fprintf ('\nError with 15 terms: \n')
fprintf ('--------------------------\n')
fprintf ( 'exp(2): %g\n',EXP_Error15)

OUTPUT:
Error with 12 terms:
--------------------------
exp(2): 1.5321e-006

Error with 15 terms:
--------------------------
exp(2): 3.54651e-009




6. b) CODE from M-File:
clear;
EXP_APP = 0; % This is the exponential approximation.
n = 0; x = 3;
% Iterate until our approximation is below the error tolerance.
while abs( EXP_APP - exp(x) ) >= 0.001
EXP_APP = EXP_APP + x^(n)/factorial(n);
n = n + 1;
end
EXP_Terms = n;
EXP_Error = abs( EXP_APP - exp(x) );
% Output
fprintf ('\nNumber of Terms Needed for the function to be within the
allowed error:\n');
fprintf ('-------------------------------------------------------------
----------\n');
fprintf ('exp(3): %g terms | Error = %g\n',EXP_Terms,EXP_Error);

OUTPUT:
Number of Terms Needed for the function to be within the allowed error:
-----------------------------------------------------------------------
exp(3): 13 terms | Error = 0.000324362




6. CODE from sinTaylor.m File:
function y = sinTaylor(x)

x = x*pi/180;

y=x;

for i = 1 : 100
a = (-1)^i*x^(2*i+1)/factorial(2*i+1);
if abs(a/y) <= 1.e-6
break
end
y = y + a;
end

display(i)

a) COMMAND WINDOW:

sinTaylor(65)

OUTPUT:

i =

5

ans =

0.9063


b) COMMAND WINDOW:

sinTaylor(195)

OUTPUT:

i =

9

ans =

-0.2588

You might also like