How to resolve the algorithm Numerical integration step by step in the Common Lisp programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Numerical integration step by step in the Common Lisp programming language
Table of Contents
Problem Statement
Write functions to calculate the definite integral of a function ƒ(x) using all five of the following methods: Your functions should take in the upper and lower bounds (a and b), and the number of approximations to make in that range (n). Assume that your example already has a function that gives values for ƒ(x) . Simpson's method is defined by the following pseudo-code:
Demonstrate your function by showing the results for:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Numerical integration step by step in the Common Lisp programming language
Source code in the common programming language
(defun left-rectangle (f a b n &aux (d (/ (- b a) n)))
(* d (loop for x from a below b by d summing (funcall f x))))
(defun right-rectangle (f a b n &aux (d (/ (- b a) n)))
(* d (loop for x from b above a by d summing (funcall f x))))
(defun midpoint-rectangle (f a b n &aux (d (/ (- b a) n)))
(* d (loop for x from (+ a (/ d 2)) below b by d summing (funcall f x))))
(defun trapezium (f a b n &aux (d (/ (- b a) n)))
(* (/ d 2)
(+ (funcall f a)
(* 2 (loop for x from (+ a d) below b by d summing (funcall f x)))
(funcall f b))))
(defun simpson (f a b n)
(loop with h = (/ (- b a) n)
with sum1 = (funcall f (+ a (/ h 2)))
with sum2 = 0
for i from 1 below n
do (incf sum1 (funcall f (+ a (* h i) (/ h 2))))
do (incf sum2 (funcall f (+ a (* h i))))
finally (return (* (/ h 6)
(+ (funcall f a)
(funcall f b)
(* 4 sum1)
(* 2 sum2))))))
You may also check:How to resolve the algorithm Factors of a Mersenne number step by step in the Ring programming language
You may also check:How to resolve the algorithm Multiple distinct objects step by step in the Groovy programming language
You may also check:How to resolve the algorithm Make directory path step by step in the zkl programming language
You may also check:How to resolve the algorithm Long literals, with continuations step by step in the Delphi programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the Amazing Hopper programming language