Last active
December 9, 2023 17:24
-
-
Save assyrianic/e1aca3fee4e400ba699ea8c31562838f to your computer and use it in GitHub Desktop.
simpson's rule in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
long double adaptive_simpsons(long double (*f)(long double), long double a, long double b, long double epsilon) { | |
long double c = (a + b) / 2.0L; | |
long double h = b - a; | |
long double fa = f(a); | |
long double fb = f(b); | |
long double fc = f(c); | |
long double integral = (h / 6.0L) * (fa + 4.0L * fc + fb); | |
long double d = (a + c) / 2.0L; | |
long double e = (c + b) / 2.0L; | |
long double fd = f(d); | |
long double fe = f(e); | |
long double integral_left = (h / 12.0L) * (fa + 4.0L * fd + fc); | |
long double integral_right = (h / 12.0L) * (fc + 4.0L * fe + fb); | |
if( fabs(integral - integral_left - integral_right) < epsilon ) { | |
return integral_left + integral_right; | |
} else { | |
return adaptive_simpsons(f, a, c, epsilon / 2.0L) + adaptive_simpsons(f, c, b, epsilon / 2.0L); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment