HW #02
Due: 09/10/2025



1. Compute


lim
x → ∞ 

xx2 ln (1 + 1

x
)
.


2. Knowing that



0 
ex cos x dx =


0 
ex sin x dx = 1

2
,
evaluate
I


0 
x  ex cos x dx,     J


0 
x  ex sin x dx,
by differentiating
x  ex cos x and  x  ex sin x.
No other methods are accepted.


3. It is known that


1

0 
ex2 d x = 1

2


 

π
 
  erf(1)  ∼ 0.746824.
How many partitions are required to achieve this much accuracy for (1) the left-point rectangular rule, (2) the mid-point rectangular rule (Eq.(4) in Lecture #5) and (10) in Lecture #4 the trapezoidal rule ?
You do not need to include the program code. Simply provide the results along with a brief discussion.

Online C compiler
Online Matlab/Octave
Sample C code for the left-point rectangular rule
#include <stdio.h>
#include <math.h>

double f(double x)
 {return 4.0/(1.0+x*x) ; }

int main()
{
 int i, n;
 double a=0.0, b=1.0, h, s=0.0 , x ;
 n=100;
 h = (b-a)/n ;
 for (i= 0;i<n;i++) 
  s = s + f(a + i*h) ;
 s=s*h ;
 printf("Result =%lf\n", s) ;
 return 0;
}
Sample Matlab/Octave code for the left-point rectangular rule
f = @(x) 4/(1+x^2);
a=0;b=1;s=0;
%n=input('Enter n=');
n=100
h=(b-a)/n;
for i=0:1:n-1 
  s=s+f(a+i*h);
end;
s=s*h;
fprintf('%f\n', s);
Sample C code for the trapezoidal rule
/* Trapezoidal rule */
#include <stdio.h>
#include <math.h>

double f(double x)
 {return 4.0/(1.0+x*x);}

int main()
{
 int i, n ;
 double a=0.0, b=1.0 , h, s=0.0, x;
/*
 printf("Enter number of partitions = ");
 scanf("%d", &n) ;
*/
 n=10;
 h = (b-a)/n ;

 for (i=1;i<=n-1;i++) s = s + f(a + i*h);

  s=h/2*(f(a)+f(b))+ h* s;

  printf("%20.12f\n", s) ;
  return 0;
}
Sample MATLAB (Octave) code for the trapezoidal rule
f = @(x) 4/(1+x^2);
a=0;b=1;s=0;
n=10;
%n=input('Enter n=');

h=(b-a)/n;

for i=1:1:n-1 
 s=s+f(a+i*h);
end;

s=h/2*(f(a)+f(b))+h*s;
fprintf('%f\n', s);
Solution
1. Note that
ln 
1 + 1

x

= 1

x
1

2
1

x2
+ 1

3
1

x3
− …
so

xx2 ln 
1 + 1

x

= 1

2
1

3 x
+ 1

4 x2
1

2
    as     x → ∞.
Alternatively, set
t 1

x
so that


lim
x→ ∞ 

xx2 ln 
1+ 1

x


=

lim
t→ 0 

1

t
ln(1+t)

t2

=

lim
t→ 0 
t − ln(1+t)

t2
(1)
=

lim
t→ 0 
t − (t t2

2
+ t3

3
− …)

t2
=

lim
t→ 0 
1

2
t

3
+ …
=
1

2
.
(2)
2.
(x ex cos x)′ = excos xx ex cos xx ex sin x,

(x ex sin x)′ = exsin xx ex sin x + x ex cos x,
If we integrate both sides from 0 to ∞, we have
0 = 1

2
IJ,

0 = 1

2
J + I,
from which it follows
I = 0,     J= 1

2
.
3.
Short answer:
Long answer:
Note: It is NOT necessary to list programs. Just the result suffices.

    (1)Left-point rectangular rule
    #include <stdio.h>
    #include <math.h>
    
    double f(double x)
     {return exp(-x*x) ; }
    
    int main()
    {
     int i, n;
     double a=0.0, b=1.0, h, s=0.0 , x ;
     printf("Enter n="); 
     scanf("%d", &n);
     h = (b-a)/n ;
     for (i= 0;i<n;i++) 
      s = s + f(a + i*h) ;
     s=s*h ;
     printf("Result =%lf\n", s) ;
     return 0;
    }
    
    
    
    C:\gcc-2.95.2>a
    Enter n=1000
    Result =0.747140
    
    C:\gcc-2.95.2>a
    Enter n=10000
    Result =0.746856
    
    C:\gcc-2.95.2>a
    Enter n=100000
    Result =0.746827
    
    C:\gcc-2.95.2>a
    Enter n=1000000
    Result =0.746824
    
    C:\gcc-2.95.2>a
    Enter n=500000
    Result =0.746825
    
    C:\gcc-2.95.2>a
    Enter n=700000
    Result =0.746825
    
    C:\gcc-2.95.2>a
    Enter n=900000
    Result =0.746824
    
    
    

    (2) Mid-point rectangular rule
    /* Rectangular rule */
    #include <stdio.h>
    #include <math.h>
    
    double f(double x)
     {return exp(-x*x) ; }
    
    int main()
    {
     int i, n;
     double a=0.0, b=1.0, h, s=0.0 , x ;
    
     printf("Number of partitions = ");
     scanf("%d", &n) ;
    
     h = (b-a)/n ;
    
     for (i= 0;i<n;i++) s = s + f(h/2 + i*h) ;
    
      s=s*h ;
    
      printf("Result =%f\n", s) ;
      return 0;
    }
    
    
    C:\gcc-2.95.2>mid
    Number of partitions = 200
    Result =0.746825
    
    C:\gcc-2.95.2>mid
    Number of partitions = 300
    Result =0.746824
    
    C:\gcc-2.95.2>mid
    Number of partitions = 290
    Result =0.746824
    
    C:\gcc-2.95.2>mid
    Number of partitions = 260
    Result =0.746825
    
    C:\gcc-2.95.2>mid
    Number of partitions = 270
    Result =0.746825
    
    C:\gcc-2.95.2>mid
    Number of partitions = 280
    Result =0.746825
    
    C:\gcc-2.95.2>mid
    Number of partitions = 290
    Result =0.746824
    
    

    (3) Trapezoidal rule
    /* Trapezoidal rule */
    #include <stdio.h>
    #include <math.h>
    double f(double x)
     {return exp(-x*x);}
    
    int main()
    {
     int i, n ;
     double a=0.0, b=1.0 , h, s=0.0, x;
    
     printf("Enter number of partitions = ");
     scanf("%d", &n) ;
    
     h = (b-a)/n ;
    
     for (i=1;i<=n-1;i++) s = s + f(a + i*h);
    
      s=h/2*(f(a)+f(b))+ h* s;
    
      printf("%f\n", s) ;
      return 0;
    }
    
    
    C:\gcc-2.95.2>gcc trap.c -o trap
    
    C:\gcc-2.95.2>trap
    Enter number of partitions = 200
    0.746823
    
    C:\gcc-2.95.2>trap
    Enter number of partitions = 300
    0.746823
    
    C:\gcc-2.95.2>trap
    Enter number of partitions = 320
    0.746824
    
    C:\gcc-2.95.2>trap
    Enter number of partitions = 310
    0.746823
    
    
According to the result above from the C program, it takes about 900,000 partitions to obtain 0.746824 using the left-point rectangular rule and 290 partitions using the left-point rectangular rule. It takes 320 partitions to obtain the same accuracy using the trapezoidal rule. Note these values vary depending on the application used.
Alternatively, according to the error analysis discussed in class, the magnitude of an error is proportional to h ( = O(h)) for the left-point rectangular rule and is proportional to h2 ( = O(h2)) for the mid-point rectangular rule and the trapezoidal rule. Hence, to get the accuracy of 10−6, it requires 106 partitions in the rectangular rule and it requires 1000 = 2√{106} partitions for the mid-point rectangular rule and the trapezoidal rule.
So theoretically, it requires 106 for the left-point rectangular rule and 1,000 partitions for the mid-point rectangular rule and the trapezoidal rule.
In reality, it requires 9 ×105 for the rectangular rule and about 300 for the mid rectangular rule and the trapezoidal rule. In fact, the mid rectangular rule and the trapezoidal rule work rather too well in this case.





File translated from TEX by TTH, version 4.03.
On 14 Sep 2025, 19:41.