CMPS 251 Homework Assignment 5 Solution, Fall 2015

American University of Beirut · Numerical Computing · 6 pages · with solution

All CMPS 251 files
CMPS 251 Homework Assignment 5 Solution, Fall 2015, page 1

Stuck on a question? A tutor can walk you through it.

CMPS 251 Homework Assignment 5 Solution, Fall 2015, page 2 CMPS 251 Homework Assignment 5 Solution, Fall 2015, page 3 CMPS 251 Homework Assignment 5 Solution, Fall 2015, page 4 CMPS 251 Homework Assignment 5 Solution, Fall 2015, page 5 CMPS 251 Homework Assignment 5 Solution, Fall 2015, page 6

More CMPS 251 files

Show the text of this homework
CMPS 251 Assignment 5-Solution Problem 1 This is a least squares problems. We apply the techniques from Section 9.1. Follow example in Slides 21- 25 as the derivation is similar except that we have three exponential functions. In MATLAB, the following could be done clear;clc t=[0.5 1 2 3 4 5 6 7 9]'; p=[6 4.4 3.2 2.7 2 1.9 1.7 1.4 1.1]'; A = [exp(-1.5*t),exp(-0.3*t),exp(-0.05*t)]; % get the solution x=[A,B,C]^T x=(A'*A)\(A'*p); tt=0.5:0.01:9; sol=x(1)*exp(-1.5*tt)+x(2)*exp(-0.3*tt)+x(3)*exp(-0.05*tt); plot(tt, sol); hold on; grid on; scatter(t,p); A=4.1375 B=2.8959 C=1.5349 0 1 2 3 4 5 6 7 8 9 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 Problem 2 We have 𝑇(ℎ) = 𝐼(𝑓; 𝑎, 𝑎 + ℎ) − 𝐶1ℎ3 − 𝑂(ℎ𝛼) = 𝐼(𝑓; 𝑎, 𝑎 + ℎ) + 2𝐶2ℎ3 − 𝑂(ℎ𝛼) 𝑀(ℎ) = 𝐼(𝑓; 𝑎, 𝑎 + ℎ) − 𝐶2ℎ3 − 𝑂(ℎ𝛽) Now 𝑇(ℎ) + 2𝑀(ℎ) = 3𝐼(𝑓; 𝑎, 𝑎 + ℎ) − 𝑂(ℎ𝛾) 𝐼(𝑓; 𝑎, 𝑎 + ℎ) = 𝑇(ℎ) + 2𝑀(ℎ) 3 + 𝑂(ℎ𝛾) Where 𝛾 is strictly greater than 3. Problem 3 1. ∫ 𝑥 ln 𝑥 𝑑𝑥 𝑛 = 4 2 1 Using the composite trapezoidal rule with ℎ = 1 4, we get 𝑇(ℎ) = ℎ 2 (𝑓(1) + 𝑓(2) + 2𝑓(1.25) + 2𝑓(1.5) + 2𝑓(1.75)) = 0.6399 Using the Composite Simpson's rule with ℎ = 1 4, we get 𝑆(ℎ) = ℎ 3 (𝑓(1) + 4𝑓(1.25) + 2𝑓(1.5) + 4𝑓(1.75) + 𝑓(2)) = 0.6363 2. ∫ 𝑒2𝑥 sin 3𝑥 𝑑𝑥 𝑛 = 8 2 0 Using the composite trapezoidal rule with ℎ = 1 4, we get 𝑇(ℎ) = ℎ 2 (𝑓(0) + 𝑓(2) + 2[𝑓(0.25) + 𝑓(0.5) + 𝑓(0.75) + 𝑓(1) + 𝑓(1.25) + 𝑓(1.5) + 𝑓(1.75)]) = −13.576 Using the composite Simpson rule with ℎ = 1 4, we get 𝑆(ℎ) = ℎ 3 (𝑓(0) + 4𝑓(0.25) + 2𝑓(0.5) + 4𝑓(0.75) + 2𝑓(1) + 4𝑓(1.25) + 2𝑓(1.5) + 4𝑓(1.75) + 𝑓(2)) = −14.1833 3. Now we need to determine n and h to get an error of 10-4. We have 𝑓(𝑥) = 𝑒2𝑥 sin 3𝑥 , 𝑓′(𝑥) = 𝑒2𝑥(3 cos 3𝑥 + 2 sin 3𝑥) , 𝑓′′(𝑥) = 𝑒2𝑥(12 cos 3𝑥 − 5 sin 3𝑥) , 𝑓′′′(𝑥) = 𝑒2𝑥(9 cos 3𝑥 − 46 sin 3𝑥), 𝑓′′′′(𝑥) = −𝑒2𝑥(120 cos 3𝑥 + 119 sin 3𝑥) Using the Trapezoidal rule, we need | 1 12 (𝑏 − 𝑎)ℎ2𝑓′′(𝜉)| < 10−4 2 12 ℎ2 ⋅ 705.4 < 10−4 ℎ = 9.22 ⋅ 10−4 𝑛 > 2168 Using the Simpson's Rule we need | 1 180 (𝑏 − 𝑎)ℎ4𝑓′′′′(𝜉)| < 10−4 1 180 ⋅ 2ℎ4 ⋅ 2845 < 10−4 ℎ < 0.0422 𝑛 > 47 4. ∫ ∫ 𝑥𝑦2𝑑𝑦𝑑𝑥 1.4 1.2 2.5 2.1 We have n=2 so x is discretized into 𝑥0 = 2.1, 𝑥1 = 2.3 𝑎𝑛𝑑 𝑥2 = 2.5 y is discretized into 𝑦0 = 1.2, 𝑦1 = 1.3, 𝑦2 = 1.4 Using a trapezoidal rule, the boundary nodes have a weight of ¼ and the internal nodes a weight of ½ The integral is given as follows 𝐼 = 1 4 (1 4 (2.1)(1.2)2 + 1 2 (2.1)(1.3)2 + 1 4 (2.1)(1.4)2) + 1 2 (1 4 (2.3)(1.2)2 + 1 2 (2.3)(1.3)2 + 1 4 (2.3)(1.4)2) + 1 4 (1 4 (2.5)(1.2)2 + 1 2 (2.5)(1.3)2 + 1 4 (2.5)(1.4)2) = 3.8985 Problem 4 On the interval [-1;1] We have 𝐼 = 𝐴𝑓(−1) + 𝐵𝑓 (− 1 3) + 𝐶𝑓 ( 1 3) + 𝐷𝑓(1) We need to evaluate the integral for the following monomials 1, 𝑥, 𝑥2 and 𝑥3 ∫ 𝑑𝑥 = 2 = 𝐴 + 𝐵 + 𝐶 + 𝐷 1 −1 ∫ 𝑥 1 −1 𝑑𝑥 = 0 = −𝐴 − 𝐵 3 + 𝐶 3 + 𝐷 ∫ 𝑥2 1 −1 𝑑𝑥 = 2 3 = 𝐴 + 𝐵 9 + 𝐶 9 + 𝐷 ∫ 𝑥3 1 −1 𝑑𝑥 = 0 = −𝐴 − 𝐵 27 + 𝐶 27 + 𝐷 Solving these equations, we get 𝐴 = 1 4 , 𝐵 = 3 4 , 𝐶 = 3 4 , 𝐷 = 1 4 Now using the linear mapping with a weight of 3ℎ 2 we get 𝐼 = 3ℎ 8 (𝑓(𝑎) + 3𝑓(𝑎 + ℎ) + 3𝑓(𝑎 + 2ℎ) + 𝑓(𝑎 + 3ℎ)) Now we need to calculate the error 𝐿𝑒𝑡 𝐹(𝑥) = ∫ 𝑓(𝑡)𝑑𝑡 𝑥 𝑎 𝐹(𝑎) = 0 𝐹(𝑎 + 3ℎ) = 𝐹(𝑎) + 3ℎ𝐹′(𝑎) + 9ℎ2 2 𝐹′′(𝑎) + 27ℎ3 6 𝐹′′′(𝑎) + 81ℎ4 24 𝐹′′′′(𝑎) + 243ℎ5 120 𝐹(5)(𝑎) + ⋯ = 3ℎ𝑓 + 9ℎ2 2 𝑓′ + 27ℎ3 6 𝑓′′ + 81ℎ4 24 𝑓′′′ + 243ℎ5 120 𝑓′′′′ + ⋯ 3ℎ 8 (𝑓(𝑎) + 3𝑓(𝑎 + ℎ) + 3𝑓(𝑎 + 2ℎ) + 𝑓(𝑎 + 3ℎ)) = 3ℎ 8 (8𝑓 + 12ℎ𝑓′ + ℎ2 2 (3 + 3.22 + 32)𝑓′′ + ℎ3 6 (3 + 3.23 + 33)𝑓′′′ + ℎ4 24 (3 + 3.24 + 34)) 𝑓′′′′ + ⋯ = 3ℎ𝑓 + 9ℎ2 2 𝑓′ + 27ℎ3 6 𝑓′′ + 81ℎ4 24 𝑓′′′ + 33ℎ5 16 𝑓′′′′ + ⋯ Subtracting the two entities leaves us with a truncation error of 243ℎ5 120 𝑓′′′′ − 33ℎ5 16 𝑓′′′′ = − 3 80 ℎ5𝑓′′′′(𝜉) Problem 5 function R=Romberg(f,a,b,n) h=b-a; R(1,1)=h/2*(f(a)+f(b)); for i=2:n+1 h=h/2; sum=0; for k=1:2:2^(i-1)-1 sum=sum+f(a+k*h); end R(i,1)=0.5*R(i-1,1)+sum*h; for j=2:i R(i,j)=R(i,j-1)+(R(i,j-1)-R(i-1,j-1))/(4^(j-1)-1); end end function f=testRom(x) f=4./(1+x.^2); R=Romberg(@testRom,0,1,5) Problem 6 function [result,x]=Simpson(f,a,b,tol,lev,lev_max) lev=lev+1; x=[a,b]; h=b-a; c=(a+b)/2; x=[x,c]; one_sim=h*(f(a)+4*f(c)+f(b))/6; d=(a+c)/2; e=(c+b)/2; x=[x,d,e]; two_sim=h*(f(a)+4*f(d)+2*f(c)+4*f(e)+f(b))/12; if lev>lev_max disp('max achieved') elseif abs(two_sim-one_sim)<15*tol result=two_sim+(two_sim-one_sim)/15; else [lef,x1]=Simpson(f,a,c,tol/2,lev,lev_max); [right,x2]=Simpson(f,c,b,tol/2,lev,lev_max); result=lef+right; x=[x,x1,x2]; end function y=testfun(x) y=cos(2.*x)./exp(x); 0 0.5 1 1.5 2 2.5 3 3.5 4 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1