Integral/Calc issues: normalizing wave function
4 views (last 30 days)
Show older comments
Corey Manring
on 23 Apr 2016
Answered: John D'Errico
on 23 Apr 2016
I'm trying to normalize a harmonic oscillator wave function. I'm getting multiple error codes, mostly involving the integral (see below code).
syms hbar a h m k y x N w v
hbar = h/(2*pi);
a= (hbar^0.5)/(m^0.25*k^0.25);
w = (k^0.5)/(m^0.5);
y=x/a;
%normalization for V=0
psi = hermiteH(0,y).*exp(((-y).^2)./2);
f = @(y) psi*psi;
F = integral(f,-Inf,Inf);
N=sqrt(1./F)
Error using integralCalc/finalInputChecks (line 511) Input function must return 'double' or 'single' values. Found 'sym'.
Error in integralCalc/iterateScalarValued (line 315) finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132) [q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 103) [q,errbnd] = vadapt(@minusInfToInfInvTransform,interval);
Error in integral (line 88) Q = integralCalc(fun,a,b,opstruct);
Error in Untitled (line 12) F = integral(f,-Inf,Inf);
I have a feeling its simply just the integral. But I don't understand the error code! Thank you!
0 Comments
Accepted Answer
John D'Errico
on 23 Apr 2016
You need to understand functions and function handles, and the difference between symbolic tools and numerical tools.
integral is a numerical tool. It performs numerical integration. NO parameters in such a function can be symbolic. Anyway, numerical integration with infinite limits can be a risky thing, because subdividing infinite intervals is always a problem. How, for example, do you find the point midway in the interval [-inf,inf]?
(-inf + inf)/2
ans =
NaN
Next, when you define a function like this:
f = @(y) psi*psi;
MATLAB does not recognize that psi is actually a function of y. If psi is a function of y, then write it as
f = @(y) psi(y)*psi(y);
However, as you have written it, psi is a symbolic variable, not truly a function of any input. So this is not a function, even though you may choose to think of it as such:
psi = hermiteH(0,y).*exp(((-y).^2)./2);
So, you have quite a few problems in the code you wrote. The main problem is if you need to work with symbolic parameters, then you need to use int, not integral.
0 Comments
More Answers (1)
Steven Lord
on 23 Apr 2016
The integral function performs numeric integration. The int function performs symbolic integration. Alternately, if you want to use integral you will need to convert your symbolic expression into something that can be evaluated numerically
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!