0
$\begingroup$

this is the question. Develop a code to perform a numerical integration for the following functions using Simpson’s rule. You must implement the algorithm in the form of a Module[ ]. Partition the domain of integration [𝑎, 𝑏] into 𝑁𝑥 = 250 subintervals.

this is the equation

𝑓 (𝑥) = sin 𝑥 tanh 𝑥 for 𝑥 ∈ [0, 𝜋].

so I did the first coding

f[x_] := Sin[x]*Tanh[x];
Integrate[f[x], {x, a, b}];
a = 0.0;
b = 1.0*Pi;
simpson[a_, b_, f_] := Module[{},
  Nx = 250;
  Deltax = (b - a)/Nx;
  x[0] = a;
  x[i_] := x[0] + i*Deltax;

then I dont know what to do after this, and I'm not sure if my coding is right or wrong. anyone help

$\endgroup$
  • 1
    $\begingroup$ (in the interest of not doing your homework for you...). A very procedural way of solving this is to define a "local" variable in the first argument of Module, and then use a loop (e.g., Do[] ) to increment that value (rather than defining a set of delayed rules). A more functional way might use a function map over a list of possible x values. $\endgroup$ – Joshua Schrier May 6 at 14:59
  • 4
    $\begingroup$ Simpson's rule has been often discussed and often implemented on this site already $\endgroup$ – Michael E2 May 6 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.