# University of Florida/Egm6341/s10.Team2/HW4

## Problem 1: Development of higher order Corrected Trapezoidal rules and application

#### Statement

Develop higher-order corrected trapezoidal rules for ${\displaystyle CT_{1}(n)\quad CT_{2}(n)\quad CT_{3}(n)}$

Then use these new corrected trapezoidal rules to find:

${\displaystyle I=\int _{0}^{1}{\frac {\exp ^{x}-1}{x}}dx}$

until the error is of order of ${\displaystyle 10^{-6}\quad }$ using ${\displaystyle \;n=2,4,8,16,32...}$

#### Solution

Higher Order corrected trapezoidal rules are of the form:

${\displaystyle \;CT_{k}(n)=CT_{k-1}(n)+a_{k}h^{2*k}}$

where the a coefficient is defined as follows:

${\displaystyle a_{i}=d_{i}\left[f^{(2i-1)}(b)-f^{(2i-1)}(a)\right]}$

and ${\displaystyle di={\frac {-B_{2i}}{(2i)!}}}$

B are Bernoulli numbers. The first values for ${\displaystyle d_{i}\quad }$ are:

${\displaystyle \;d_{1}={\frac {-1}{12}}\qquad d_{1}={\frac {1}{720}}\qquad d_{3}={\frac {-1}{30240}}}$

These corrected trapezoidal rules define the integral as follows:

${\displaystyle \;I=CT_{k}(n)+\Theta (h^{(2*(k+1)})}$

This means that higher order corrected trapezoidal rules can be developed as long as the function is continuous and its higher order derivatives can be calculated.

The first three corrected trapezoidal rules are found as follows:

${\displaystyle \;CT_{1}(n)=CT_{0}(n)+a_{1}h^{2}}$

${\displaystyle \;CT_{2}(n)=CT_{1}(n)+a_{2}h^{4}}$

${\displaystyle \;CT_{3}(n)=CT_{2}(n)+a_{2}h^{6}}$

Using matlab to calculate the integrals, yields the results as follows:

 Integral Values using Corrected Trapezoidal Rules n Ct1 Ct2 Ct3 Error Ct1 Error Ct2 Error Ct3 2 1.317875061148223 1.317908476816203 1.317908314863727 2.709080863771973e-005 6.324859342043965e-006 6.162906866391538e-006 4 1.317900452799411 1.317902541278660 1.317902538748152 1.699157449630562e-006 3.893217990214026e-007 3.867912916621918e-007

It can be noticed that the corrected trapezoidal rules yield a solution to the integral much faster than the uncorrected Composite Trapezoidal Rule.

#### Matlab Code Used for Problem

MATLAB Code: Finding the Corrected Trapezoidal Rules

function [Ct1, Ct2, Ct3, Ect1, Ect2, Ect3] = cortrap (a,b,n)
tic
h=(b-a)/n;
x=linspace(a,b,n+1);

%declare the Function and its derivatives
F=(exp(x) - 1)./x;
F(1)=1;
F1=(exp(x)./x - (exp(x) - 1)./x.^2);
F1(1)=0.5;
F2=(exp(x)./x - (2*exp(x))./x.^2 + (2*(exp(x) - 1))./x.^3);
F2(1)=(1/3);
F3=(exp(x)./x - (3*exp(x))./x.^2 + (6*exp(x))./x.^3 - (6*(exp(x) - 1))./x.^4);
F3(1)=.25;
%singularity values were inserted

%calculating a1, a2, and a3 coefficients
r=n+1;  %finds out the last term of the array
a1=(-1/12)*(F1(r)-F1(1)); %Bernoulli number d1=-1/12
a2=(1/720)*(F2(r)-F2(1)); %Bernoulli number d2=1/720
a3=(-1/30240)*(F3(r)-F3(1)); %Bernoulli number d3=-1/30240

%Calculating Ct0 using composite trapezoidal rule
Ct0=ctrapz(n);
Ct1=Ct0 + (a1*(h^2));
Ct2=Ct1 + (a2*(h^4));
Ct3=Ct2 + (a3*(h^6));

Ect1=abs(I-Ct1);
Ect2=abs(I-Ct2);
Ect3=abs(I-Ct3);
toc


MATLAB Code: Calculating the Values of the function

function F= valus(x)
F=(exp(x)-1)./x;


MATLAB Code: Calculating the Composite Trapezoidal Rule

function I=ctrapz(n)
i=0;
Itot=0;
It=0;
It2=0;
h=0;
while i<=n
if i==0
Itot1=1;
else if i<n
h=1/n;
It(i)=2*valus(h*i);
else
It2=valus(1);
end
end
Itot=Itot1+sum(It)+It2;
i=i+1;
end

I=Itot/(2*n);


solved by--- Egm6341.s10.Team2.GV 21:56, 3 March 2010 (UTC)

checked by---Egm6341.s10.team2.patodon 20:53, 3 March 2010 (UTC)

## Problem 2: Higher order error analysis of Trapezoidal rule for periodic functions: For which n does ${\displaystyle \;E_{n}^{1}\simeq 0}$

#### Statement

To prove that the value of the trapezoidal rule error: ${\displaystyle \;E_{n}^{1}\simeq 0}$ for a periodic function and find the effect of ${\displaystyle \;n}$(number of intervals or panels in trapezoidal rule) on the convergence of the function.

#### Solution

Let ${\displaystyle f\left(x\right)}$ be a periodic function in the interval ${\displaystyle \left[a,b\right]}$

We know that for a periodic function with periodicity of ${\displaystyle \;b-a,}$ ${\displaystyle \;f\left(a\right)=f\left(a+\left(b-a\right)\right)=f\left(b\right)}$

Differentiating ${\displaystyle \;f\left(x\right)}$ w.r.t ${\displaystyle \;x}$ :

${\displaystyle f'\left(a\right)=f'\left(a+\left(b-a\right)\right)=f'\left(b\right)}$

similarly,

${\displaystyle f^{k}\left(a\right)=f^{k}\left(a+\left(b-a\right)\right)=f^{k}\left(b\right)}$

Therefore the coefficients of the expansion of ${\displaystyle \;E_{n}^{1}}$ i.e. ${\displaystyle \;a_{1},a_{2},a_{3},a_{4}}$ becomes zero due the following reason:

We know that:

${\displaystyle a_{i}=d_{i}\times \left[f^{\left(2i-1\right)}\left(b\right)-f^{\left(2i-1\right)}\left(a\right)\right]}$ where ${\displaystyle \;d_{i}={\frac {-B_{2i}}{\left(2i\right)!}}}$

${\displaystyle f^{\left(2i-1\right)}\left(b\right)\simeq f^{\left(2i-1\right)}\left(a\right)}$ when periodicity of the function is ${\displaystyle \;b-a}$

Therefore, in the case of periodic function ${\displaystyle f\left(x\right)}$ with periodicity ${\displaystyle \;b-a}$, ${\displaystyle \;a_{i}\simeq 0\;\forall \;i=1,2,3...}$

Hence the error expressed as:

${\displaystyle \;E_{n}^{1}=\sum _{i=1}^{\infty }a_{i}h^{2i}\simeq 0}$ as ${\displaystyle \;a_{i}\simeq 0\;\forall \;i=1,2,3...}$

The same result is tested by using a Matlab program for trapezoidal rule and it proves to be true that the function converges rapidly for periodic functions using trapezoidal rule as the number of intervals 'n' increases and thus the error term ${\displaystyle \;E_{n}^{1}}$ tends to zero.

%As illustrated below in the Matlab code for a periodic function f=sin(x) between the periodicity (b-a)=(2*pi-0), the Error 'E'%
%tends to zero rapidly with the increase in the number of panels-'n'. In the below code the 'n' value varies from 1 through 10.%

%Error of trapezoidal rule as even power of 'h'%
tic;
clc;
clear;
syms x;
f=sin(x);
a=0;
b=(2*pi);
d=[-1/12;1/720;-1/30240];
for n=1:10
h=(b-a)/n;
E1=0;
for i=1:3
A(i)=d(i).*(subs(diff(f,2i-1),x,b)-subs(diff(f,2i-1),x,a));
E1=E1+A(i)*(h^(2*i));
end
E(n)=E1;
end
toc;


checked by---Egm6341.s10.team2.patodon 20:55, 3 March 2010 (UTC)

## Problem 3: Discussions of pros and cons of various quadrature methods

#### Statement

Discuss the pros and cons of the following quadrature methods:

1. Taylor Series
2. Composite Trapezoidal rule
3. Composite Simpson's rule
4. Romberg table (including Richardson's extrapolation)
5. Corrected Trapezoidal rule

#### Solution

Taylor Series:
Pros:

1. Powerful tool that helps to approximate any type of simple or complex functions into a polynomial form ${\displaystyle \;P_{n}(x)}$ with an error in the form of remainder ${\displaystyle \;R_{n+1}(x)}$

Cons:

1. The function ${\displaystyle \;f(x)}$ should be differentiable in the given limits 'a' and 'b' in which it is approximated as a polynomial.
2. The product of two sub-functions in a given function ${\displaystyle \;f(x)}$ like for example ${\displaystyle e^{x}.\cos \left(x\right)}$ would make it more difficult and more costly (in terms of computation) to evaluate successive derivatives.

Composite Trapezoidal rule:
Pros:

1. Very intuitive and easy to use
2. The numerical integral results obtained for periodic functions like trigonometric functions (sinx, cosx, tanx etc) is very accurate owing to the fact that the error is dependent on the difference between the odd derivatives of the limiting points - 'a' and 'b'.
3. It yields better accuracy than simple trapezoidal rule in calculating the numerical integral of a function.

Pros:

1. Needs many subintervals to converge and the convergence rate is low for non-periodic functions.
2. Runge Phenomena.

Composite Simpson's rule:
Pros:

1. It is still intuitive and easy to use
2. The convergence rate is OK
3. Exact for cubic polynomials

Cons:

1. This rule can be applied to only even number of finite intervals between the limits- 'a' and 'b'. So the value of 'n' in Simpson's rule is always an even number. i.e. n=2i; where (i=1,2,3...)
2. Runge Phenomena

Romberg table (including Richardson's extrapolation):
Pros:

1. The successive computation of ${\displaystyle \;I(2n)}$ is cheaper and faster when ${\displaystyle \;I(n)}$ is already computed.
2. The accuracy of the numerical integration results obtained are better than other methods. The higher the power of 'h' term in the trapezoidal error, the better the accuracy.

Cons:

1. The success of Romberg integration is only justified if the integrand-'f' satisfies the hypotheses of the Euler–Maclaurin Theorem.For example, as illustrated in the An Introduction to Numerical Analysis by Suli and Mayers Text Pg.218 The function ${\displaystyle \;x\to x^{\frac {1}{3}}}$ is not differentiable at ${\displaystyle \;x=0}$, so the required conditions are not satisfied for any extrapolation.

checked by --- --Egm6341.s10.Team2.GV 22:00, 3 March 2010 (UTC)

## Problem 4: Transformation of variables in the error expression of HO trapezoidal rule

#### Statement

Given the expression

 ${\displaystyle \displaystyle {\boldsymbol {E_{n}^{1}=\sum _{k=0}^{n-1}[\int _{x_{k}}^{x_{k+1}}f(x)dx-{\frac {h}{2}}[f(x_{k})+f(x_{k+1})]]}}}$ (1)

where ${\displaystyle {\boldsymbol {x_{k}=a+kh}}}$ and ${\displaystyle {\boldsymbol {h={\frac {(b-a)}{n}}}}}$,

Show that by transformation of variables we get

 ${\displaystyle \displaystyle {\boldsymbol {E_{n}^{1}={\frac {h}{2}}\sum _{k=0}^{n-1}[\int _{-1}^{1}g_{k}(t)dt-[g_{k}(-1)+g_{k}({+1})]]}}}$ (2)

where ${\displaystyle {\boldsymbol {g_{k}(t)=f(x(t));x\epsilon [x_{k},x_{k+1}]}}}$

#### Solution

Given the expression

 ${\displaystyle \displaystyle {\boldsymbol {E_{n}^{1}=\sum _{k=0}^{n-1}[\int _{x_{k}}^{x_{k+1}}f(x)dx-{\frac {h}{2}}[f(x_{k})+f(x_{k+1})]]}}}$ (1)

we use the transformation ${\displaystyle \displaystyle {x_{k}=a+kh}}$ to transform the interval from ${\displaystyle \displaystyle {[x_{k},x_{k+1}]}}$ to ${\displaystyle \displaystyle {[-1,1]}}$ by introducing ${\displaystyle \displaystyle {[x_{k},x_{k+1}]}}$.We get

${\displaystyle \displaystyle {x(-1)=x_{k}}}$

${\displaystyle x(0)={\frac {x_{k}+x_{k+1}}{2}}}$

${\displaystyle \displaystyle {x(1)=x_{k+1}}}$

substituting in (1) we get

 ${\displaystyle \displaystyle E_{n}^{1}=\sum _{k=0}^{n-1}[\int _{-1}^{+1}f(x(t))dt-{\frac {h}{2}}[f(x(-1))+f(x(+1)]]}$
 ${\displaystyle \displaystyle E_{n}^{1}=\sum _{k=0}^{n-1}[\int _{-1}^{+1}f(t{\frac {h}{2}}+0)dt-{\frac {h}{2}}[f(x(-1))+f(x(+1)]]}$
 ${\displaystyle \displaystyle E_{n}^{1}=\sum _{k=0}^{n-1}[\int _{-1}^{+1}f(t{\frac {h}{2}}+0)dt-{\frac {h}{2}}[f(x(-1))+f(x(+1)]]}$

taking ${\displaystyle \displaystyle {\frac {h}{2}}}$ out we get

 ${\displaystyle \displaystyle {\boldsymbol {E_{n}^{1}={\frac {h}{2}}\sum _{k=0}^{n-1}[\int _{-1}^{1}g_{k}(t)dt-[g_{k}(-1)+g_{k}({+1})]]}}}$ (2)

solved by---Egm6341.s10.team2.niki 17:46, 27 February 2010 (UTC)

## Problem 5: Integration by parts

#### Statement

Show the following relation via integration by parts

${\displaystyle \int _{-1}^{1}(-t)\cdot g^{(1)}(t)=\int _{-1}^{1}g(t)dt-\left[g(-1)+g(1)\right]}$

#### Solution

Integration by Parts

{\displaystyle {\begin{aligned}\int _{-1}^{1}g(t)dt&=\int _{-1}^{1}g(t)\cdot (1)dt\\&=\left[g(t)\cdot (t)\right]_{-1}^{1}-\int _{-1}^{1}t\cdot g^{(1)}(t)\\&=\left[g(-1)+g(1)\right]+\int _{-1}^{1}(-t)\cdot g^{(1)}(t)\\\end{aligned}}}

Rearrangement yields

${\displaystyle \int _{-1}^{1}(-t)\cdot g^{(1)}(t)=\int _{-1}^{1}g(t)dt-\left[g(-1)+g(1)\right]}$

solved by---Egm6341.s10.team2.lee 01:45, 2 March 2010 (UTC)

checked by---Egm6341.s10.team2.niki 05:23, 2 March 2010 (UTC)

checked by---Egm6341.s10.team2.patodon 20:48, 3 March 2010 (UTC)

## Problem 6: Derivative of ${\displaystyle g_{k}(t)}$ as it relates to the proof to the theorem of higher order trapezoidal rules

#### Statement

The following is defined:

${\displaystyle g_{k}(t)=f(x(t))}$

where,

${\displaystyle x(t)=t{\frac {h}{2}}+{\frac {x_{k}+x_{k+1}}{2}}}$

Proof the following:

${\displaystyle g_{k}^{i}=\left({\frac {h}{2}}\right)^{i}f^{i}(x(t))}$

#### Solution

The first step is to state the chain rule for a composite function which is the case in this situation,

${\displaystyle Y=F(G(x))}$

${\displaystyle {\frac {dY}{dx}}=F^{'}(G(x))G^{'}(x)}$

The first derivative is found as follows:

${\displaystyle g_{k}(t)=f(x(t))}$

${\displaystyle g_{k}^{'}(t)=f^{'}(x(t))x^{'}(t)}$

${\displaystyle x^{'}(t)={\frac {h}{2}}}$

By this the first derivative is defined as:

${\displaystyle g_{k}^{(1)}(t)={\frac {h}{2}}f^{'}(x(t))}$

Using the chain rule again to find the second derivative as follows:

${\displaystyle g_{k}^{(2)}(t)={\frac {h}{2}}\left[{\frac {d}{dt}}f^{1}(x(t))\right]}$

${\displaystyle g_{k}^{(2)}(t)=\left({\frac {h}{2}}\right)^{2}f^{(2)}(x(t))}$

Apply the chain rule once more to find the third derivative as follows:

${\displaystyle g_{k}^{(3)}(t)=\left({\frac {h}{2}}\right)^{2}{\frac {d}{dt}}f^{(2)}(x(t))}$

${\displaystyle g_{k}^{(3)}(t)=\left({\frac {h}{2}}\right)^{3}f^{(3)}(x(t))}$

One notices that the value of ${\displaystyle {\frac {h}{2}}}$ comes out of the derivative everytime it is applied.

One is able to then conclude a general expression for the derivative of the function as follows:

${\displaystyle g_{k}^{(i)}(t)=\left({\frac {h}{2}}\right)^{i}f^{(i)}(x(t))}$

solved by---Egm6341.s10.Team2.GV 05:03, 27 February 2010 (UTC)

checked by---Egm6341.s10.team2.patodon 21:10, 3 March 2010 (UTC)

## Problem 7: Verifying the Error for Higher Order Trapezoidal Rule

#### Statement

Show the following relation via integration by parts

${\displaystyle \int _{-1}^{1}g^{(1)}(t)\cdot P_{1}(t)dt=\left[P_{2(t)}\cdot g^{(1)}(t)\right]_{-1}^{1}-\int _{-1}^{1}P_{2}(t)\cdot g^{(2)}(t)dt}$

#### Solution

Integration by Parts

${\displaystyle \int _{-1}^{1}g^{(1)}(t)\cdot P_{1}(t)dt=\left[P_{2(t)}\cdot g^{(1)}(t)\right]_{-1}^{1}-\int _{-1}^{1}P_{2}(t)\cdot g^{(2)}(t)dt}$

${\displaystyle {\mbox{where,}}\qquad P_{1}(t)=-t\qquad {\mbox{and}}\qquad P_{2}(t)=\int P_{1}(t)dt=-{\frac {t^{2}}{2}}+c}$

solved by---Egm6341.s10.team2.patodon 20:59, 3 March 2010 (UTC)

checked by---Egm6341.s10.team2.lee 02:15, 2 March 2010 (UTC)

## Problem 8: Installation of chebfun program

#### Statement

Install Chebfun program (Tefethen et al. 2004)

#### Solution

Status - Successful

solved by---

checked by---Egm6341.s10.team2.patodon 21:27, 3 March 2010 (UTC)

## Problem 9: Comparison of codes

#### Statement

For the integral ${\displaystyle \displaystyle {I=\int _{0}^{1}{\frac {e^{x}-1}{x}}dx}}$

1. Optimise the code for Romberg table
2. Compare second column of Romberg table with the results of Simpson's rule and derive any relationship.
3. Compute ${\displaystyle \displaystyle {I_{n}}}$ to the order of ${\displaystyle \displaystyle {10^{-10}}}$ and find the computational time using
• Code for Composite Trapezoidal Rule
• Code for Composite Simpson's Rule
• Code for Romberg table
• Chebfun "sum" command
• Built-in MATLAB function "trapz"
• Built-in MATLAB function "clencurt"

#### Solution

Exact value of the Integral from Mathematica is calculated by teh command "Integrate[((exp(x)-1)/x),+{x,+0,+1}]" the value is

1.3179021514544038948600088442492318379749012457927839928404611969976461077561394826119536468343922075 [[((exp(x)-1)/x),+{x,+0,+1}]]

##### Part1:Corrected code for Romberg table

MATLAB Code: For Efficient Production of the Romberg Table

function [table] = Rombergg(a,b,n)
tic
table=zeros(6,6);

%Generate 1st column of Romberg Table using built in composite trapezoidal
%function.  Notice it calls the function 'Valus' which obtains the values
%for the function.  The function is also corrected at initial point 0
for j=1:6
r=n;
x=linspace(a,b,r+1);
FX=valus(x);
FX(1)=1;  %because of 0 start point
table(j,1)=trapz(x,FX);
n=2*r;
end

%Generate 2nd column of Romberg Table
for j=1:5
table(j,2)= ([(2^2)*table(j+1,1)] - [table(j,1)])/(2^2 - 1);
end

%Generate 3rd column of Romberg Table
for j=1:4
table(j,3) = ([(2^(2*2))*table(j+1,2)] - table(j,2))/(2^(2*2)-1);
end

%Generate 4th column of Romberg Table
for j=1:3
table(j,4) = ([(2^(2*3))*table(j+1,3)] - table(j,3))/(2^(2*3)-1);
end

%Generate 5th Column of Romberg Table
for j=1:2
table(j,5) = ([(2^(2*4))*table(j+1,4)] - table(j,4))/(2^(2*4)-1);
end

%Generate 6th column of Romberg Table
table(1,6) = ([(2^(2*5))*table(2,5)] - table(1,5))/(2^(2*5)-1);

toc


##### Part2:Comaprison of Simpson's rule and Romberg table

The below table shows the values for the given integral computed using Composite Simpsons rule and the second column of the Romberg table for the same. By comparing the values of the Integral (or of the corresponding errors)) for any n, we see that the value from the composite Simpson's rule is the same as the value for n/2 (previous row) of ${\displaystyle \displaystyle {T_{1}(n)}}$.

 Comparison of Integral Values Number of Terms ${\displaystyle {\boldsymbol {n}}}$ Values from${\displaystyle {\boldsymbol {Comp.Simpson's}}}$ Values from Romberg Table${\displaystyle {\boldsymbol {T_{1}(n)}}}$ Error${\displaystyle E_{Simpson's}}$ Error${\displaystyle E_{Romberg}}$ 2 1.3180086656766800 1.3179089166831400 1.06514E-04 6.76523E-06 4 1.3179089166831400 1.3179025760028100 6.76523E-06 4.24548E-07 8 1.3179025760028100 1.3179021780157100 4.24548E-07 2.65613E-08 16 1.3179021780157100 1.3179021531149100 2.65613E-08 1.66051E-09 32 1.3179021531149100 1.3179021515581900 1.66051E-09 1.03790E-10 64 1.3179021515581900 - 1.03790E-10 -

For e.g: Denoting Simpson's as ${\displaystyle \displaystyle {S(n)}}$ we see that

 ${\displaystyle \displaystyle \displaystyle {S(16)=T_{1}(8)}}$
 ${\displaystyle \displaystyle \displaystyle {S(32)=T_{1}(16)}}$
 ${\displaystyle \displaystyle \displaystyle {S(64)=T_{1}(32)}}$

etc. In general we can write

 ${\displaystyle \displaystyle \displaystyle {S(2^{i})=T_{1}(2^{i-1})}}$

wkt

 ${\displaystyle \displaystyle T_{1}(2^{i})={\frac {4T(2^{i})-T(2^{i-1})}{3}}}$

 ${\displaystyle \displaystyle \therefore S(2^{i})={\frac {4T(2^{i})-T(2^{i-1})}{3}}}$ (1)
##### Part3:Codes

Using Code for Composite trapezoidal rule

    I =1.31790215145440389486;        %Exact value of the integral
n = 1;
clc
format long;
tic                               %clock begins
while(true)
In = ctrapz(n);               %calls ctrapz function which is the code used previously in the homework
if(abs((I - In)) < (10^(-10)))%check for error
break;
end
n = 2*n;
end
toc%clock stops
output = In
number = n                        %number of terms used


Gives n= 32768 ,In =1.31790215149321 and time 4.973 sec

Using Code for Composite Simpson's Rule

   I = 1.31790215145;                 %Exact value of the integral
n = 1;
clc
format long;
tic%time starts
while(true)
In = simpb(0,1,n);              %calls function simpb which is the code developed
if(abs((I - In)) < (10^(-10)))  %check for order of error
break;
end
n = 2*n;
end
toc%clock stops
output = In                        % integral value calculated
number = n                         %gives number of terms


Gives n= 128 ,In= 1.31790215146089 and time=0.017sec

Using code for Romberg table

function [table] = Rombergg(a,b,n)
tic
table=zeros(6,6);

%Generate 1st column of Romberg Table using built in composite trapezoidal
%function.  Notice it calls the function 'Valus' which obtains the values
%for the function.  The function is also corrected at initial point 0
for j=1:6
r=n;
x=linspace(a,b,r+1);
FX=valus(x);
FX(1)=1;  %because of 0 start point
table(j,1)=trapz(x,FX);
n=2*r;
end

%Generate 2nd column of Romberg Table
for j=1:5
table(j,2)= ([(2^2)*table(j+1,1)] - [table(j,1)])/(2^2 - 1);
end

%Generate 3rd column of Romberg Table
for j=1:4
table(j,3) = ([(2^(2*2))*table(j+1,2)] - table(j,2))/(2^(2*2)-1);
end

%Generate 4th column of Romberg Table
for j=1:3
table(j,4) = ([(2^(2*3))*table(j+1,3)] - table(j,3))/(2^(2*3)-1);
end

%Generate 5th Column of Romberg Table
for j=1:2
table(j,5) = ([(2^(2*4))*table(j+1,4)] - table(j,4))/(2^(2*4)-1);
end

%Generate 6th column of Romberg Table
table(1,6) = ([(2^(2*5))*table(2,5)] - table(1,5))/(2^(2*5)-1);

toc


Gives time =0.04sec

Using chebfun - sum:

  tic
y = chebfun('(exp(x)-1)./x',[eps 1]);
I=sum(y)
toc


I = 1.317902151454404 and time = 0.009175 sec.

Using Trapz function:

    I = 1.31790215145;               %Exact value of integral
n = 1;
tic                              %clock begins
while(true)

X = 0:1/n:1;                 %divides x into n equal parts
Y = (exp(X) - 1) ./ X;       %given function
Y(1) = 1;                    %corrects for the singulairty at x=0
In = trapz(X,Y);             %calls teh built-in function trapz
if(abs((I - In)) < (10^(-10))) %check order of error
break;
end
n = 2*n;
end
toc                             %clock ends
output = In
number = n


n=32768 ,In=1.31790215149321 and time = 0.006879 sec

   I =1.3179021514544038948600088;  %Exact value of integral
n = 2;
clc
format long;
tic                              %clock begins
while(true)
X = 0:1/n:1;                 %divides the interval into n equal parts
F=@(X)(exp(X)-1)./X;         %given function as an anonymous function

if(abs((I - In)) < ((10)^(10)))  %check for error
break;
end
n = n+1;
end
toc
output = In
number = n


Gives n=2 ,In= 1.31790215145441 and time = 0.004 sec

Using clencurt function

  tic
[x,w] = clencurt(2^5);
xx=(1+eps)/2+x.*(1-eps)/2;
ww=w.*(1-eps)/2;
yy=(exp(xx)-1)./xx;
II=ww*yy
toc


Gives II=1.317902151454404 and time = 0.000308 sec

##### Part3:Computational times
 Computational Times ${\displaystyle {\boldsymbol {METHOD}}}$ ${\displaystyle {\boldsymbol {n}}}$ ${\displaystyle {\boldsymbol {Time}}}$ Composite Trapezoidal 32768 4.93 Composite Simpsons 128 0.017 Romberg Table 32 0.04 Chebfun-sum NA 0.009175 Trapz 32768 0.006879 Quad 2 0.004 Clencurt 16 0.000308

solved by---Egm6341.s10.team2.niki 18:20, 28 February 2010 (UTC)

checked by---Egm6341.s10.team2.lee 22:20, 2 March 2010 (UTC)

## Problem 10: Another comparison of codes

#### Statement

Repeat Problem 9 for integral ${\displaystyle \displaystyle {I=\int _{-5}^{5}{\frac {1}{1+x^{2}}}dx}}$

#### Solution

The exact value of the definite integral is atan(5)-atan(-5)=2.746801533890034...

##### own code - composite trapezoidal rule

Function Definition

 function I = trapzoid(f,a,b,n)
h=(b-a)/n;
x=linspace(a,b,n+1);
fx=feval(f,x);
I=h*(fx(1)/2+sum(fx(2:1:n))+fx(n+1)/2);



Call Function

 clear;clc
format long
f = @(x) 1./(1+x.^2);
tic
I = trapzoid(f,-5,5,2^14)
toc
e = atan(5)-atan(-5)-I


Result

 I = 2.746801532971545
Elapsed time is 0.001745 seconds.
e = 9.184866200939723e-010

##### own code - composite simpson's rule

Function Definition

 function In = simpson(f,a,b,n)
% n must be a positive even integer
h=(b-a)/n; %step size
x=linspace(a,b,n+1); %(n+1) equally spaced points
fx=feval(f,x); %function evaluations
%Composite Simpson
In=(h/3)*(fx(1)+4*sum(fx(2:2:n))+2*sum(fx(3:2:n-1))+fx(n+1));


Call Function

 clear;clc
format long
f = @(x) 1./(1+x.^2);
tic
I = simpson(f,-5,5,2^8)
toc
e = atan(5)-atan(-5)-I


Result

 I = 2.746801533727021
Elapsed time is 0.000343 seconds.
e = 1.630109380812428e-010


Function Definition

 function I_romb = rombquad(f,a,b,m)
h = 2.^((1:m)-1)*(b-a)/2^(m-1);             % intervals used.
k1 = 2.^((m-2):-1:-1)*2+1;                  % Index into intervals.
f1 = feval(f,a:h(1):b);                     % Function evaluations.
R = zeros(1,m);                             % Pre-allocation.
% Define the starting vector:
for ii = 1:m
R(ii) = 0.5*h(ii)*(f1(1)+2*sum(f1(k1(end-ii+1):k1(end-ii+1)-1:end-1)) + f1(end));
end
% Interpolations:
for jj = 2:m
jpower = (4^(jj-1)-1);
for kk = 1:(m-jj+1)
R(kk) = R(kk)+(R(kk)-R(kk+1))/jpower;
end
end
I_romb = R(1);


Call Function

 clear;clc
format long
y = @(x) 1./(1+x.^2);
tic
toc
e=atan(5)-atan(-5)-I_romb


Result

 I_romb = 2.746801534346014
Elapsed time is 0.000356 seconds.
e = -4.559819188898473e-010

##### matlab - trapz and quad

Call Function

 clear;clc
format long
x=linspace(-5,5,2^14+1);
y = 1./(1+x.^2);
I_exact=atan(5)-atan(-5)
tic;I_ct=trapz(x,y);toc
I_ct
e_ct=I_exact-I_ct
I_cs
e_cs=I_exact-I_cs


Result

 I_exact = 2.746801533890032
Elapsed time is 0.003191 seconds.
I_ct = 2.746801532971568
e_ct = 9.184639715442700e-010
Elapsed time is 0.009436 seconds.
I_cs = 2.746801534322983
e_cs = -4.329510083778132e-010


##### matlab - clencurt

Call Function

 tic
[xx,ww] = clencurt(2^10);
xx=xx.*5;
ww=ww.*5;
yy=1./(1+xx.^2);
II=ww*yy
toc


Result

 II = 2.746801533890032
Elapsed time is 0.089293 seconds.

##### "sum" command of chebfun matlab toolbox

Call Function

 x = chebfun('x',[-5 5]);
tic
I=sum(1./(1+x.^2))
toc
e=abs(atan(5)-atan(-5)-I)


Result

 I = 2.746801533890033
Elapsed time is 0.042520 seconds.
e = 1.332267629550188e-015


solved by---Egm6341.s10.team2.lee 22:22, 2 March 2010 (UTC)

checked by-----Egm6341.s10.team2.niki 20:45, 6 March 2010 (UTC)

## Problem 11: Finding The Arc Length of an Ellipse using Numerical Integration

#### Statement

Find the Arc Length of the ellipse where:

${\displaystyle r(\theta )={\frac {1-e^{2}}{1-e\cos(\theta )}}}$

and

${\displaystyle eccentricity=e=\sin({\frac {\pi }{12}})}$

Using:

1)Composite Trapezoidal Rule

2)Composite Simpson's Rule

3)Romberg Table

4)"Sum" Command of the chebfun toolbox

5)"Trapz" Command of Matlab

7)"Clencurt" Command of Matlab

Until the error is less than ${\displaystyle 10^{-6}}$

#### Solution

##### Composite Trapezoidal Rule
 Composite Trapezoidal Rule n Value Error 2 6.283185307 2.E-01 4 6.072738504 4.E-03 8 6.069092055 1.E-06 16 6.06909096 8.E-08

function [I,E]=compostrap(a,b,n)

h=(b-a)/n;
pi2=2*pi();
i=0;
Itot=0;
It=0;
It2=0;

while i<=n
if i==0
else if i<n
u=h*i;
else
end
end
Itot=Itot1+sum(It)+It2;
i=i+1;
end

I=Itot*(h);

%Error Calculation

E=Ir-I;


##### Composite Simpson's Rule
 Composite Simpson's Rule n Value Error 2 5.470081296 6.E-01 4 5.867072234 2.E-01 8 6.000117905 7.E-02 16 5.870432233 2.E-01 32 5.969761779 1.E-01 64 6.101815883 3.E-02 128 6.085453421 2.E-02 256 6.07727219 8.E-03 512 6.062882886 6.E-03 1024 6.071136267 2.E-03 2048 6.067538941 2.E-03 4096 6.06831495 8.E-04 8192 6.069346623 3.E-04 16384 6.068896957 2.E-04 32768 6.069154875 6.E-05 65536 6.069122917 3.E-05 131072 6.069106939 2.E-05 262144 6.069098949 8.E-06 524288 6.069084897 6.E-06 1048576 6.069092957 2.E-06 2097152 6.069091958 1.E-06

MATLAB Code:

function I = msimpb(a,b,w)

q=1;
i=a;
n=0;
Sum=0;
c=0;

e=sin(pi()/12);

while n<w
n=2^q;
h=(a+b)/n;
while i<=b
fx= (1-e^2)/(1-e*cos(i)); %(exp(i)-1)/(i);  Declare Function Here
if i==a
Sum=1;
else if i==b
Sum=Sum+fx;
else if i==(b-h)
Sum=Sum+(4*fx);
else if rem(c,2)==0
Sum=Sum+(2*fx);
else
Sum=Sum+(4*fx);
end
end
end
end
c=c+1;
i=i+h;
end
n;
In=Sum*(h/3);
I=In;
i=a;
c=0;
q=q+1;
Sum=0;
end


##### Romberg Table

Romberg Table for n=2

   6.2832    6.0026    6.0722    6.0691    6.0691    6.0691
6.0727    6.0679    6.0692    6.0691    6.0691         0
6.0691    6.0691    6.0691    6.0691         0         0
6.0691    6.0691    6.0691         0         0         0
6.0691    6.0691         0         0         0         0
6.0691         0         0         0         0         0


Table for Error of Romberg Table

  -0.2141    0.0665   -0.0031   -0.0000    0.0000   -0.0000
-0.0036    0.0012   -0.0001    0.0000   -0.0000         0
-0.0000    0.0000   -0.0000   -0.0000         0         0
-0.0000   -0.0000   -0.0000         0         0         0
-0.0000   -0.0000         0         0         0         0
-0.0000         0         0         0         0         0


Romberg Table for n=4

   6.0727    6.0679    6.0692    6.0691    6.0691    6.0691
6.0691    6.0691    6.0691    6.0691    6.0691         0
6.0691    6.0691    6.0691    6.0691         0         0
6.0691    6.0691    6.0691         0         0         0
6.0691    6.0691         0         0         0         0
6.0691         0         0         0         0         0


Error for Romberg Table n=4

  -0.0036    0.0012   -0.0001    0.0000   -0.0000   -0.0000
-0.0000    0.0000   -0.0000   -0.0000   -0.0000         0
-0.0000   -0.0000   -0.0000   -0.0000         0         0
-0.0000   -0.0000   -0.0000         0         0         0
-0.0000   -0.0000         0         0         0         0
-0.0000         0         0         0         0         0


Romberg Table for n=8

   6.0691    6.0691    6.0691    6.0691    6.0691    6.0691
6.0691    6.0691    6.0691    6.0691    6.0691         0
6.0691    6.0691    6.0691    6.0691         0         0
6.0691    6.0691    6.0691         0         0         0
6.0691    6.0691         0         0         0         0
6.0691         0         0         0         0         0


Table for Error when n=8

 1.0e-005 *

  -0.1180    0.0281   -0.0109   -0.0084   -0.0085   -0.0085
-0.0085   -0.0085   -0.0085   -0.0085   -0.0085         0
-0.0085   -0.0085   -0.0085   -0.0085         0         0
-0.0085   -0.0085   -0.0085         0         0         0
-0.0085   -0.0085         0         0         0         0
-0.0085         0         0         0         0         0


##### "Sum" Command of Chebfun Matlab Toolbox

${\displaystyle In=6.069090959564776}$

${\displaystyle Error=I-In=-8.457248767967940e-008}$

>> chebfun('elrad(x)', [0 pi2])
ans =
chebfun column (1 smooth piece)
interval          length    endpoint values
(        0,      6.3)       43        1.3       1.3
vertical scale = 1.3

>> sum(F1)

ans =

6.069090959564776

I =

6.069090874992289

>> Error=I-ans

Error =

-8.457248767967940e-008


##### "Trapz" Command of Matlab
Trapz Matlab Command

 n-value Numerical Integral (In) Error (En) 2 6.283185307 2.E-01 4 6.072738504 4.E-03 8 6.069092055 1.E-06

The value found using the "Quad" Matlab Command was:

${\displaystyle I=6.069090875}$

with an error smaller than ${\displaystyle 10^{-6}}$

solved by--- --Egm6341.s10.Team2.GV 22:04, 3 March 2010 (UTC)

checked by---Egm6341.s10.team2.patodon 21:15, 3 March 2010 (UTC)

## Contributing Authors and Assignments

#### Signatures

solved problems 4 and 9, proofread problems 5 and 10:--Egm6341.s10.team2.niki 18:22, 28 February 2010 (UTC)

solved problems 5 and 10, proofread problems 7 and 9:--Egm6341.s10.team2.lee 22:26, 2 March 2010 (UTC)

solved problems 7, proofread problems 1, 2, 5, 6, 8 and 11 Egm6341.s10.team2.patodon 23:01, 2 March 2010 (UTC)

solved problems 1, 6 and 11. Proofread 3, 9. --Egm6341.s10.Team2.GV 22:06, 3 March 2010 (UTC)