How to resolve the algorithm Numerical integration step by step in the Mathematica/Wolfram Language programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Numerical integration step by step in the Mathematica/Wolfram Language 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 Mathematica/Wolfram Language programming language

The provided Wolfram language code defines five functions for numerical integration: leftRect, rightRect, midRect, trapezium, and simpson. All these methods are used to approximate the definite integral of a function f over an interval [a, b] by dividing the interval into N subintervals and applying a specific rule to approximate the area under the curve of f.

1. leftRect:

  • This function implements the left-hand Riemann sum, which approximates the integral by summing the area of rectangles whose heights are the function values at the left endpoints of the subintervals and whose widths are equal to the subinterval length.

2. rightRect:

  • Similar to leftRect, but it uses right endpoints of the subintervals for the rectangle heights.

3. midRect:

  • This function uses the midpoint of each subinterval as the height of the rectangle.

4. trapezium:

  • The trapezoidal rule approximates the integral by summing the areas of trapezoids whose bases are the subinterval lengths and whose heights are the average of the function values at the endpoints of each subinterval.

5. simpson:

  • The Simpson's rule approximates the integral using a weighted sum of function values at specific points within each subinterval. It is generally more accurate than the other methods.

All functions take the following inputs:

  • f: The function to be integrated.
  • a: The lower limit of integration.
  • b: The upper limit of integration.
  • N: The number of subintervals for the approximation.

For each function, the code sets up a loop to iterate over the subintervals, calculates the corresponding area based on the specific rule, and accumulates the results into a sum. Finally, it returns the estimated integral value multiplied by the subinterval width dx.

Source code in the wolfram programming language

leftRect[f_, a_Real, b_Real, N_Integer] := 
 Module[{sum = 0, dx = (b - a)/N, x = a, n = N} ,
  For[n = N, n > 0, n--, x += dx; sum += f[x];];
  Return [ sum*dx ]]

rightRect[f_, a_Real, b_Real, N_Integer] := 
 Module[{sum = 0, dx = (b - a)/N, x = a + (b - a)/N, n = N} ,
  For[n = N, n > 0, n--, x += dx; sum += f[x];];
  Return [ sum*dx ]]

midRect[f_, a_Real, b_Real, N_Integer] := 
 Module[{sum = 0, dx = (b - a)/N, x = a + (b - a)/(2 N), n = N} ,
  For[n = N, n > 0, n--, x += dx; sum += f[x];];
  Return [ sum*dx ]]

trapezium[f_, a_Real, b_Real, N_Integer] := 
 Module[{sum = f[a], dx = (b - a)/N, x = a, n = N} ,
  For[n = 1, n < N, n++, x += dx; sum += 2 f[x];];
  sum += f[b];
  Return [ 0.5*sum*dx ]]

simpson[f_, a_Real, b_Real, N_Integer] := 
 Module[{sum1 = f[a + (b - a)/(2 N)], sum2 = 0, dx = (b - a)/N, x = a, n = N} ,
  For[n = 1, n < N, n++, sum1 += f[a + dx*n + dx/2]; 
   sum2 += f[a + dx*n];];
  Return [(dx/6)*(f[a] + f[b] + 4*sum1 + 2*sum2)]]


  

You may also check:How to resolve the algorithm Mouse position step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Floyd's triangle step by step in the TXR programming language
You may also check:How to resolve the algorithm Metered concurrency step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Set consolidation step by step in the Racket programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the MAXScript programming language