Commit 5d35c0c1 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Finished assignment 6.

parent b1414ded
...@@ -4,18 +4,26 @@ ...@@ -4,18 +4,26 @@
#include "func_ptr.h" #include "func_ptr.h"
#include "integral.h" #include "integral.h"
#define STRIDE 10000
double accurate_integral(func_ptr f, method_ptr method, double a, double accurate_integral(func_ptr f, method_ptr method, double a,
double b, double accuracy, unsigned int stride) { double b, double accuracy, unsigned int decimals) {
unsigned int steps = 0; unsigned int steps = 5000, i, prefix;
double result, old; double result = 0, old = 1;
do { while( fabs(old - result) >= accuracy ) {
steps += stride; steps *= 2;
old = result; old = result;
result = integral(f, method, a, b, steps); result = integral(f, method, a, b, steps);
} while( fabs(old - result) >= accuracy ); }
for( decimals += 2, prefix = (int)result; prefix; prefix /= 10 )
decimals++;
printf("%.30f\n", old);
for( i = 0; i < decimals; i++ )
printf(" ");
printf("| could deviate from here\n%.30f\n", result);
return result; return result;
} }
...@@ -24,12 +32,21 @@ double f1(double x) { ...@@ -24,12 +32,21 @@ double f1(double x) {
return x * pow(M_E, -x); return x * pow(M_E, -x);
} }
#define PRINT_INTEGRAL(func, method, a, b, acc) (printf(#func " from " #a " to " \ #define PRINT_INTEGRAL(func, method, a, b, decimals) { \
#b " using %-19s %.11e", accurate_itegral(&func, &method, a, b, acc, STRIDE), \ double real = 0; \
#method " method:")) double _i = accurate_integral(&func, &method, a, b, \
pow(10, -1.0 * decimals), decimals); \
printf(#func " from " #a " to " #b " using %-19s %.11e (%.8e%%)\n", \
#method " method:", _i, fabs(_i - real)); \
}
int main(int argc, char *argv[]) {
if( argc != 2 ) {
printf("Usage: %s DECIMALS", argv[0]);
return 1;
}
int main(void) { PRINT_INTEGRAL(f1, gauss, 0, 2, atoi(argv[1]));
PRINT_INTEGRAL(f1, gauss, 0, 2, 1e-5);
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment