Vector calculation of HermiteH Polynomials

12 views (last 30 days)
The function im trying to code is hermiteH and it is as follows:
note: this is Wolfram syntax.
I want to create a vector of n hermite polynomials, and then each one to go through, some sort of matrix operation comes to mind but the implementation eludes me.
note: taken from wikipedia: Hermite polynomials.
My goal is to get something like be run through.
In the example ill get with .
My current Code:
Hk = @(k,x) hermiteH(k,x/sqrt(2))*2.^(-k/2);
k = 2;
hPoly = zeros(1,k+1);
for i=1:k
hPoly = hPoly + Hk(i,[0 1 2]);
end
hPoly
hPoly = 1×3
-1 1 5
The code is Not right.
  1. The MATLAB hermiteH is the "physicist's Hermite polynomials" (i.e. ) and not the "probabilist's Hermite polynomials" (i.e. ) I'm looking for but I "fixed" it with the extra "" term, but I'm not sure.
  2. The result is wrong and I cant get it right.
  3. The end goal is to implement the Wolfram syntax into a functional MATLAB code with k and x variables. note: to make multiple polynomials in one vector with the hermiteH command it requires k to be a vector (i.e. 1:7) and x to be symbolic so basically k is a vector and x is a vector and make the implementation versetile.
  4. The problem I think is the first term in the result adds the first Hermite k times, the second Hermite k-1 times and so on...
Any advice on the matter is much obliged.
  4 Comments
John D'Errico
John D'Errico on 17 Jan 2023
conv is the classic MATLAB trick for multiplication of polynomials or many other things. You can use it to build polynomials, etc. I even used it in my VPI toolbox to perform multiplication of variable precision numbers. For example, suppose you want to multiply a pair of polynomials? I'll do it using syms first.
Suppose I want to multiply (x+1) by (x^2 - 3*x - 2)? Firt, in symbolic form so we can compare the results.
syms x
expand((x+1)*(x^2 - 3*x - 2))
ans = 
But now recognize that these polynomials can be represented as a list of coefficients in standard MATLAB form.
conv([1,1],[1 -3 -2])
ans = 1×4
1 -2 -5 -2
Do you see the multiplication worked perfectly? As a result, we now have a list of coefficients of the product polynomial. Try it with other products of polynomials, and it always works, at least as long as there are no problems with the precision of the coefficients in double precision arithmetic.
Why does it work? Think about what conv does. Expand it out, and you will see that convolution does exactly what you want with the product of those polynomials. (Conv is a tremendously valuable tool, useful in many areas of mathematics. I cannot begin to count the number of ways I have used it over the years.)
Alex Rudyak
Alex Rudyak on 18 Jan 2023
@John D'Errico I've completly missed this one, thank you it's clearer now, I'll note it down for future use

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 17 Jan 2023
Edited: John D'Errico on 17 Jan 2023
First, the two sets of polynomials are effectively the same thing, but for a transformation. As you said, the two are just orthogonal with respect to a subtly different version of the same exponential form. Here, for example, is the degree 3 polynomial, and that matches what wikipedia shows. First, note the normalization. These are not normalized to have the coefficient of the highest degree term 1.
syms x
P3 = hermiteH(3,x)
P3 = 
This is what Wikipedia refers to as a physicist's polynomials. They are orthognal wrt exp(-x^2). So effectively derived from an erf, which makes sense in context. For example...
P2 = hermiteH(2,x)
P2 = 
then we would expect to find this integral to be zero.
int(P2*P3*exp(-x^2),[-inf,inf])
ans = 
0
Every once in a while something works. ;-) But not everything. I prefer my orthognal polynomials to be normalized to have unit norm too. And that would mean this next would yield 1. Such is life. Just a scale factor, and I can live with it.
int(P3*P3*exp(-x^2),[-inf,inf])
ans = 
The Probabilists form will be orthogonal to exp(-x^2/2). We should recognize this from the Gaussian (Normal) distribution PDF. And we can get one from the other.
S2 = sqrt(sym(2));
hermiteH(3,x/S2)
ans = 
But we need to normalize it, so the highest order term has coefficient 1, at least if you want to match the table. And to do that, you just divide by 2^(n/2), So I'll write a little function that does it:
hermiteProb = @(x,n) simplify(hermiteH(n,x/S2)/S2^n);
P2prob = hermiteProb(x,2)
P2prob = 
P3prob = hermiteProb(x,3)
P3prob = 
int(P2prob*P3prob*exp(-x^2/2),[-inf,inf])
ans = 
0
Again, they are orthogonal wrt the indicated weight function. (WHEW! Got lucky again!) Though they are not normalized so the integral is 1, as would be expected if they were orthonormal. Oh well, I guess nothing is ever perfect. Still they have the form you desire.
int(P3*P3*exp(-x^2/2),[-inf,inf])
ans = 
Still not my favorite normalization. Oh well.
Finally, if you want to evaluate these polynomials at a list of points, that is pretty easy. You could convert them to a function, using matlabFunction.
P3probfun = matlabFunction(P3prob)
P3probfun = function_handle with value:
@(x)x.*-3.0+x.^3
P3probfun(-1.5:.5:1.5)
ans = 1×7
1.1250 2.0000 1.3750 0 -1.3750 -2.0000 -1.1250
Or plot it.
X = linspace(-5,5);
plot(X,P3probfun(X),'-')
grid on
  4 Comments
Alex Rudyak
Alex Rudyak on 18 Jan 2023
Edited: Alex Rudyak on 18 Jan 2023
Hey just a quick update,
I've calculated the polynomials and just matlab functioned it all like so:
syms x
S2 = sqrt(sym(2));
hermiteProb = @(x,n) simplify(hermiteH(n,x/S2)/S2^n);
P0prob = hermiteProb(x,0);
P1prob = hermiteProb(x,1);
P2prob = hermiteProb(x,2);
P3prob = hermiteProb(x,3);
P4prob = hermiteProb(x,4);
P5prob = hermiteProb(x,5);
P6prob = hermiteProb(x,6);
P7prob = hermiteProb(x,7);
PNprobVec = matlabFunction([P0prob P1prob P2prob P3prob P4prob P5prob P6prob P7prob]);
x = [-0.5 -0.1 0 0.1 0.5 1 2 3];
g = PNprobVec(x);
g = reshape(g(2:end),8,7);
PNprobMat = table(g(:,1), g(:,2), g(:,3), g(:,4), g(:,5), g(:,6), g(:,7));
PNprobMat.Properties.VariableNames = ["P1 Answer","P2 Answer","P3 Answer","P4 Answer","P5 Answer","P6 Answer","P7 Answer"]
PNprobMat = 8×7 table
P1 Answer P2 Answer P3 Answer P4 Answer P5 Answer P6 Answer P7 Answer _________ _________ _________ _________ _________ _________ _________ -0.5 -0.75 1.375 1.5625 -6.2812 -4.6719 40.023 -0.1 -0.99 0.299 2.9401 -1.49 -14.551 10.395 0 -1 0 3 0 -15 0 0.1 -0.99 -0.299 2.9401 1.49 -14.551 -10.395 0.5 -0.75 -1.375 1.5625 6.2812 -4.6719 -40.023 1 0 -2 -2 6 16 -20 2 3 2 -5 -18 -11 86 3 8 18 30 18 -96 -396
Now I need to apply an integration on the matrix, but thats a question for another day.
Thank you for your time!

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!