Docsity

Homework 4 Solutions - Computational Methods in Engineering | MAE 107, Assignments of Mechanical Engineering

Toggle sidebar
bg1
MAE 107 – Computational Methods
Summer Session II 2009
Homework # 4 – Solutions
Problem 1 – 5 points
1. Anaytical Solution
J=Z1
0
1
1+x2dx =tan−1x
1
0=tan−11−tan−10=π
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 xon [0π]using 10 and 20 segments
which we compare with the true answer Rπ
0sinxdx=2.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Homework 4 Solutions - Computational Methods in Engineering | MAE 107 and more Assignments Mechanical Engineering in PDF only on Docsity!

MAE 107 – Computational Methods

Summer Session II 2009

Homework # 4 – Solutions

Problem 1 – 5 points

  1. Anaytical Solution J =

Z (^1)

0

1 + x^2 dx = tan−^1 x

∣^1

0 =^ tan

− (^1 1) − tan− (^1 0) = π 4

  1. 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- A_i = A_i + f(a+i_indexh); end % Add up all the function evaluations at the even index points for j_index = 2:2:N- A_j = A_j + f(a+j_indexh); end % Apply the Simpson’s 1/3 rule to the sum of the parts I = (h/3)(f(a) + 4A_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.

simpson13(@(x) sin(x),0,pi,10)

ans =

simpson13(@(x) sin(x),0,pi,20)

ans =

  1. 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)’)

Then the average value of f (^4 )^ on the interval [0 1] is equal to

f (^4 )^ = 120 I 3 − 480 I 4 + 384 I 5 = 24 ( 16 I 5 − 20 I 4 + 5 I 3 ). (1)

In the previous equation, we noted In =

R (^1) 0

d x ( 1 + x^2 ) n^. Using integration by parts, we can relate^ In +^1 to^ In^ as

In =

Z (^1)

0

d x ( 1 + x^2 ) n^

[

x ( 1 + x^2 ) n

] 1

0

  • 2 n

Z (^1)

0

x^2 d x ( 1 + x^2 ) n +^1

2 n^

  • 2 n ( In − In + 1 ).

We can rearrange this equation as

2 nIn + 1 = ( 2 n − 1 ) In +

2 n^

Applying this relation to n = 3 and n = 4, we obtain:

8 I 5 = 7 I 4 +

, 6 I 4 = 5 I 3 +

Substitution in (1) leads to

f (^4 )^ = 24 ( 16 I 5 − 20 I 4 + 5 I 3 ) = 24

14 I 4 +

− 20 I 4 + 5 I 3

− 6 I 4 +

+ 5 I 3

The average f (^4 )^ is zero therefore the leading order in the truncation error of the composite Simpson’s 1/3 rule vanishes for this particular function. The next term in the truncation error varies like 1/ N^6 , which explains the behavior of the error observed above.

Problem 2 – 4 points

  1. Anaytical Solution

C =

Z (^2)

1

2 x +

x

dx =

Z (^2)

1

4 x^2 + 9 x −^2 + 12

dx =

x^3

2

1

− 9 x −^1

∣^2

1 +^12 x |

2 1 =^

  1. Romberg

(a) Using the pre-build Trapezoidal Integration code, we can determine the approximate values for N = 4 and 8 along with the relative errors:

I11 = int_trap(@(x) (2.*x+3./x).ˆ2,1,2,4)

I11 =

E11 = abs((155/6 - I11)/(155/6))

E11 =

I21 = int_trap(@(x) (2.*x+3./x).ˆ2,1,2,8)

I21 =

E21 = abs((155/6 - I21)/(155/6))

E21 =

(b) Using the Romberg Integration we can get an estimate for I 1 , 2 with the equation I 1 , 2 = 4 I^2 ,^1 − 3 I^1 ,^1 :

I12 = (4*I21-I11)/

I12 =

E12 = abs((155/6 - I12)/(155/6))

E12 =

1.04094735108113e-

This results for the relative error is 2 orders of magnitude smaller than either of the Trapazoidal estimates made above.

(c) Just using the Trapezoidal Integration method, we can determine how many intervals we would have needed to match the accuracy of the Romberg integration by simply writing a basic while loop that iterates and adds intervals until the relative error of the Romberg step is met:

% romberg_trapazoid

% Initial guess on relative error e_r = 1;

% Close all figures and clear the command line close all clc % Define the equation were marching g = @(y) 1 + y.ˆ2;

% Give a p value -> determines step size h -> determines # segments N p = 1; h = 2ˆ(-p); N = (1-0)/h;

% create blank arrays to put data in y = zeros(1,N);

% March down along each segment for step = 1:N % Use explicit euler to determine next value y(step+1) = y(step) + h*g(y(step)); end

% plot the results of the explicit euler march vs the exact soln % Exact solution xplot=linspace(0,1,101); yplot=tan(xplot); figure(1) hold on plot(h[0:N],y,’r’) plot(xplot,yplot,’b--’) title({[’Solution using explicit Euler’],[’for g(y)=1+yˆ2’]}) legend(’Explicit Euler’,’Exact’,’Location’,’Northwest’) xlabel(’X’) ylabel(’Y’)

% output the estimated value at y(1) and the true relative error disp([’The estimated value y(1) = ’,num2str(y(end)),’.’]) disp([’The true relative error, e_r = ’, num2str(abs((y(end)-yplot(end))/yplot(end))), ’.’])

With the results:

The estimated value y(1) = 1.125. The true relative error, e_r = 0.27765.

(b) We repeat the previous question with increasing values of p (therefore reducing each time the step size h = 2 − p ). The result of the error in the prediction of y ( 1 ) are presented in table 1

(^00) 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

1

Solution using explicit Euler for g(y)=1+y^2

X

Y

Explicit Euler Exact

Figure 2: Explicit Euler march of dy dx = 1 + y^2 with h = 1 /2.

p Step Size ( h ) Num of steps ( N ) True Relative Error er 1 0.5000 2 0. 2 0.2500 4 0. 3 0.1250 8 0. 4 0.0625 16 0. 5 0.0312 32 0. 6 0.0156 64 0. 7 0.0078 128 0. 8 0.0039 256 0. 9 0.0020 512 0. 10 0.0010 1024 0. 11 0.0005 2048 0. 12 0.0002 4096 0.

Table 1: Evolution of the error on y ( 1 ) when using Explicit Euler to solve the ODE and increasing number of steps N.

% output the estimated value at y(1) and the true relative error disp([’The estimated value y(1) = ’,num2str(y(end)),’.’]) disp([’The true relative error, e_r = ’, num2str(abs((y(end)-yplot(end))/yplot(end))), ’.’])

We note that the function g ( x ) is evaluated 4 times during each step!

(b) With h = 12 , we can estimate the value of y ( 1 ) with the results:

The estimated value y(1) = 1.5546. The true relative error, e_r = 0.001795.

(^00) 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

1

Examinatino of explicit euler for g(y)=1+y^2

X

Y

Explicit Euler Exact

Figure 3: Runge Kutta march of dy dx = 1 + y^2 with h = 1 /2.

(c) Repeating the integration with increasing values of p , we detail the variations of the relative error in table 2.

p Step Size ( h ) Num of steps ( N ) True Relative Error er 1 0.5000 2 0. 2 0.2500 4 0.

Table 2: Evolution of the relative error on y ( 1 ) with the number of steps N , when using the RK4 scheme.

Using only 4 steps and the RK4 method, it is possible to achieve a relative error on y ( 1 ) equal to 10−^4. To achieve such an error with RK4, 16 function evaluations were performed. To achieve the same level of accuracy with explicit Euler, it would have been necessary to use N ≥ 10 , 000 steps and therefore 10, 000 function evalu- ations. This illustrates the reduction of the error when using a higher order scheme and how, despite the higher number of function evaluations per step in RK4, the computational cost can be significantly reduced by using RK instead of Explicit Euler when a bound on the relative error is imposed.

Problem 4 – 3 points

  1. Coefficients One possible method is to solve for the coefficients of the third order polynomial p 3 ( x ) = α x^3 + β x^2 + γ x + δ by enforcing that p 3 ( 0 ) = −2, p 3 ( 1 ) = −3, p 3 ( 2 ) = −8 and p 3 ( 3 ) = −11. This results in the following linear system: (^) 

 

δ γ β α

This system can be solved using Gauss Elimination (the rows operations are indicated on the left: − Forward Elimination

( 2 ) ← ( 2 ) − ( 1 ) ( 3 ) ← ( 3 ) − ( 1 ) ( 4 ) ← ( 4 ) − ( 1 )

δ γ β α

( 3 ) ← ( 3 ) − 2 × ( 2 )

( 4 ) ← ( 4 ) − 3 × ( 2 )

δ γ β α

( 4 ) ← ( 4 ) − 3 × ( 3 )

δ γ β α

− Backward Substitution

δ γ β α

( 3 ) ← [( 3 ) − 6 × ( 4 )] / 2

δ γ β α

δ γ β α