Skip to content

Commit

Permalink
Translation and corrected h calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrutkowski-aero authored Apr 15, 2024
1 parent d632997 commit c5b3d59
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions code/info2/kwad.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

// oblicza calke metoda trapezow
// computes definite integral using complex the trapezoidal rule
double trapez(double a, double b, double (*pf)(double), int n )
{
double h = (b - a) / (n - 1);
double h = (b - a) / n;
double suma = 0.5 * (pf(a) + pf(b));
double x = a;

for (int i = 1; i < n-1; i++)
for (int i = 0; i < n-1; i++)
{
x += h;
suma += pf(x);
}
return suma * h;
}


// oblicza calke metoda simpsona
// computes definite integral using complex the Simpson rule
double simpson( double a, double b, double (*pf)(double), int n )
{
double x = a;
int nc = abs((n - 2) / 2 + 1) * 2 + 1; // poprawione n tak by bylo nieparzyste i >= n_old
double h = (b - a) / (nc - 1);
double h = (b - a) / (2*n);
double h2 = h * 2;
double suma = pf(a) + 4. *pf(a + h) + pf(b);
double x1 = a + h;

double suma = pf(a) + 4. *pf(x1) + pf(b);

for (int i = 3; i < nc; i += 2)
for (int i = 0; i < n-1; i += 1)
{
x += h2;
suma += 2. * pf(x) + 4. * pf(x + h);
Expand Down

0 comments on commit c5b3d59

Please sign in to comment.