Integration
Problem Statement
- Problem: Compute
- Analytical approach:
- Find such that
- Compute
- Why numerical methods are needed:
- Some integrals cannot be computed analytically (having no closed-form antiderivative)
- Example:
- For some other functions, their antiderivatives exist but are too complicated to find analytically
- So we approximate numerically
- Some integrals cannot be computed analytically (having no closed-form antiderivative)
- Definition: Numerical quadrature = the numerical approximation of definite univariate integrals
n-point Quadrature
จุดประสงค์ร่วม
- ต้องการ
- แต่ทำไม่ได้ตรง ๆ → เลยเปลี่ยนเป็น ผลรวมเชิงเส้นของค่า ที่บางจุด
- เป้าหมาย:
- ใช้น้อยจุด (cost ต่ำ)
- แต่แม่นยำพอสมควร
General Form
- Approximate by:
where
Key Terms
- 's are called nodes or abscissas
- 's are called weights or coefficients
- Many possible rules depending on the choices of , 's, and 's
Goal and Cost
- Goal: Choose nodes and weights to get desired accuracy at a reasonable computational cost
- Main computational cost: evaluating
Rule Types
- A quadrature rule is open if and
- A quadrature rule is closed if and
Derivation of Quadrature Rules: Idea
Method 1: Integrating the Interpolant 7 - Interpolation
- Choose nodes in some fashion (e.g., equally-spaced points)
- Find the polynomial of degree that interpolates at these nodes
- Evaluate and use it as approximation of
Key Properties
- The derivations can be done on arbitrary , , and
- This results in a general formula that can be used to approximate any definite integrals
Example of Derivation of a New Quadrature Rule
Deriving the Midpoint Rule
Given:
Step 1: Let us choose
Step 2: Choose
Step 3: Find the polynomial interpolant of the one point
- The interpolant is: — Horizontal line (อันนี้คือเอาไป interpolate แล้ว)
Step 4: Calculate the quadrature rule
Result:
This is known as the ==midpoint rule==:
Method 2: Method of Undetermined Coefficients
- Choose nodes somehow first (e.g., equally-spaced points)
- Choose the weights so that computes the following integrals exactly:
- , , , ,
- The rule is
Setting up the Equations
For exactly:
For exactly:
For exactly:
General pattern for exactly:
The last equation for exactly:
System of Moment Equations
Solve the following system of moment equations:
1 & 1 & \cdots & 1 \\ x_1 & x_2 & \cdots & x_n \\ \vdots & \vdots & \ddots & \vdots \\ x_1^{n-1} & x_2^{n-1} & \cdots & x_n^{n-1} \end{bmatrix} \begin{bmatrix} w_1 \\ w_2 \\ \vdots \\ w_n \end{bmatrix} = \begin{bmatrix} b - a \\ (b^2 - a^2)/2 \\ \vdots \\ (b^n - a^n)/n \end{bmatrix}}$$ for $w_i$'s. This is known as the **method of undetermined coefficients**. >[!quote] END OF WEEK 5 ___ ## Method of Undetermined Coefficients vs Integrating-the-Interpolant ### Claim - If we use the same set of nodes, the method of undetermined coefficients gives the same weights as the previous integrating-the-interpolant idea. ### Proof - Let $w_1, \ldots, w_n$ denote the weights obtained by the method of undetermined coefficients - Let $Q_n(f) = \sum_{i=1}^n w_i f(x_i)$ denote the resulting quadrature rule obtained by the method - Let $p_{n-1}(x) = c_1 + c_2x + \ldots + c_nx^{n-1}$ denote the polynomial of degree $n-1$ that interpolates $f$ at $x_1, \ldots, x_n$ #### Proof Steps $$\int_a^b p_{n-1}(x)dx = \int_a^b (c_1 + c_2x + \cdots + c_nx^{n-1})dx$$ - This equals: $$c_1\int_a^b dx + c_2\int_a^b x dx + \cdots + c_n\int_a^b x^{n-1} dx$$ - Which becomes: $$c_1(w_1 + \cdots + w_n) + c_2(w_1x_1 + \cdots + w_nx_n) + \cdots + c_n(w_1x_1^{n-1} + \cdots + w_nx_n^{n-1})$$ - Rearranging: $$w_1(c_1 + c_2x_1 + \cdots + c_nx_1^{n-1}) + w_2(c_1 + c_2x_2 + \cdots + c_nx_2^{n-1}) + \cdots + w_n(c_1 + c_2x_n + \cdots + c_nx_n^{n-1})$$ - This simplifies to: $$w_1p_{n-1}(x_1) + w_2p_{n-1}(x_2) + \cdots + w_np_{n-1}(x_n) = w_1f(x_1) + w_2f(x_2) + \cdots + w_nf(x_n) = Q_n(f)$$ - The last line follows because $p_{n-1}$ interpolates $f$ at $x_1 \ldots x_n$, so $p_{n-1}(x_i) = f(x_i)$ - This shows that the quadrature rule obtained by the method of undetermined coefficients is exactly the integral of the interpolant ## Example 1: 2-Point Quadrature Rule ### Problem - Use the method of ==undetermined coefficients== to derive a 2-point quadrature rule - $Q_2(f) = w_1f(x_1) + w_2f(x_2)$ for the interval $[a,b]$ using $x_1 = a$, $x_2 = b$ ### Solution - The interval $[a,b]$ using $x_1 = a$, $x_2 = b$, $n = 2$ - Solve: $$\begin{bmatrix} 1 & 1 \\ a & b \end{bmatrix} \begin{bmatrix} w_1 \\ w_2 \end{bmatrix} = \begin{bmatrix} b-a \\ \frac{b^2-a^2}{2} \end{bmatrix}$$ #### First Row - $w_1 + w_2 = b - a$ - Therefore: $w_2 = b - a - w_1$ ... (1) #### Second Row - $aw_1 + bw_2 = \frac{b^2-a^2}{2}$ - Substituting (1): $aw_1 + b(b-a-w_1) = \frac{b^2-a^2}{2}$ - Expanding: $aw_1 + (b-a)b - bw_1 = \frac{b^2-a^2}{2}$ - Simplifying: $(b-a)b - (b-a)w_1 = \frac{(b-a)(b+a)}{2}$ - Dividing by $(b-a)$: $b - w_1 = \frac{a+b}{2}$ - Solving: $w_1 = b - \frac{a+b}{2} = b - \frac{b}{2} - \frac{a}{2} = \frac{b-a}{2}$ #### Final Result - $w_2 = b - a - w_1 = (b-a) - \frac{b-a}{2} = \frac{b-a}{2}$ - The quadrature rule is: $$\boxed{Q_2(f) = \frac{b-a}{2}f(a) + \frac{b-a}{2}f(b) = \frac{b-a}{2}(f(a) + f(b))}$$ - This is known as the **trapezoid rule** ## Exercise 2: 1-Point Quadrature Rule ### Problem - Derive a 1-point quadrature rule for the interval $[a,b]$ using $x_1 = a$ ### Solution - Solve: $[1][w_1] = [b-a]$ - Therefore: $w_1 = b-a$ - The rule is: $$\boxed{Q_1(f) = (b-a)f(a)}$$ ## Exercise 3: 2-Point Quadrature Rule with Different Nodes ### Problem - Derive a 2-point quadrature rule for the interval $[a,b]$ using: - $x_1 = \frac{a+b}{3}$ - $x_2 = \frac{2(a+b)}{3}$ - Assuming $a$ and $b$ have the same sign ### Solution - Solve: $$\begin{bmatrix} 1 & 1 \\ \frac{a+b}{3} & \frac{2}{3}(a+b) \end{bmatrix} \begin{bmatrix} w_1 \\ w_2 \end{bmatrix} = \begin{bmatrix} b-a \\ \frac{b^2-a^2}{2} \end{bmatrix}$$ #### From First Row - $w_1 + w_2 = b - a$ - Therefore: $w_2 = b - a - w_1$ ... (2) #### From Second Row - $\frac{1}{3}(a+b)w_1 + \frac{2}{3}(a+b)w_2 = \frac{b^2-a^2}{2}$ - Substituting (2) and simplifying: - $\frac{1}{3}(a+b)w_1 + \frac{2}{3}(a+b)(b-a-w_1) = \frac{(b-a)(a+b)}{2}$ - After algebraic manipulation: $-\frac{1}{3}w_1 = \frac{1}{2}(b-a) - \frac{2}{3}(b-a)$ - Solving: $w_1 = \frac{b-a}{2}$ #### Final Result - $w_2 = b - a - w_1 = (b-a) - \frac{b-a}{2} = \frac{b-a}{2}$ - The quadrature rule is: $${Q_2(f) = \frac{b-a}{2}f\left(\frac{a+b}{3}\right) + \frac{b-a}{2}f\left(\frac{2(a+b)}{3}\right)}$$ ___ ## Degree of a Quadrature Rule ### Definition - A quadrature rule is said to be of **degree $d$** if: - It is exact (error is zero) for every polynomial of degree $d$ or lower - It is not exact for some polynomial of degree $d+1$ - Higher-degree rules are more accurate ### Key Properties - By construction, an $n$-point interpolatory quadrature rule is of degree **at least $n-1$** - From the method of undetermined coefficients, an $n$-point quadrature rule is exact for the monomial bases $1, x, x^2, \ldots, x^{n-1}$ - Therefore, it is exact for any polynomials of degree $n-1$ >[!quote] END OF MIDTERM COVERAGE # Newton-Cotes Quadrature ## Definition **Newton-Cotes quadrature rules** are interpolatory quadrature rules with equally-spaced nodes. ### Open Newton-Cotes Rule - An **n-point open Newton-Cotes rule** has nodes: $$\boxed{x_i = a + i(b - a)/(n + 1), \quad i = 1, \ldots, n}$$ ### Closed Newton-Cotes Rule - An **n-point closed Newton-Cotes rule** has nodes: $$\boxed{x_i = a + (i - 1)(b - a)/(n - 1), \quad i = 1, \ldots, n}$$ > Think of "closed" rules as including the endpoints (like a closed interval [a,b]), while "open" rules exclude the endpoints (like an open interval (a,b)). --- ## Examples of Newton-Cotes Quadrature Rules ### 1. Midpoint Rule - **1-point open Newton-Cotes** $$\boxed{M(f) = (b - a)f\left(\frac{a + b}{2}\right)}$$ > Imagine approximating the area under a curve by a single rectangle whose height is determined by the function value at the center point. ### 2. Trapezoid Rule - **2-point closed Newton-Cotes** $$\boxed{T(f) = \frac{b - a}{2}(f(a) + f(b))}$$ > Think of connecting the two endpoints with a straight line and finding the area of the resulting trapezoid. ### 3. Simpson's Rule - **3-point closed Newton-Cotes** $$\boxed{S(f) = \frac{b - a}{6}\left(f(a) + 4f\left(\frac{a + b}{2}\right) + f(b)\right)}$$ > Simpson's rule fits a parabola through three points and finds the area under that parabola. --- ## Example 3: Approximating an Integral **Problem:** Approximate $I(f) = \int_0^1 e^{-x^2} dx$ with the three Newton-Cotes rules. **Solution:** - **Midpoint Rule:** $$M(f) = (1 - 0)e^{-(0.5)^2} \approx 0.778801$$ - **Trapezoid Rule:** $$T(f) = \frac{1}{2}(e^0 + e^{-1}) \approx 0.683940$$ - **Simpson's Rule:** $$S(f) = \frac{1}{6}\left(e^0 + 4e^{-(0.5)^2} + e^{-1}\right) \approx 0.747180$$ **True solution:** $\approx 0.746824$ **Key Observation:** The midpoint rule is more accurate than the trapezoid rule. --- ## Exercise 4 Approximate the following with the three Newton-Cotes rules: 1. $\int_{-2}^{2} (x^2 - 2x)dx$ 2. $\int_{0}^{\pi/2} \sin x \, dx$ --- ## Exercise 4.1 Solution **Problem:** $\int_{-2}^{2} (x^2 - 2x)dx$ **Function values:** - $f(-2) = (-2)^2 - 2(-2) = 8$ - $f(0) = 0^2 - 2(0) = 0$ - $f(2) = 2^2 - 2(2) = 0$ **Results:** - $M(f) = (2 - (-2))(0) = 0$ - $T(f) = \frac{2 - (-2)}{2}(8 + 0) = 16$ - $S(f) = \frac{4}{6}(8 + 4(0) + 0) = \frac{16}{3}$ **True solution:** $$\int_{-2}^{2} (x^2 - 2x)dx = \left[\frac{x^3}{3} - x^2\right]_{-2}^{2} = \frac{16}{3}$$ Again, the midpoint rule is more accurate than the trapezoid rule. --- ## Exercise 4.2 Solution **Problem:** $\int_{0}^{\pi/2} \sin x \, dx$ **Function values:** - $f(0) = \sin(0) = 0$ - $f(\pi/4) = \sin(\pi/4) = \frac{\sqrt{2}}{2}$ - $f(\pi/2) = \sin(\pi/2) = 1$ **Results:** - $M(f) = \frac{\pi}{2} \cdot \frac{\sqrt{2}}{2} = \frac{\sqrt{2}\pi}{4}$ - $T(f) = \frac{\pi/2 - 0}{2}(0 + 1) = \frac{\pi}{4}$ - $S(f) = \frac{\pi/2 - 0}{6}\left(0 + 4\frac{\sqrt{2}}{2} + 1\right) = \frac{\pi}{12}(1 + 2\sqrt{2})$ **True solution:** $$\int_{0}^{\pi/2} \sin x \, dx = -\cos x\big|_{0}^{\pi/2} = 1$$ Again, the midpoint rule is more accurate than the trapezoid rule. --- ## Degree of Precision ### Midpoint Rule is of Degree 1 **Claim:** Midpoint rule is of degree 1. > This means it integrates polynomials up to degree 1 exactly. **Proof:** - By construction, midpoint rule is exact for degree 0 (constants) - For degree 1, any polynomial has form $p(x) = cx + e$ - Exact integral: $\int_a^b (cx + e)dx = \frac{c}{2}(b^2 - a^2) + e(b - a)$ - Midpoint rule gives: $$(b - a)p\left(\frac{a + b}{2}\right) = (b - a)\left(c\frac{a + b}{2} + e\right) = \frac{c}{2}(b^2 - a^2) + e(b - a)$$ - These match! ✓ **Counterexample for degree 2:** $$\int_0^2 3x^2 dx = 8, \quad \text{but } M(f) = 2(3)(1)^2 = 6$$ So midpoint rule is NOT exact for all degree 2 polynomials. --- ### Graphical Interpretation [Image showing midpoint rule with linear function] --- ## Degrees of Other Rules **Trapezoid Rule:** Degree 1 - Same degree as midpoint rule **Simpson's Rule:** Degree 3 - Higher precision! Even though it uses only 3 points, it's exact for cubics. > Simpson's rule "punches above its weight" - it's exact for polynomials up to degree 3, even though it only uses 3 evaluation points. --- ## Accuracy Comparison ### Claim: Midpoint is Generally Twice as Accurate as Trapezoid **Proof using Taylor Series:** Let $m = \frac{a + b}{2}$ (midpoint) **Taylor expansion of $f(x)$ about $m$:** $$f(x) = f(m) + f'(m)(x - m) + \frac{f''(m)}{2}(x - m)^2 + \frac{f^{(3)}(m)}{6}(x - m)^3 + \frac{f^{(4)}(m)}{24}(x - m)^4 + \ldots$$ **Integrate both sides from $a$ to $b$:** $$\int_a^b f(x)dx = \int_a^b f(m)dx + \int_a^b f'(m)(x - m)dx + \int_a^b \frac{f''(m)}{2}(x - m)^2 dx + \ldots$$ **Key results** (odd powers integrate to zero): - $\int_a^b \frac{f^{(n)}(m)(x - m)^n}{n!} dx = 0$ for odd $n$ - $\int_a^b f(m)dx = f(m)(b - a)$ - $\int_a^b \frac{f''(m)(x - m)^2}{2!} dx = \frac{f''(m)}{24}(b - a)^3$ - $\int_a^b \frac{f^{(4)}(m)(x - m)^4}{4!} dx = \frac{f^{(4)}(m)}{1920}(b - a)^5$ **Therefore:** $$I(f) = M(f) + E(f) + F(f) + \ldots$$ where: $$\boxed{E(f) = \frac{f''(m)}{24}(b - a)^3}$$ $$\boxed{F(f) = \frac{f^{(4)}(m)}{1920}(b - a)^5}$$ These represent the first two error terms for the midpoint rule. --- ### For Trapezoid Rule Starting from Taylor expansions at $a$ and $b$, then adding them: $$f(a) + f(b) = 2f(m) + f''(m)\left(\frac{b - a}{2}\right)^2 + \frac{f^{(4)}(m)}{12}\left(\frac{b - a}{2}\right)^4 + \ldots$$ **Multiply by $(b - a)/2$:** $$T(f) = M(f) + 3E(f) + 5F(f) + \ldots$$ **Rearranging:** $$I(f) = T(f) - 2E(f) - 4F(f) + \ldots$$ **Comparison:** - Midpoint error: $E(f) + F(f) + \ldots$ - Trapezoid error: $2E(f) + 4F(f) + \ldots$ > The trapezoid rule has **twice the error** of the midpoint rule (if $(b - a)$ is not too large and $f^{(4)}$ is well-behaved). --- ### Error Estimation From $T(f) = M(f) + 3E(f) + 5F(f) + \ldots$, we can rearrange: $$\boxed{E(f) \approx \frac{T(f) - M(f)}{3}}$$ > We can **estimate the error** by comparing the trapezoid and midpoint results! **Key insights:** - Halving the interval length $(b - a)$ decreases error by factor of $\approx 1/8$ - The difference $T(f) - M(f)$ gives us error information --- ## Example 5: Error Estimation **Problem:** Estimate error of midpoint and trapezoid rules for $\int_0^1 x^2 dx$ **Calculations:** - $M(f) = (1 - 0)\left(\frac{1}{2}\right)^2 = \frac{1}{4}$ - $T(f) = \frac{1 - 0}{2}(0^2 + 1^2) = \frac{1}{2}$ **Error estimate:** $$E(f) \approx \frac{T(f) - M(f)}{3} = \frac{1/4}{3} = \frac{1}{12}$$ **Conclusion:** - Error in $M(f)$ is about $1/12$ - Error in $T(f)$ is about $1/6$ (twice as large) --- ## Simpson's Rule Derivation From the midpoint and trapezoid expansions: **Multiply midpoint by 2/3:** $$\frac{2}{3}I(f) = \frac{2}{3}M(f) + \frac{2}{3}E(f) + \frac{2}{3}F(f) + \ldots$$ **Multiply trapezoid by 1/3:** $$\frac{1}{3}I(f) = \frac{1}{3}T(f) - \frac{2}{3}E(f) - \frac{4}{3}F(f) + \ldots$$ **Add them:** $$I(f) = \left(\frac{2}{3}M(f) + \frac{1}{3}T(f)\right) - \frac{2}{3}F(f) + \ldots$$ **But:** $$\frac{2}{3}M(f) + \frac{1}{3}T(f) = \frac{2}{3}(b - a)f(m) + \frac{1}{3}\frac{b - a}{2}(f(a) + f(b)) = S(f)$$ **Therefore:** $$\boxed{I(f) = S(f) - \frac{2}{3}F(f) + \ldots}$$ This shows the dominant error term of Simpson's rule! > Simpson's rule eliminates the $E(f)$ error term, leaving only the much smaller $F(f)$ term. This is why it's so much more accurate! --- ## Gaussian Quadrature ### Motivation - Previously: Fix nodes $x_i$, then choose optimal weights $w_i$ to maximize degree. - **New idea:** Choose **both** $x_i$ and $w_i$ optimally to maximize degree! ### Result - For each $n$, there is a unique **n-point Gaussian quadrature rule** of degree $2n - 1$. - ==This is the **highest possible accuracy** for $n$ nodes!== - **Trade-off:** More difficult to derive (nonlinear system) --- ## Derivation Method Use **method of undetermined coefficients:** - Find $w_i$ and $x_i$ ($2n$ free variables) - Make rule exact for: $\int_a^b dx, \int_a^b x dx, \int_a^b x^2 dx, \ldots, \int_a^b x^{2n-1} dx$ (2n equations) - Results in a **nonlinear system** --- ## Example 6: 2-Point Gaussian Rule on `[-1, 1]` **Problem:** Derive a Gaussian quadrature rule for $n = 2$ on $[-1, 1]$, then use it to approximate $\int_{-1}^{1} e^{-x^2} dx$. **Form of rule:** $$G_2(f) = w_1 f(x_1) + w_2 f(x_2)$$ **Degree:** $2n - 1 = 2(2)-1=3$ (exact for polynomials up to degree 3) **System of equations:** 1. Exact for $\int_{-1}^{1} dx$: $$w_1 + w_2 = 2=\int_{-1}^{1} dx$$ 2. Exact for $\int_{-1}^{1} x dx$: $$w_1 x_1 + w_2 x_2 = 0=\int_{-1}^{1} x\, dx$$ 3. Exact for $\int_{-1}^{1} x^2 dx$: $$w_1 x_1^2 + w_2 x_2^2 = \frac{2}{3}=\int_{-1}^{1} x^2\, dx$$ 4. Exact for $\int_{-1}^{1} x^3 dx$: $$w_1 x_1^3 + w_2 x_2^3 = 0=\int_{-1}^{1} x^3\, dx$$ **Solution process:** >ก็หายังไงก็ได้ สุดท้ายคือให้หา $x_1,x_2$ ว่ามีค่าเป็นอะไร (ต้องรู้แต่ละค่าของ $w$ ด้วย) แล้วเอากลับไปแทนเข้า Form of rule ที่เรามีตั้งแต่ตอนแรก From equation (1): $w_2 = 2 - w_1$ From equation (2): $w_1 = \frac{-2x_2}{x_2 - x_1}$ Therefore: $w_2 = 2 - \frac{2x_2}{x_2 - x_1}$ Substituting into equation (3) and simplifying: $$3x_1 x_2 = -1$$ $$x_1 = -\frac{1}{3x_2}$$ Substituting into equation (4) and solving: $$x_2^4 = \frac{1}{9}$$ $$x_2 = \pm\frac{1}{\sqrt{3}}$$ **Choosing** $x_2 = \frac{1}{\sqrt{3}}$: - $x_1 = -\frac{1}{\sqrt{3}}$ - $w_1 = w_2 = 1$ **Final rule:** $$\boxed{G_2(f) = f\left(-\frac{1}{\sqrt{3}}\right) + f\left(\frac{1}{\sqrt{3}}\right)}$$ **Application:** $$\int_{-1}^{1} e^{-x^2} dx \approx G_2(e^{-x^2}) = e^{-1/3} + e^{-1/3} \approx 1.4331$$ ![[Figure_1.png|center|500]] >So even though the visual looks “rectangular,” the **choice of positions** x1,x2=±1/3x1,x2=±1/3 is _mathematically optimized_ — not arbitrary midpoints. --- ## Gaussian Quadrature Summary **General n-point rule on [a, b]:** Solve the nonlinear system: $$\begin{align} w_1 + w_2 + \ldots + w_n &= b - a \\ w_1 x_1 + w_2 x_2 + \ldots + w_n x_n &= \frac{b^2 - a^2}{2} \\ w_1 x_1^2 + w_2 x_2^2 + \ldots + w_n x_n^2 &= \frac{b^3 - a^3}{3} \\ &\vdots \\ w_1 x_1^{2n-1} + w_2 x_2^{2n-1} + \ldots + w_n x_n^{2n-1} &= \frac{b^{2n} - a^{2n}}{2n} \end{align}$$ **Note:** Choose specific $a$ and $b$ before solving! --- ## Notes About Gaussian Quadrature - Weights and nodes are **specific to the interval** - To integrate over a different interval, **transform** it to the standard interval - Example: $\int_0^2 x^2 dx = \int_{-1}^{1} (x + 1)^2 dx$ >แค่เลื่อนแกน ให้มันมาอยู่ในช่วง -1, 1 ถ้านึกภาพไม่ออกดู `note2.py` --- ## Improving Accuracy Two approaches: 1. **Increase number of nodes** ($n$) and use the new $n$-node rule 2. **Subdivide the interval** into multiple subintervals and apply the same rule to each > Example: To compute $\int_0^2 f(x)dx$, use Simpson's rule on $[0,1]$ and $[1,2]$ separately, then add results. See `intro-composite.py` |Concept|Effect| |---|---| |One big interval|Big approximation error (curve changes a lot inside)| |Many small intervals|Each small piece is almost linear, so the sum fits the real curve better| |Limit (n \to \infty)|Becomes the **true integral**| --- ## Composite Rules >แล้วข้างบนนั่นแหละเป็น Intro ของ Composite Rules >แทนที่จะใช้สูตรครั้งเดียวทั้งช่วง `[a,b]` เราหั่นช่วงออกเป็น k ช่วงเล็ก ๆ แล้วใช้สูตรเดิมกับแต่ละช่วง สุดท้ายรวมผลทั้งหมด ### Composite Midpoint Rule **Setup:** - Subdivide $[a, b]$ into $k$ equal subintervals - Length of each: $h = (b - a)/k$ - Points: $x_j = a + jh$, $j = 0, \ldots, k$ **Formula:** $$\boxed{M_k(f) = h \sum_{j=1}^{k} f\left(\frac{x_{j-1} + x_j}{2}\right)}$$ ### Composite Trapezoid Rule $$\boxed{T_k(f) = h\left(\frac{1}{2}f(a) + f(x_1) + \ldots + f(x_{k-1}) + \frac{1}{2}f(b)\right)}$$ --- ## Exercise 7 Solution **Problem:** Approximate $\int_0^2 x^2 dx$ using $M_3(f)$ and $T_3(f)$ **True value:** $\frac{x^3}{3}\big|_0^2 = \frac{8}{3} = 2.6667$ **Setup:** - $k = 3$ - $h = \frac{2 - 0}{3} = \frac{2}{3}$ - $x_0 = 0, x_1 = \frac{2}{3}, x_2 = \frac{4}{3}, x_3 = 2$ **Composite Midpoint:** $$M_3(f) = \frac{2}{3}\left(f\left(\frac{1}{3}\right) + f(1) + f\left(\frac{5}{3}\right)\right)$$ $$= \frac{2}{3}\left(\frac{1}{9} + 1 + \frac{25}{9}\right) = \frac{70}{27} \approx 2.5926$$ **Composite Trapezoid:** $$T_3(f) = \frac{2}{3}\left(\frac{1}{2}f(0) + f(2/3) + f(4/3) + \frac{1}{2}f(2)\right)$$ $$= \frac{2}{3}\left(0 + \frac{4}{9} + \frac{16}{9} + 2\right) = \frac{76}{27} \approx 2.8148$$ --- ## Key Takeaways - **Newton-Cotes rules** use equally-spaced nodes - **Midpoint rule** is generally more accurate than trapezoid rule - **Simpson's rule** achieves degree 3 with only 3 points - **Gaussian quadrature** achieves maximum accuracy (degree $2n-1$) for $n$ nodes - **Composite rules** subdivide the interval to improve accuracy - Error can be estimated from difference between methods