User:Egm6341.s11.team3.Xia

= Numerical Methods =

Extra Solution
From Taylor series :

$$ e^x = 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \frac{x^4}{24} + \frac{x^5}{120} + ... $$

Then the original fuction could be expressed as:

$$ f(x) = 1 + \frac{x}{2} + \frac{x^2}{6} + \frac{x^3}{24} + \frac{x^4}{120} + ... $$

$$ \Rightarrow \lim_{x\to 0} f(x) = 1 + \frac{0}{2} + \frac{0^2}{6} + \frac{0^3}{24} + \frac{0^4}{120} + ... =1 $$

The original function is plotted from 0 to 1. For comparison, serie-form of f(x) of order 2nd and 3rd are also shown in the following plot.



Extra Solution
From Taylor series :


 * {| style="width:100%" border="0"

$$ e^x = 1 + \sum_{i=1}^n \frac{x^i}{i!} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 *  $$ \displaystyle
 * }

Then the original fuction could be expressed as:


 * {| style="width:100%" border="0"

$$ f(x) = \frac{e^{x}-1}{x} = \sum_{i=1}^n \frac{x^{i-1}}{i!} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 *  $$ \displaystyle
 * }


 * {| style="width:100%" border="0"

$$ \Rightarrow P_n(x) = \sum_{i=0}^{n} \frac{x^{i}}{(i+1)!} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 *  $$ \displaystyle (Eq. 2.12)
 * }

To find $$ R_n(x) $$, we need to solve for $$ f^{(n+1)}(x) $$ which is difficult. So, here we compute $$ R_n(x) $$ from the previous function $$ e^{x} $$. From (Eq. 2.7), we could write $$ e^{x} $$ as:


 * {| style="width:100%" border="0"

$$ e^{x} = 1+ \sum_{i=1}^{n+1} \frac{x^i}{i!} + \frac{x^{n+2}}{(n+2)!} e^\xi, \quad \xi \in [0, x] $$
 * style="width:95%" |
 * style="width:95%" |
 *  $$ \displaystyle $$
 * }


 * {| style="width:100%" border="0"

$$ \Rightarrow f(x) = \frac{e^{x}-1}{x} = \sum_{i=0}^{n} \frac{x^i}{(i+1)!} + \frac{x^{n+1}}{(n+2)!} e^\xi, \quad \xi \in [0, x] $$
 * style="width:95%" |
 * style="width:95%" |
 *  $$ \displaystyle (Eq. 2.13)

$$
 * }

Comparing (Eq. 2.12) and (Eq. 2.13) yields,


 * {| style="width:100%" border="0"

$$ R_n(x) = \frac{x^{n+1}}{(n+2)!} e^\xi, \quad \xi \in [0, x] $$
 * style="width:95%" |
 * style="width:95%" |
 *  $$ \displaystyle

$$
 * }

Problem
Given from lecture notes:


 * {| style="width:100%" border="0"

$$ p_2(x)=\sum_{i=0}^{n=2} l_{i,2}(x)f(x_i) $$ $$ 
 * style="width:95%" |
 * style="width:95%" |
 *  $$ \displaystyle (Eq. 11.1)
 * }
 * {| style="width:100%" border="0"

$$ l_{i,n}=\prod_{j=0,j \neq i}^{n} \frac{x-x_j}{x_i-x_j} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 *  $$ \displaystyle (Eq. 11.2)
 * }

Derive: Simple simpson's rule.

Solution
From definition:

<span id="(1)">
 * {| style="width:100%" border="0"

$$ I_2(f) = \int_{a}^{b} f_2(x)dx = \sum_{i=0}^{2} \int_{a}^{b} l_{i,2}(x)dx f(x_i) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 11.3)
 * }

With

<span id="(1)">
 * {| style="width:100%" border="0"

$$ x_0=a;x_1=\frac {a+b}{2};x_2=b. $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

from (Eq. 11.2), we can obtain:

<span id="(1)">
 * {| style="width:100%" border="0"

$$ l_{0,2}(x) = \frac {(x-x_1)(x-x_2)}{(x_0-x_1)(x_0-x_2)} = \frac {2}{(a-b)^2} [x^2-\frac{a+3b}{2} x +\frac{(a+b)b}{2}] $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 11.4)
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ l_{1,2}(x) = \frac {(x-x_0)(x-x_2)}{(x_1-x_0)(x_1-x_2)} = -\frac {4}{(a-b)^2} [x^2-(a+b) x +ab] $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 11.5)
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ l_{2,2}(x) = \frac {(x-x_0)(x-x_1)}{(x_2-x_0)(x_2-x_1)} = \frac {2}{(b-a)^2} [x^2-\frac{3a+b}{2} x +\frac{(a+b)a}{2}] $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 11.6)
 * }

Integrate (Eq. 11.4)(Eq. 11.5)(Eq. 11.6), we can solve

<span id="(1)">
 * {| style="width:100%" border="0"

$$ \int_{a}^{b}l_{0,2}(x) = \frac {2}{(b-a)^2} [-\frac{1}{12} (a-b)^3] = \frac{1}{6}(b-a) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 11.7)
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ \int_{a}^{b}l_{1,2}(x) = \frac {4}{(a-b)^2} [-\frac{1}{6} (a-b)^3] = \frac{4}{6}(b-a) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 11.8)
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ \int_{a}^{b}l_{2,2}(x) = \frac {2}{(b-a)^2} [-\frac{1}{12} (a-b)^3] = \frac{1}{6}(b-a) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 11.9)
 * }

With

<span id="(1)">
 * {| style="width:100%" border="0"

$$ h=\frac {b-a}{2} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

Plug (Eq. 11.7)(Eq. 11.8)(Eq. 11.9) into (Eq. 11.3), yields

<span id="(1)">
 * {| style="width:100%" border="0"

$$ I_2(f) = \int_{a}^{b} l_{0,2}(x)dx f(x_0) + \int_{a}^{b} l_{1,2}(x)dx f(x_1) + \int_{a}^{b} l_{2,2}(x)dx f(x_2) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 11.10)
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ \Rightarrow I_2(f) = \frac {h}{3} (f(x_0) + 4f(x_1) + f(x_2)) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

Which is right the simple simpson's rule.

Problem
Given:

<span id="(1)">
 * {| style="width:100%" border="0"

$$ f(x)=\frac {e^x-1}{x} on [-1,1], x_0=-1,x_n=1 $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.1)
 * }

Find: 1) Construct $$ f_n(x) $$ by lagrange interpolation functions for $$ n=1,2,4,8,16 $$ and then plot $$ f(x) $$ and $$ f_n(x) $$. 2) Compute weight functions for $$ n=1,2,4,8,16 $$ and then form tables. 3) Compute $$ I_n(x) $$ for $$ n=1,2,4,8 $$ and compare to $$ I $$ that is solved using WolframAlpha with more digits. 4) For $$ n=5 $$, plot $$ l_0,l_1,l_2 $$ and then predict the shapes of $$ l_3,l_4,l_5 $$ based on those.

Solution
1) According to the problem, we construct the fuctions from following equations:

<span id="(1)">
 * {| style="width:100%" border="0"

$$ f_n(x)=p_n(x)=\sum_{i=0}^{n} l_{i,n}(x)f(x_i) $$ $$ <span id="(1)">
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.2)
 * }
 * {| style="width:100%" border="0"

$$ l_{i,n}=\prod_{j=0,j \neq i}^{n} \frac{x-x_j}{x_i-x_j} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.3)
 * }

For n=1,

<span id="(1)">
 * {| style="width:100%" border="0"

$$ x_0=-1,x_1=1 $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ l_{0,1}(x) = \frac {x-x_1}{x_0-x_1} = -\frac{1}{2}(x-1) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.4)
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ l_{1,1}(x) = \frac {x-x_0}{x_1-x_0} = \frac{1}{2}(x+1) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.5)
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ f(x_0) = \frac {e^{x_0}-1}{x_0} = 1-e^{-1} \displaystyle $$ $$ f(x_1) = \frac {e^{x_1}-1}{x_1} = e-1 $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ \Rightarrow f_1(x) = l_{0,1}(x)f(x_0) + l_{1,1}(x)f(x_1) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.6)
 * }

For n=2,

<span id="(1)">
 * {| style="width:100%" border="0"

$$ x_0=-1,x_1=0,x_2=1 $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ l_{0,2}(x) = \frac {(x-x_1)(x-x_2)}{(x_0-x_1)(x_0-x_2)} = \frac{1}{2}x(x-1) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.7)
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ l_{1,2}(x) = \frac {(x-x_0)(x-x_2)}{(x_1-x_0)(x_1-x_2)} = -(x+1)(x-1) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.8)
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ l_{1,2}(x) = \frac {(x-x_0)(x-x_1)}{(x_2-x_0)(x_2-x_1)} = \frac{1}{2}(x+1)x $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.9)
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ f(x_0) = \frac {e^{x_0}-1}{x_0} = 1-e^{-1} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ f(x_1) = \frac {e^{x_1}-1}{x_1} = 1 $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ f(x_2) = \frac {e^{x_2}-1}{x_2} = e-1 $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ \Rightarrow f_2(x) = l_{0,2}(x)f(x_0) + l_{1,2}(x)f(x_1) +  l_{2,2}(x)f(x_2)$$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.10)
 * }

For n=4,8,16, we use matlab for plotting with (Eq. 12.2) and (Eq. 12.3). The code is attached below:

The plots are shown as following:



2) For n=1, integrate previous (Eq. 12.4) and (Eq. 12.5) from [-1,1], we get,

<span id="(1)">
 * {| style="width:100%" border="0"

$$ w_{0,1} = \int_{-1}^{1} l_{0,1}(x)dx = 1 $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ w_{1,1} = \int_{-1}^{1} l_{1,1}(x)dx = 1 $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

For n=2, integrate previous (Eq. 12.7), (Eq. 12.8) and (Eq. 12.9) from [-1,1], we get,

<span id="(1)">
 * {| style="width:100%" border="0"

$$ w_{0,2} = \int_{-1}^{1} l_{0,2}(x)dx = \frac{1}{3} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ w_{1,2} = \int_{-1}^{1} l_{1,2}(x)dx = \frac{4}{3} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ w_{2,2} = \int_{-1}^{1} l_{2,2}(x)dx = \frac{1}{3} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

For n=4,8,16, by apply LIET, we obtain the linear system equations:

<span id="(1)">
 * {| style="width:100%" border="0"

$$ \sum_{i=0}^{n}w_{i,n}(x_i)^j = \frac{b^{j+1}-a^{j+1}}{j+1} $$ when $$ j=0,1,2,...,n $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.11)
 * }

Solving (Eq. 12.11) for n=4,8,16 by matlab and the code is attached below:

The results are shown in the following tables:

3) Integrate using Newton-Cotes Method, The integration could be expressed as:

<span id="(1)">
 * {| style="width:100%" border="0"

$$ I_n(f) = \int_{a}^{b} f_n(x)dx = \sum_{i=0}^{n} w_{i,n}f(x_i) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.12)
 * }

With previously calculated value of weight functions, The results of the integrations are shown in the following table and compared with that obtained from WolframAlpha.

4) For n=5, the lagrange functions are computed from (Eq. 12.3) using matlab, the code is attached below:

The first three fuctions are plotted as follows:



Now, we will analyze the shapes of the lagrange functions. First it is obvious to identify:

<span id="(1)">
 * {| style="width:100%" border="0"

$$ x_i = -x_{n-i} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

From (Eq. 12.3), we could derive

<span id="(1)">
 * {| style="width:100%" border="0"

$$ l_{n-i,n}(x) = \prod_{j=0,j \neq n-i}^{n} \frac{x-x_j}{x_{n-i}-x_j} = \prod_{j=0,j \neq n-i}^{n} \frac{x+x_{n-j}}{x_{n-i}+x_{n-j}} $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle
 * }

<span id="(1)">
 * {| style="width:100%" border="0"

$$ = \prod_{j=0,n-j \neq i}^{n} \frac{-x-x_{n-j}}{x_{i}-x_{n-j}} = \prod_{k=0,k \neq i}^{n} \frac{-x-x_{k}}{x_{i}-x_{k}} = l_{i,n}(-x) $$ $$
 * style="width:95%" |
 * style="width:95%" |
 * <p style="text-align:right"> $$ \displaystyle (Eq. 12.13)
 * }

So from (Eq. 12.13), we can predict that $$ l_3 $$ and $$ l_2 $$, $$ l_4 $$ and $$ l_1 $$, $$ l_5 $$ and $$ l_0 $$ are axis-symmetric function pairs (With respect to y axis). We further plot $$ l_3 $$, $$ l_4 $$, $$ l_5 $$ in the following figure and the prediction is verified.



Plots
=References=