Docsity

Taylor Series Expansion of cos(x) and Error Analysis - Prof. Aldo Ferri, Assignments of Computer Science

Toggle sidebar
bg1
ME2016, Dr. Ferri
Homework Solution 1
1. (4 points) The Taylor series expansion of cos(x) is given by
"+−+−= !
x
!
x
!
x
)xcos( 642
1
642
(a) Plot cos(x) vs x from -3<x<3 using increments of x equal to 0.1.
(b) On the same plot, compare cos(x) with 1-x2/2, and 1-x2/2+x4/24 (3 curves) over the same
range of x considered in part (a)
(c) Make a table similar to the one shown in Example 3.2 of the text for the case of x =1. You
may want to use the Matlab program calc_exp.m which performs a similar operation for the
function f(x) = ex ; the program is available in the Announcements section of the class
website.
(d) How many terms are needed before the true percent relative error, εt, is less than 0.01%?
(a) and (b): The following commands can be used to generate Figure 1 below.
» x=-3:0.1:3;
» exact=cos(x);
» two_terms = 1-(x.^2)/2;
» three_terms = 1-(x.^2)/2+(x.^4)/24;
» plot(x,exact,x,two_terms,'--',x,three_terms,'-.')
» xlabel('x')
» ylabel('y')
» legend('exact','2 terms','3 terms')
-3 -2 -1 0123
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
0.5
1
x
y
exact
2 terms
3 terms
pf3
pf4
pf5

Partial preview of the text

Download Taylor Series Expansion of cos(x) and Error Analysis - Prof. Aldo Ferri and more Assignments Computer Science in PDF only on Docsity!

ME2016, Dr. Ferri Homework Solution 1

  1. (4 points) The Taylor series expansion of cos(x) is given by

x !

x !

x cos( x) 2 4 6

2 4 6

(a) Plot cos(x) vs x from -3<x<3 using increments of x equal to 0.1. (b) On the same plot, compare cos(x) with 1-x^2 /2, and 1-x^2 /2+x^4 /24 (3 curves) over the same range of x considered in part (a) (c) Make a table similar to the one shown in Example 3.2 of the text for the case of x =1. You may want to use the Matlab program calc_exp.m which performs a similar operation for the function f(x) = ex ; the program is available in the Announcements section of the class website. (d) How many terms are needed before the true percent relative error, εt, is less than 0.01%?

(a) and (b): The following commands can be used to generate Figure 1 below.

» x=-3:0.1:3; » exact=cos(x); » two_terms = 1-(x.^2)/2; » three_terms = 1-(x.^2)/2+(x.^4)/24; » plot(x,exact,x,two_terms,'--',x,three_terms,'-.') » xlabel('x') » ylabel('y') » legend('exact','2 terms','3 terms')

-3 -2 -1 0 1 2 3

-3.

-2.

-1.

-0.

0

1

x

y

exact 2 terms 3 terms

Note that the Taylor series appears to be fairly accurate for |x|<<1. It is also seen that the 3-term series is accurate over a broader range of x than is the 2-term series.

(c) The code below is a slightly modified version of calc_exp

% This program computes the cosine % function by means of a Taylor series

% Input x and n

x=input('Enter x... '); n=input('Enter number of terms... ');

% initialize variables and vectors

exact_value = cos(x) ; eps_t = 100*(exact_value - 1)/exact_value; EPS_t = zeros(1,n); EPS_t(1) = eps_t; EPS_a = zeros(1,n); SUM = ones(1,n);

sum=1; % the first term in the sum is 1 fact = 1;

% Begin loop to compute and sum terms 2 through n

for k=2:n;

p=(2k-2);* sum = sum + ((-1)^(k+1))(x^p)/factorial(p)* ;

eps_t = 100(exact_value - sum)/exact_value; eps_a = 100(sum - SUM(k-1))/sum;

% Store results

SUM(k) = sum; EPS_t(k) = eps_t; EPS_a(k) = eps_a;

end

approximate_value = sum;

% Display results

disp(' ') disp(' ') disp(' Terms Result eps_t(%) eps_a(%)') disp(' ') disp([(1:n)' SUM' EPS_t' EPS_a'])

Starting with an initial guess of x = 1, perform 4 iterations of the algorithm. At the end of the 4th iteration, what is the true percent relative error, εt? What is the approximate percent relative error, εa, between the 3rd iterate and the 4th iterate for x?

There are a number of ways to do this problem. Of course, we can just iterate with the algorithm directly in the Matlab Workspace:

» x = 1; » x=x+sin(x) x = 1.8415e+ » x=x+sin(x) x = 2.8051e+ » x=x+sin(x) x = 3.1353e+ » x_old = x; » x=x+sin(x) x = 3.1416e+ » eps_t = 100(pi-x)/pi eps_t = 1.3369e-*

» eps_a = 100(x-x_old)/x eps_a = 2.0105e-*

A better approach is to write a Matlab program or “script” file. In the code below, a table is constructed by adding rows to an existing table. The variable “NaN” is a built-in Matlab quantity called “not-a-number.” It is used here because we need at least 2 iterations before we can define εa.

x = 1; eps_t = 100(pi-x)/pi; Tab = [0 x eps_t NaN]; % top row for "zeroth iteration" for k = 1: x_old = x; x = x + sin(x); eps_a = 100(x-x_old)/x; eps_t = 100(pi-x)/pi; Tab = [Tab; [k x eps_t eps_a] ]; % add row for kth iteration end*

format short e

disp(' ') disp(' iteration# x eps_t eps_a') disp(Tab)

The output from this code is:

iteration# x eps_t eps_a 0 1.0000e+000 6.8169e+001 NaN 1.0000e+000 1.8415e+000 4.1384e+001 4.5696e+ 2.0000e+000 2.8051e+000 1.0712e+001 3.4352e+ 3.0000e+000 3.1353e+000 2.0105e-001 1.0532e+ 4.0000e+000 3.1416e+000 1.3369e-006 2.0105e-

So, the answer to the question is that ε t = 1.3369e-006 and ε a = 2.0105e-

  1. (2 points) The Matlab command rand produces a uniformly-distributed random number between 0 and 1. For example, the Matlab commands x = rand; y = rand; will produce a point randomly located in the unit square: 0 ≤ x ≤ 1, 0 ≤ y ≤ 1. The probability that the randomly- generated point (x,y) will lie within the "unit quarter-circle" is equal to the ratio between the area of the unit quarter-circle and that of a unit square; i.e., π/4. [Note that (x,y) lies within the unit quarter circle if : x^2 + y^2 < 1.] Write a Matlab program that will (a) generate a minimum of 10,000 points (x,y); (b) test each point and increment a counter if x^2 + y^2 < 1, (c) use the final value of the counter to produce an estimate of π, (d) calculate the relative percent true error, εt.

Unit square, Area = 1

Unit ¼ circle, Area = π/

1.0 x

y

0

The following code performs the desired operations:

n = input('Enter number of random points ... '); count = 0; for k=1:n; x=rand; y=rand; if x^2 + y^2 < 1; count = count +1; end; end;

pi_est = 4count/n eps_t=100(pi - pi_est)/pi