From 11335b43617c1e3408f12a2b68d8094d09a10528 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Mon, 16 Jan 2023 12:17:32 +0000 Subject: [PATCH] FP: Aula09 - Ex9 added --- 1ano/fp/aula09/integrate.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1ano/fp/aula09/integrate.py diff --git a/1ano/fp/aula09/integrate.py b/1ano/fp/aula09/integrate.py new file mode 100644 index 0000000..7101390 --- /dev/null +++ b/1ano/fp/aula09/integrate.py @@ -0,0 +1,28 @@ +import sys +import math + + +def integrate(f, a, b, n): + """The integral of f(x) for x between a and b. + Aproximated using the trapezoidal rule with n uniform subintervals.""" + assert n >= 1 + return ((b - a) / 2 * n * (f(a) + sum(2 * f(a + (b - a) * i / n) for i in range(1, n)) + f(a + (b - a)))) / (n ** 2) + + +def example(n): + a = integrate(lambda x: (x - 2) / (x + 3), 0, 1, n) + return a + + +# You may define auxiliary functions here. + + +# Do not change the code below! +def evalPrint(expression): + # Evaluate and print an expression and its result. + result = eval(expression) + print("{}\n--> {!r}".format(expression, result)) + + +if __name__ == "__main__": + evalPrint(" ".join(sys.argv[1:]))