Message Boards Message Boards

0
|
9516 Views
|
5 Replies
|
9 Total Likes
View groups...
Share
Share this post:
GROUPS:

How do I fix this trapezoidal rule input?

Posted 10 years ago

The question is: "Attach Mathematica (or Matlab) code solving the problems listed below. Approximate the integral from 0 to 1 for e^(-(x^2)) using the trapezoidal rule with n=20, n=200, n=2000. Write down the approximate value you get here."

While the answer isn't so hard to find using wolfram, I'm having trouble writing the input. Using Wolfram's NIntegrate page, I tried the following:

NIntegrate[e^(-(x^2)), {x, 0, 1}, Method -> "TrapezoidalRule"]

I ended up with the error "The integrand e^-x^2 has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,0.5}}. >>" I also haven't figured out how to include n=20, 200, and 2000. What am I doing wrong? Thanks for the help in advance.

POSTED BY: Marshall G
5 Replies

You have to fix your input in the following way:

   In[214]:= NIntegrate[Exp[-(x^2)], {x, 0, 1}, Method -> "TrapezoidalRule"]
   Out[214]= 0.746824

In other words, in

NIntegrate[e^(-(x^2)), {x, 0, 1}, Method -> "TrapezoidalRule"]

you have not properly specified the exponent.

Exponential functions can be entered using the key sequence "Esc e e Esc"...

Further, you can use the options MaxRecursion and Points to calculate the integral with different numbers of sampling points.

In[232]:= Table[ NIntegrate[Exp[-(x^2)], {x, 0, 1}, MaxRecursion -> 0, Method -> {"TrapezoidalRule", "Points" -> n}], {n, {20, 200, 2000}}]
Out[232]= {0.746824, 0.746824, 0.746824}

If you want to see the sampling points use:

Needs["Integration`NIntegrateUtilities`"]
Table[ NIntegrateSamplingPoints@
      NIntegrate[Exp[-(x^2)], {x, 0, 1}, MaxRecursion -> 0, Method -> {"TrapezoidalRule", "Points" -> n}], {n, {20, 200, 2000}}]
POSTED BY: Anton Antonov

I'd go with an explicit summation using the trapezoidal rule. That way you need not figure out how to impose a specific sampling in NIntegrate. Can be done with Sum as below though certainly other variants are possible.

trapezoidal[func_, var_, p1_, p2_, n_] := With[{dx = (p2 - p1)/n},
  Sum[((func /. var -> N[j]) + (func /. var -> N[j - dx]))*dx/2, {j, 
    p1 + dx, p2, dx}]]

Examples:

Map[trapezoidal[Exp[-x^2], x, 0, 1, #] &, {20, 200, 2000}]
In[328]:= NIntegrate[Exp[-x^2], {x, 0, 1}]

(* Out[328]= 0.746824132812 *)

N[Integrate[Exp[-x^2], {x, 0, 1}]]

(* Out[334]= 0.7468241328121 *)
(* Out[333]= {0.74667083694, 0.74682259998, 0.746824117484} *)

Compare to NIntegrate and to N[] of the exact integral.

In[328]:= NIntegrate[Exp[-x^2], {x, 0, 1}]

(* Out[328]= 0.746824132812 *)

N[Integrate[Exp[-x^2], {x, 0, 1}]]

(* Out[334]= 0.7468241328121 *)

Be sure to acknowledge receiving assistance from Wolfram Community.

POSTED BY: Daniel Lichtblau

Take a quick look at your code inside your notebook. Do you see how "e" is in blue in your code? That means it isn't defined.

Lowercase "e" doesn't mean anything special. Mathematical Constants, like Mathematical Functions are capitalized. Replace "e" with "E"

NIntegrate[E^(-(x^2)), {x, 0, 1}, Method -> "TrapezoidalRule"]

POSTED BY: Sean Clarke

While you're at it, also try this:

WolframAlpha["Integrate e^(-(x^2)) from 0 to 1 using the Trapezoid Rule"]

This calls WolframAlpha and shows you what code you might use to solve this problem. You can see that it's possible to change the number of points used in the Trapezoid Rule.

There are more examples of this on Wolfram|Alpha's webpage:

http://www.wolframalpha.com/examples/NumericalIntegration.html

Here's how you can use Wolfram|Alpha to look at the 5 interval trapezoid rule for this integral:

http://www.wolframalpha.com/input/?i=5+interval+trapezoidal+rule+integrate+e%5E%28-%28x%5E2%29%29+on+%5B0%2C1%5D

POSTED BY: Sean Clarke
Posted 10 years ago

I've tried the methods in this thread and they've worked out smoothly. Thanks for the help, everyone!

POSTED BY: Marshall G
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract