00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "IntegrationOfFunctions.h"
00018
00019 #include<iostream>
00020
00021 namespace Flood
00022 {
00023
00024
00025
00027
00028 IntegrationOfFunctions::IntegrationOfFunctions(void)
00029 {
00030
00031 }
00032
00033
00034
00035
00037
00038 IntegrationOfFunctions::~IntegrationOfFunctions(void)
00039 {
00040
00041 }
00042
00043
00044
00045
00046
00047
00055
00056 double IntegrationOfFunctions
00057 ::calculate_trapezoid_integral(double (*f)(double), double a, double b , int n)
00058 {
00059 double trapezoid_integral = 0.0;
00060
00061
00062
00063 double h = (b-a)/(n-1.0);
00064
00065
00066
00067 double sum = 0.0;
00068
00069 for(int i = 1; i < n; i++)
00070 {
00071 sum += f(a + i*h);
00072 }
00073
00074
00075
00076 trapezoid_integral = h*(f(a)/2.0 + sum + f(b)/2.0);
00077
00078 return(trapezoid_integral);
00079 }
00080
00081
00082
00083
00091
00092 double IntegrationOfFunctions
00093 ::calculate_Simpson_integral(double (*f)(double), double a, double b, int n)
00094 {
00095 double Simpson_integral = 0.0;
00096
00097
00098
00099 double h = (b-a)/(n-1.0);
00100
00101 double sum = f(a)/3.0;
00102
00103 for(int i = 1; i < n-1; i++)
00104 {
00105 if(i%2 != 0)
00106 {
00107 sum += 4.0*f(a + i*h)/3.0;
00108 }
00109 else
00110 {
00111 sum += 2.0*f(a + i*h)/3.0;
00112 }
00113 }
00114
00115 sum += f(b)/3.0;
00116
00117
00118
00119 Simpson_integral = h*sum;
00120
00121 return(Simpson_integral);
00122 }
00123
00124
00125
00126
00132
00133 double IntegrationOfFunctions
00134 ::calculate_trapezoid_integral(const Vector<double>& x, const Vector<double>& y)
00135 {
00136
00137
00138 int n = x.get_size();
00139
00140
00141
00142 double trapezoid_integral = 0;
00143
00144 for(int i = 0; i < n-1; i++)
00145 {
00146 trapezoid_integral += 0.5*(x[i+1]-x[i])*(y[i+1]+y[i]);
00147 }
00148
00149
00150
00151 return(trapezoid_integral);
00152 }
00153
00154
00155
00156
00162
00163 double IntegrationOfFunctions::calculate_Simpson_integral(const Vector<double>& x, const Vector<double>& y)
00164 {
00165 double Simpson_integral = 0.0;
00166
00167
00168
00169 int n = x.get_size();
00170
00171 int m = 0;
00172
00173 double a = 0.0;
00174 double fa = 0.0;
00175 double b = 0.0;
00176 double fb = 0.0;
00177 double c = 0.0;
00178 double fc = 0.0;
00179 double wa = 0.0;
00180 double wb = 0.0;
00181 double wc = 0.0;
00182 double h = 0.0;
00183
00184 double sum = 0.0;
00185
00186 if(n%2 != 0)
00187 {
00188 m=(n-1)/2;
00189
00190 for(int i = 0 ; i < m ; i++ )
00191 {
00192 a = x[2*i];
00193 b = x[2*i+1];
00194 c = x[2*i+2];
00195
00196 fa = y[2*i];
00197 fb = y[2*i+1];
00198 fc = y[2*i+2];
00199
00200 wa = (c-a)/((a-b)*(a-c))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(b+c)+b*c);
00201 wb = (c-a)/((b-a)*(b-c))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(a+c)+a*c);
00202 wc = (c-a)/((c-a)*(c-b))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(a+b)+a*b);
00203
00204 sum += wa*fa+wb*fb+wc*fc;
00205 }
00206 }
00207 else
00208 {
00209 m=(n-2)/2;
00210
00211 for(int i = 0; i < m; i++ )
00212 {
00213 a = x[2*i];
00214 b = x[2*i+1];
00215 c = x[2*i+2];
00216
00217 fa = y[2*i];
00218 fb = y[2*i+1];
00219 fc = y[2*i+2];
00220
00221 wa = (c-a)/((a-b)*(a-c))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(b+c)+b*c);
00222 wb = (c-a)/((b-a)*(b-c))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(a+c)+a*c);
00223 wc = (c-a)/((c-a)*(c-b))*(1.0/3.0*(a*a+c*c+a*c)-0.5*(a+c)*(a+b)+a*b);
00224
00225 sum += wa*fa+wb*fb+wc*fc;
00226 }
00227
00228
00229
00230 h = x[n-1]-x[n-2];
00231
00232 sum += h*(y[n-1]+y[n-2])/2.0;
00233 }
00234
00235 Simpson_integral = sum ;
00236
00237 return(Simpson_integral);
00238 }
00239
00240 }
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258