HW4_Solutions

HW4_Solutions - MAE 107 Computational Methods Summer...

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

View Full Document Right Arrow Icon
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 v v 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
>> 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
Background image of page 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:
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/11/2011 for the course MAE 107 taught by Professor Rottman during the Summer '08 term at UCSD.

Page1 / 12

HW4_Solutions - MAE 107 Computational Methods Summer...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online