3
Lab 6, Quadrature Topics Mathematics: quadrature formulae: trapezoidal and Simpson’s rule; estimation of errors; introduction to extrapolation techniques; engineering quadrature example; verifying solutions. MATLAB: writing code for solving quadrature problems; utilising functions; global and format statements; . Checkpoints indicate tasks or questions which your tutor may ask you about. Preparation Read your lecture notes. See also Chapra and Chanale’s Chapter 21, pp 601–624. In this lab you will write function files to perform a particular quadrature rule. These functions will also accept another function file (you will also write) to provide the integrand. Download the given functions trap.m and simp.m for the respective simple basic quadrature rules of trapezoid (trapezium) and Simpson and examine them. Simple quadrature To evaluate integrals you should first be satisfied that your quadrature files provide the correct answers. So we first will use four integrands we know the answers for. These four integrals are I 1 = Z 1 0 x dx I 2 = Z 1 0 x 2 dx I 3 = Z 1 0 x 3 dx I 4 = Z 1 0 x 4 dx Question 1 1. First evaluate these integrals analytically so that you know the correct answer. 2. Write separate function m-file for the integrand of each of the above integrals that can be called with the sim- ple quadrature rule trap.m and simp.m. You should use the calling function structure: function [ans]= int(x), Test your functions on each of these simple rules. Ensure your quadrature calculation gives the correct answer for I 1 . Checkpoint: Why does simp.m give the correct answer for I 2 and I 3 while trap.m does not? Explain your results from the simple trapezoid and Simpson’s rules by comparing the error formula for these methods. 3. From these results you should realise that it is necessary to use composite rules to effectively evaluate most integrals. Take a copy of the file trap.m and convert this to the composite trapezoidal rule using the calling func- tion structure: function [ans]= comp_trap(ff,a,b,n), where here n corresponds to the number of 1

lab6

Embed Size (px)

Citation preview

Page 1: lab6

Lab 6, Quadrature

Topics

• Mathematics: quadrature formulae: trapezoidal and Simpson’s rule; estimation of errors; introduction toextrapolation techniques; engineering quadrature example; verifying solutions.

• MATLAB: writing code for solving quadrature problems; utilising functions; global and format statements;

. Checkpoints indicate tasks or questions which your tutor may ask you about.

Preparation

Read your lecture notes. See also Chapra and Chanale’s Chapter 21, pp 601–624. In this lab you will writefunction files to perform a particular quadrature rule. These functions will also accept another function file (youwill also write) to provide the integrand.

Download the given functions trap.m and simp.m for the respective simple basic quadrature rules of trapezoid(trapezium) and Simpson and examine them.

Simple quadrature

To evaluate integrals you should first be satisfied that your quadrature files provide the correct answers. So wefirst will use four integrands we know the answers for. These four integrals are

I1 =

∫ 1

0

x dx

I2 =

∫ 1

0

x2 dx

I3 =

∫ 1

0

x3 dx

I4 =

∫ 1

0

x4 dx

Question 1

1. First evaluate these integrals analytically so that you know the correct answer.

2. Write separate function m-file for the integrand of each of the above integrals that can be called with the sim-ple quadrature rule trap.m and simp.m. You should use the calling function structure: function [ans]= int(x),Test your functions on each of these simple rules. Ensure your quadrature calculation gives the correctanswer for I1.

Checkpoint: Why does simp.m give the correct answer for I2 and I3 while trap.m does not?Explain your results from the simple trapezoid and Simpson’s rules by comparing the error formulafor these methods.

3. From these results you should realise that it is necessary to use composite rules to effectively evaluate mostintegrals.

Take a copy of the file trap.m and convert this to the composite trapezoidal rule using the calling func-tion structure: function [ans]= comp_trap(ff,a,b,n), where here n corresponds to the number of

1

Page 2: lab6

intervals so that h = (b − a)/n. Look at trap.m for guidance on the other variables. Test your functioncomp_trap.m on each of the above integrals for varying n.

Here we will use the brute force method of determining accuracy by using the quadrature rule for varyingn. This is produced by doubling n at each application of the rule so that the first four digits of significanceeventually do not change. This is taken as 4 significant digits of accuracy. The commands format longand format short may be useful here. Record n.

Checkpoint: Explain your results and compare with the true error.

4. Take a copy of the file simp.m and convert this to the composite Simpson rule using the function structurefunction function [ans]= comp_simp(ff,a,b,n) where here n corresponds to the number of applica-tions of Simpson’s rule, so that h = (b− a)/(2n). Test your function comp_simp.m on each of the aboveintegrals for varying n. Use the brute force method to determine 6 significant digits of accuracy. Recordn.

Checkpoint: Explain your results and compare with the true error.

5. A better way to estimate the error is to use the error estimate of the rules; as given in lectures. So for thetrapezium rule on N-intervals the error is

|I(f)− TN | = |b− a|12

h2|f ′′(c)|, where c ∈ (a, b) and h = (b− a)/N

By writing the same equation for 2N intervals (remember then h is replaced by h/2), eliminate the derivativeto show

I − T2N =T2N − TN

3so getting an estimate of the error for T2N . Or

I − TN =4

3(T2N − TN )

so getting an estimate of the error for TN . Show these formulae. This clever way of forming the error isan asymptotic error estimate.

Checkpoint: Use the asymptotic way to estimate your error in I4 by the composite trapeziumrule and compare with the true error. For what value of n can you estimate I4 by T2N to 6significant digits of accuracy by The below formula.

Even better we can use this formula to get a better estimate of the integral, Namely

I = T2N +T2N − TN

3

It can be shown that this is in fact Simpson’s rule.

Question 2

Use your composite trapezium rule to evaluate

I =

∫ 6

2

F (x) dx

where

F (x) =

{x ln(x), 2 ≤ x ≤ 4

a− x, 4 < x ≤ 6

where a = 6,

Choose n to get 4 significant digits of accuracy.

Checkpoint: What value of n did you use; explain how you found n. Compare with the exact valueto check you have the correct answer. Also try the composite Simpson rule on this. Comment onyour results.

2

Page 3: lab6

Figure 1: A uniform beam forms the semi-parabolic cantilever arch

Question 3

A uniform beam forms the semi-parabolic cantilever arch A, B as shown in figure 1. The vertical displacementof A due to the force P can be shown to be

δA =Pb3

EIC

(h

b

)where E is the elastic modulus (N/m2 or pascal), I is the second moment of area (m4), and the product EI isthe beam stiffness or the bending rigidity of the beam. The term C is

C

(h

b

)=

∫ 1

0

z2

√1 +

(2h

bz

)2

dz

Write a program that computes C(h/b

)for any given value of h/b to four decimal places. The command global

is useful to pass parameters to functions.

Checkpoint: Use the program to compute C(1.0), and C(2.0).When EI = 104Nm−2, and b = 10m, plot δ/P verses h/b for 0 < h/b < 2 i.e. deflection per unitload.

3