0
$\begingroup$

I have a function called psi0 that looks like this: psi0 := E^(-(x - 10)^2/10) and I'm trying to integrate it numerically from 0 to a variable L. However, I keep getting and error saying

NIntegrate::inumr: The integrand e^(-(1/10) (-10+x)^2) has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,10.}}.

I'm using NIntegrate like this: NIntegrate[psi0[x], {x, 0, L}] when I get this error but if I do this: NIntegrate[E^(-(x - 10)^2/10), {x, 0, L} it works perfectly fine. What's going wrong with the function?

$\endgroup$
4
  • $\begingroup$ You write psi0 := then call it using psi0[x] ? $\endgroup$
    – Nasser
    May 4 at 23:20
  • $\begingroup$ Sorry I'm new to mathematica, how would I fix this? $\endgroup$
    – Anik Patel
    May 4 at 23:24
  • $\begingroup$ Please see below. $\endgroup$
    – Nasser
    May 4 at 23:25
  • $\begingroup$ e is not defined as a System` symbol, but E is, representing the exact value of the base of the natural logarithm. The letter e appears in the warning message, but the letter E appears in the code that works perfectly fine. $\endgroup$
    – Michael E2
    May 8 at 17:07

1 Answer 1

2
$\begingroup$

You had this

L = 10;
psi0 := E^(-(x - 10)^2/10)
NIntegrate[psi0[x], {x, 0, L}]

enter image description here

You can't call psi0[x] when the function is defined as psi0:= E^(-(x - 10)^2/10) . You need to define the function as actually taking an argument x for Mathematica to find it and use it.

I set L=10 since you did not show what L was.

Try the following

Clear["Global`*"]
L = 10;
psi0[x_] := E^(-(x - 10)^2/10)
NIntegrate[psi0[x], {x, 0, L}]

(*2.80247*)

Btw, you do not numerical integrate on this, Mathematica can solve it analytically

Clear["Global`*"]
psi0[x_] := E^(-(x - 10)^2/10)
sol = Integrate[psi0[x], {x, 0, L}]

Mathematica graphics

sol /. L -> 10

Mathematica graphics

N[%]

Mathematica graphics

$\endgroup$
8
  • $\begingroup$ What does Clear["Global*"]` do? $\endgroup$
    – Anik Patel
    May 4 at 23:26
  • $\begingroup$ @AnikPatel it just clears all your variables and other definitions from the notebook so they do not mess up anything in the following calculation. $\endgroup$
    – Nasser
    May 4 at 23:27
  • $\begingroup$ How can I make the integral a function? Like if have a function f(n,x) and I want g(n) to be the integral of f(n,x)dx from 0 to L? $\endgroup$
    – Anik Patel
    May 5 at 0:00
  • $\begingroup$ @AnikPatel if I understand you write, may be like this? !Mathematica graphics $\endgroup$
    – Nasser
    May 5 at 0:04
  • $\begingroup$ I got it, I wasn't clearing the function and it was screwing me up $\endgroup$
    – Anik Patel
    May 5 at 0:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.