Solve Simpson Algorithm with C Language

xiaoxiao2021-03-06  214

#include

#include

#include

Double Sum4 (DOUBLE); // Function Declaration

Double Sum2 (double);

Double f (double);

//

void main ()

{

Double Sn, S2n, H, Result, A, B

Double EPS;

Int n;

Cout << "Please enter a =";

CIN >> A;

Cout << "Please enter b =";

CIN >> B;

Cout << "Please enter EPS (such as: If you enter 7, it is indicated as 10 ^ -7) =";

CIN >> EPS;

IF (A> B || EPS <0) Return;

EPS = POW (10, -EPS); // Accuracy

H = b-a;

n = (int) ((b-a) / h);

SN = H / (6 * n) * (f (a) 4 * SUM4 (H) 2 * Sum2 (H) f (b));

While (h> 0)

{

H / = 2;

S2n = h / (6 * n) * (f (a) 4 * SUM4 (H) 2 * Sum2 (H) f (b));

IF ((FABS (S2n-Sn) / 15)> EPS) SN = S2n;

Else

{

Result = S2n; // Exit circulatory conditions

Break;

}

}

Cout << "The result is" << setPrecision (9) << result << Endl << "h size =" << h << Endl;

}

//

Double F (double x) // function f (x) expression

{

Return (Exp (-x * x));

}

//

Double Sum4 (Double H) // Composite SIMPSON formula is 4 results

{

INT N, I;

Double Sum = 0;

n = (int) (1 / h);

For (i = 0; i

SUM = f ((i 0.5) * h);

Return SUM;

}

//

Double Sum2 (Double H) // Composite SIMPSON formula is 2 results

{

INT N, I;

Double Sum = 0;

n = (int) (1 / h);

For (i = 1; i

SUM = f (i * h);

Return SUM;

}

转载请注明原文地址:https://www.9cbs.com/read-128681.html

New Post(0)