Can you help me? Task: Implement a function (operator) that takes as an argument a…
- User
Can you help me? Task: Implement a function (operator) that takes as an argument a function of one variable f and returns a function of one variable that calculates (numerically) the expression: the integral from 0 to x of the function f(t). You can use the trapezoid method with a constant step. Then optimize the function using memoization. When optimizing, assume that the resulting primitive will be used to plot the graph (i.e., called repeatedly at different points). Compare using 'time' time of calculation with and without memoize My code, I implemented only integral and some simple functions for comparing: (defn f-linear [x] x) ; f(x) = x (defn f-square [x] (Thread/sleep 10) (* x x)) ; f(x) = x2 (defn f-polinom [x] (+ ( 2 x x) ( 3 x) 1)) ; f(x) = 2 * x**2 + 3 * x + 1 (defn f-sin [x] (Math/sin x)) ; f(x) = sin(x) (defn f-exponential [x] (Math/exp x)) ; f(x) = e^x
(defn trapezoid [f a b] ; h = b - a (* (* (+ (f a) (f b)) (- b a)) 0.5) )
(defn integral [f a b h] (if (< a b) (+ (trapezoid f a (+ a h)) (integral f (+ a h) b h) ) 0 ) )
- Assistant