Double

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

double.

integralC++

// C++ program calculating double integral using Simpson's 1/3 Rule

#include <iostream>
#include<cmath>
using namespace std;

float givenFunction(float x, float y)


{
return pow(x, 2) + pow(y, 2);
}

// Function to find the double integral value


/*
lx and ux are upper and lower limit of x integral
ly and uy are upper and lower limit of y integral
h and k is the step size for integration x and y
*/

float DoubleIntegral(float h, float k,float lx, float ux, float ly,


float uy) {

int nx, ny;

float f[40][40], a[4], res; // f stores the table, a stores


the integral y

// Calculating the number of points


// in x and y integral
nx = (ux - lx) / h + 1;
ny = (uy - ly) / k + 1;

// Calculating the values of the table


for (int i = 0; i < nx; ++i) {
for (int j = 0; j < ny; ++j) {
f[i][j] = givenFunction(lx + i * h,ly + j * k);
}
}

// Calculating the integral value

Page 1/2
double.integralC++

for (int i = 0; i < nx; ++i) {


a[i] = 0;
for (int j = 0; j < ny; ++j) {
if (j == 0 || j == ny - 1)
a[i] += f[i][j];
else if (j % 2 == 0)
a[i] += 2 * f[i][j];
else
a[i] += 4 * f[i][j];
}
a[i] *= (k / 3);
}

res=0;
// Calculating the final integral value
for (int i = 0; i < nx; ++i) {
if (i == 0 || i == nx - 1)
res += a[i];
else if (i % 2 == 0)
res += 2 * a[i];
else
res += 4 * a[i];
}
res =res*(h / 3);

return res;
}

int main()
{

float h, k, lx, ux, ly, uy;

lx = 0, ux = 1, ly = 1,
uy = 2, h = 0.1, k = 0.2;

printf("%f", DoubleIntegral(h, k, lx, ux, ly, uy));


return 0;
}

Page 2/2

You might also like