eci115-hw5-key

.pdf

School

University of California, Davis *

*We aren’t endorsed by this school

Course

115

Subject

English

Date

Dec 6, 2023

Type

pdf

Pages

7

Uploaded by BarristerYak3836

ECI 115: Computer Methods in Civil & Environmental Engineering Assignment #5: Due Thursday May 17 at 4:00pm Problems from Chapra & Canale 7 th ed., with some additional instructions: 1. Textbook problem 22.15. Note: The problem asks you to perform Romberg integration to a tolerance of 0.1%. This refers to the % change in the integral estimate between iterations (i.e., each time you add a new row to the Romberg table). You will need to modify the class code to implement a stopping condition based on tolerance, which we did not do in class. Points 6 ( 4 points for calculations, 2 points for results) Code close all ; clear all ; clc; % Define Inliine Functions Q = @(t) 9+5.*(cos(0.4.*t).^2); % m^3/min C = @(t) 5.*exp(-0.5.*t)+2.*exp(0.15.*t); % mg/m^3 % or % M = Q(t).*C(t); % mg/min % Define initial variables t1 = 2; % minutes t2 = 8; % minutes tol = 0.1; % Percent E_a = 1; num_rows = 0; % Check accuracy and increase row number if insufficient while E_a > tol/100 num_rows = num_rows+1; R = zeros(num_rows, num_rows); % Initialize Romberg table n = 1; % first step size: (b-a), one segment h = (t2-t1)/n; % Set first value R(1,1) = (h/2)*(Q(t1).*C(t1) + 2*sum(Q(t1+h:h:t2-h).*C(t1+h:h:t2-h)) + Q(t2).*C(t2)); % first entry in table for iter = 1:num_rows n = n*2; % every new row uses 2x the number of segments h = (t2-t1)/n; R(iter+1,1) = (h/2)*(Q(t1).*C(t1) + 2*sum(Q(t1+h:h:t2-h).*C(t1+h:h:t2-h)) + Q(t2).*C(t2)); % new row entry, bottom of first column % now that we've added a new row in column 1, update all other % columns (one new value for each column) for k = 2:iter+1 j = 2 + iter - k; % indexing hack to place the new value R(j,k) = (4^(k-1) * R(j+1,k-1) - R(j,k-1)) / (4^(k-1) - 1); end end E_a = abs((R(1,num_rows+1)-R(1,num_rows))/R(1,num_rows+1)); end I = R(1,num_rows+1); % the best integral estimate % Display Results disp( '-------------------------------------------------------------------------------' ); disp([ 'Romberg estimate: ' num2str(I, 16), ' Number of Rows: ' ,num2str(num_rows+1)]);
Solution Romberg estimate: 335.9591979460033 Number of Rows: 4
2. Repeat 22.15 using 5-point and 6-point Gauss quadrature. Compare to your answer from Problem 1, and also compare to the result using the built-in quad function. Points 12 ( 4 points for calculations, 2 points for results, 6 points for comparison) Code close all ; clear all ; clc; % Define Inliine Functions Q = @(t) 9+5.*(cos(0.4.*t).^2); % m^3/min C = @(t) 5.*exp(-0.5.*t)+2.*exp(0.15.*t); % mg/m^3 % or % M = Q(t).*C(t); % mg/min % Define initial variables t1 = 2; % minutes t2 = 8; % minutes %5-point Gauss quadrature c = [0.2369269, 0.4786287, 0.5688889, 0.4786287, 0.2369269]; x = [-0.906179846, -0.538469310, 0.0, 0.538469310, 0.906179846]; y = (t1+t2)/2 + (t2-t1)/2 * x; I = (t2-t1)/2 * sum(c.*(Q(y).*C(y))); disp( '-------------------------------------------------------------------------------' ); disp([ 'Gauss 5-pt estimate: ' num2str(I, 16)]); clear y I %6-point Gauss quadrature c = [0.1713245, 0.3607616, 0.4679139, 0.4679139, 0.3607616, 0.1713245]; x = [-0.932469514, -0.661209386, -0.238619186, 0.238619186, 0.661209386, 0.932469514 ]; y = (t1+t2)/2 + (t2-t1)/2 * x; I = (t2-t1)/2 * sum(c.*(Q(y).*C(y))); disp( '-------------------------------------------------------------------------------' ); disp([ 'Gauss 6-pt estimate: ' num2str(I, 16)]); clear y I % Built in quadrature M = @(t) (9+5.*(cos(0.4.*t).^2)).*(5.*exp(-0.5.*t)+2.*exp(0.15.*t)); % mg/min [I, num_evals] = quad(M,t1,t2); disp( '-------------------------------------------------------------------------------' ); disp([ 'Built-in "quad" estimate: ' num2str(I, 16)]); Solution Gauss 5-pt estimate: 335.9623703901323 Gauss 6-pt estimate: 335.9625339188002 Built-in "quad" estimate: 335.9625300764334
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help