HW4_Solutions

# HW4_Solutions - MAE 107 Computational Methods Summer...

This preview shows pages 1–4. Sign up to view the full content.

MAE 107 – Computational Methods Summer Session II 2009 Homework # 4 – Solutions Problem 1 – 5 points 1. Anaytical Solution J = Z 1 0 1 1 + x 2 dx = tan 1 x vextendsingle vextendsingle 1 0 = tan 1 1 tan 1 0 = π 4 2. simpson13.m function [I] = simpson13(f,a,b,N) % Calculates the approx. integral with the composite Simpson 1/3 rule over % N segments % Inputs: f: function handle of the function to be integrated % a,b: lower and upper bounds of the integral % N: number of intervals to be used % Check to see if N is odd if mod(N,2) ˜= 0 error(’N is odd, this function only works if there are an even number of segments’) end % Segment width h = (b-a)/N; % Initialize the sums corresponding respectively to the even and odd indices: A_i = 0; A_j = 0; % Add up all the function evaluations at the odd index points for i_index = 1:2:N-1 A_i = A_i + f(a+i_index*h); end % Add up all the function evaluations at the even index points for j_index = 2:2:N-2 A_j = A_j + f(a+j_index*h); end % Apply the Simpson’s 1/3 rule to the sum of the parts I = (h/3)*(f(a) + 4*A_i + 2*A_j + f(b)); We now test our Simpson 1/3 integration by estimating the integral of sin x on [ 0 π ] using 10 and 20 segments which we compare with the true answer R π 0 sin x d x = 2. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
>> simpson13(@(x) sin(x),0,pi,10) ans = 2.0001 >> simpson13(@(x) sin(x),0,pi,20) ans = 2.0000 3. Simpson Script for Error Analysis %integral.m % Uses the Simson13.m function do determine the error vs segments % Detemine the ’actual’ (although numerical result) J = pi/4; % Set up the increments and the vector ’pointers’ increments = [20:4:200]; I = zeros(1,length(increments)); E = zeros(1,length(increments)); % What is the function f = @(x) 1./(1+x.ˆ2); for index = 1:length(increments) % Run the simson13 code for each set of increments I(index) = simpson13(f,0,1,increments(index)); % Determine the absolute error E(index) = abs(I(index)-J); end % Plot the results figure subplot(2,1,1) plot(increments, I) title(’Integral Approx. from 0 to 1 of 1/1+xˆ2’) xlabel(’Numer of Incremetns (n)’) ylabel(’Integral Approx. Value’) subplot(2,1,2) loglog(increments,E) title(’Error of Integral Approx.’) xlabel(’Number of Increments (n)’) 2
ylabel(’Total error’) 10 1 10 2 10 3 10 -16 10 -15 10 -14 10 -13 10 -12 10 -11 10 -10 10 -9 10 -8 10 -7 Error of Integral Approx. N Absolute error Error 1/N 6 Figure 1: Variation of the absolute error with the number of increments N By looking at the decray rate of the error in a log-log plot, we see that the error decays as 1 / N 6 when N increases as the two lines are parallel in a log-log plot. Note that this is significantly faster than expected from the general results for Simpson’s 1/3 rule. Extra credit: For a general function f ( x ) , the composite Simpson’s 1/3 rule provides an estimate of the integral with a truncation error whose leading order term as N increases is: E = ( b a ) 5 180 N 4 f ( 4 ) with f ( 4 ) , the average value of the fourth derivative of f over the integration interval [ a b ] . For the particular function f considered here, by successive differentiation, we obtain: f ( x ) = 1 1 + x 2 f ( x ) = 2 x ( 1 + x 2 ) 2 f ′′ ( x ) = 2 ( 1 + x 2 ) 2 + 8 x 2 ( 1 + x 2 ) 3 = 6 ( 1 + x 2 ) 2 8 ( 1 + x 2 ) 3 f ′′′ ( x ) = 24 x ( 1 + x 2 ) 3 + 48 x ( 1 + x 2 ) 4 f ( 4 ) ( x ) = 24

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern