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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const double PI = 4.0 * atan( 1.0 );
// Routines supplied by KCL2000
double intsimpson13( double a, double b, int n, double theta, double(*funcion)(double x, double y));
double inttrapecio(double a, double b, int n, double theta, double(*funcion)(double x, double y));
double funcion(double phi, double theta);
// My own routines
double agm( double x, double y );
double ellipt( double k );
double mySimpson( double a, double b, int n, double theta, double (*f)( double, double ) );
int main()
{
#define COL << setw( 25 ) << fixed << setprecision( 16 ) <<
const double a = 0.0, b = PI / 2.0;
const double theta = 70.0 * PI / 180.0;
// Exact values
double k = sin( 0.5 * theta );
double exact = ellipt( k );
cout COL "k=" COL k << '\n';
cout COL "ellipt(k)=" COL exact << "\n\n";
// Values by numerical integration
cout COL "n" COL "trapezium" COL "Simpson" COL "logE_trapezium" COL "logE_Simpson" << '\n';
for ( int n = 4; n <= 60; n+= 2 )
{
double value_trapezium = inttrapecio ( a, b, n, theta, funcion );
double value_Simpson = intsimpson13( a, b, n, theta, funcion );
// double value_Simpson = mySimpson ( a, b, n, theta, funcion );
double logE_trapezium = log10( abs( value_trapezium - exact ) );
double logE_Simpson = log10( abs( value_Simpson - exact ) );
cout COL n COL value_trapezium COL value_Simpson COL logE_trapezium COL logE_Simpson << '\n';
}
}
//****************** functions supplied by user KCL2000 **************
double intsimpson13( double a, double b, int n, double theta, double(*funcion)(double x, double y))
{
double h = (b-a)/double(n);
double sum1 = 0;
double sum2 = 0;
double x1,x2;
double fx1,fx2;
for (int i=1; i<(n/2); i++)
{
x1 = ((b-a)*2*i)/n+a;
fx1=funcion(x1,theta);
sum1=sum1+fx1;
}
for (int i=1; i<=(n/2); i++)
{
x2= ((b-a)*(2*i-1))/n+a;
fx2=funcion(x2,theta);
sum2=sum2+fx2;
}
double hint=2*sum1+4*sum2+funcion(a,theta)+funcion(b,theta);
return (hint*h)/3;
}
double inttrapecio(double a, double b, int n, double theta, double(*funcion)(double x, double y))
{
double h = (b-a)/double(n);
double sum = 0;
double x;
double fx;
for (int i=1; i<n; i++)
{
x = ((b-a)*i)/n;
fx=funcion(x,theta);
sum=sum+fx;
}
double inth = funcion(a,theta)+funcion(b,theta)+2*sum;
return inth*h/2;
}
double funcion(double phi, double theta)
{
double k=sin(theta/2);
return 1/(sqrt(1-(k*k*sin(phi)*sin(phi))));
}
//****************** end of functions supplied by user KCL2000 **************
//****************** My routines ***********************
double agm( double x, double y )
{
const double EPSILON = 1.0e-30;
double a = x, g = y;
double aold = a + 1, gold = g + 1;
while ( abs( a - aold ) > EPSILON || abs( g - gold ) > EPSILON )
{
aold = a;
gold = g;
a = 0.5 * ( aold + gold );
g = sqrt( aold * gold );
}
return a;
}
double ellipt( double k )
{
return 0.5 * PI / agm( 1.0, sqrt( 1.0 - k * k ) );
}
double mySimpson( double a, double b, int n, double theta, double (*f)( double, double ) )
{
double dx = ( b - a ) / n;
double I = f( a, theta ) + f( b, theta );
for ( int i = 1; i < n; i += 2 ) I += 4.0 * f( a + i * dx, theta );
for ( int i = 2; i < n; i += 2 ) I += 2.0 * f( a + i * dx, theta );
return I *= dx / 3.0;
}
|