problem receiving: "1.#INF" in numerical integration of a function

I'm using 4 methods: Simpson's, trapezoid's, rectangle's and the tangent's.
Integrating the function: "2/sqrt(1-pow(x,2.0))" for the 2 first methods I receive "1.#INF",
and for the rest is 3, 14 , but only if n>=10000
(normally it works even if n=10).
Could you please explane me those 2 errors and how to solve them?
Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <cstdlib>
#include <iostream.h>
#include <math.h>
#include <stdio.h>

using namespace std;

double F(double x)
{
  return 2/sqrt(1-pow(x,2.0));
}

long double simpson(int n ,double a, double b)
{
    long double s=0.0, h=0.0, x=0.0;
    if((n/2)*2 != n) {n=n+1;}
    h = (b-a)/double(n); 
    for ( int i=2; i<=n-1; i=i+2)
    {
        x = a+double(i)*h;
        s = s + 2.0*F(x) + 4.0*F(x+h);
    }
    s = (s + F(a)+F(b)+4.0*F(a+h) )*h/3.0;
    return s;
}

double trapezoid(int n, double a, double b)
    {double h=0.0;
    double T=0.0;
    double y=0.0;
    double z1=0.0;
    double z2=0.0;
    h=(b-a)/double(n);
    z1=h*(F(a)+F(b))/2;
    for(int i=1; i<=(n-1); i=(i+2))
            {z2+=F(a+(h*double(i)));}
    z2=h*z2;
    T=z1+z2;
    return T;}
    


double rectangle(int n, double a, double b)
       {double h=0,p=0;
        double y=0;
        h=(b-a)/double(n);
        for(int i=0; i<=(n-1);i++)
           {y+=F(a+(h*double(i)));}
           p=h*y;
        return p;    
       }
       
       
double  tangents(int n, double a, double b)
        {double h=0,p=0;
         double y=0;
         h=(b-a)/double(n);
         for(int i=1; i<=(n-1);i=i+2)
           {y+=F(a+(h*double(i)));}
           p=2*h*y;
         return p;}
        
              
void choice(int n, double a, double b)
     {int choice=0;
      double s=0.0;
      double t=0.0;
      double r=0.0;
      double ta=0.0;
     printf("Choix de method: \n\t1 - simpson\n\t2 - trapezoid\n\t3 - rectangle\n\t4 - tangents\n\nChoix:  ");
     scanf("%d",&choice);
     switch(choice)
 {case 1: 
        simpson(n,a,b);
        s=simpson(n,a,b);  
        printf("\n %lf\n",s); /* on affiche le re'sultat */
        break;
  case 2:
       trapezoid(n,a,b);
       t=trapezoid(n,a,b); /* on stock dans une variable tampon */
       printf("\n %lf\n",t);
       break;
  case 3:
        rectangle(n,a,b);
        r=rectangle(n,a,b);  /* on stock dans une variable tampon */
        printf("\n %lf\n",r);
        break;
  case 4:
       tangents(n,a,b);
       ta=tangents(n,a,b);
       printf("\n %lf\n",ta);
        break;
       
  }}


int main(int argc, char *argv[])
{ int n;
  double a,b;
  a=0.0; /* Borne inferieure */
  b=1.0; /* Borne superieure */
  n=1000; /* Nombre d'iteration  */
    choice(n,a,b);
    system("PAUSE");
    return 0;
}
Last edited on
1.#INF means +∞. If you divide by 0, that is the result

lim (±n/x) = ±∞
x→0
Topic archived. No new replies allowed.