# HOMEWORK ASSIGNMENT 9.pdf - EGM 3344 Section 1234 Spring...

• 8
• 100% (4) 4 out of 4 people found this document helpful

This preview shows page 1 - 4 out of 8 pages.

EGM 3344 Section 1234 Spring 2017 Name: Benjamin Morrisey Homework Set 9 Problems Completed: 19.2, 19.5, 20.3, 20.4
19.2 Evaluate the following integral: 40(1- e-x) dx (a) analytically (b) single application of the trapezoid rule, (c) composite trapezoidal rule with n = 2 and 4, (d) single application of Simpson’s 1/3 rule with n=4, (f) Simpson’s 3/8 rule, and (g) determine the true percent relative error based on (a). (a)40 dx - 40e-x = 4 + 1/e4-1 =3.0183 (b)function I = trap(func, a, b, n, varargin) if nargin<3, error('at least 3 input arguments required'),end if ~ (b>a), error('upper bound must be greater than lower'),end if nargin<4||isempty(n), n=100;end x = a; h = (b-a)/n; s = func(a,varargin{:}); s = s+func(b,varargin{:}); I = (b-a)*s/(2); end >> f = @(x) 1-exp(-x); >> trap(f,0,4,5) ans = 1.963368722222532 >> e = ((3.0183-1.9634)/3.0183)*100 e = 34.950137494616172 (c)function I = trap(func, a, b, n, varargin) if nargin<3, error('at least 3 input arguments required'),end if ~ (b>a), error('upper bound must be greater than lower'),end if nargin<4||isempty(n), n=100;end x = a; h = (b-a)/n; s=func(a,varargin{:}); for i = 1 : n-1 x=x+h; s = s+2*func(x,varargin{:}); end s = s+func(b,varargin{:}); I = (b-a)*s/(2*n); end >> trap(f,0,4,2) ans = 2.711013794638040 >> e = ((3.0183-2.7110)/3.0183)*100 e = 10.181227843488061 >> trap(f,0,4,4) ans = 2.937840387779714 >> e= ((3.0183-2.9378)/3.0183)*100 e = 2.667064241460418
dx
(d)function I = simp(func, a, b, n, varargin)if nargin<3, error('at least 3 input arguments required'),endif ~ (b>a), error('upper bound must be greater than lower'),endif nargin<4||isempty(n), n=100;endx = a; h = (b-a)/n;s=func(a,varargin{:});for i = 1 : n-1x=x+h;s = s+4*func(x,varargin{:});end