



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




ME2016, Dr. Ferri Homework Solution 1
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-
Unit square, Area = 1
Unit ¼ circle, Area = π/
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