clear all
syms x n m real
%This is the Fourier--Hermite series with normalized Hermite Funtions.
psi(n,x) = (hermiteH(n,x)*exp((-x^2)/2))/sqrt((2^n)*factorial(n)*sqrt(pi));
f(x)=sin(x); %Function that we want to approximate.
n = 0:20; %Change the upper bounds of n to get a more precise approximation.
%Series coefficients for the function.
coeffs = int(f(x)*psi(n,x),x,-inf,inf);
psi1 = psi(n,x);
%Check that the two vectors are the same size in order to take dot product.
size(coeffs);
size(psi1);
%Take the dot product in order to get the approximation of the function.
approximation = dot(coeffs,psi1);
%Compare approximation and exact function.
fplot(f(x),[-2*pi 2*pi],'LineWidth',2.5)
ax = gca;
set(ax,'FontName', 'Times New Roman', 'FontSize', 14)
hold on
fplot(approximation,'LineWidth',2.5)
legend('Sin(x)','Hermite Approximation', 'Location', 'southoutside')
title('Approximation of Sin(x) using Hermite Polynomial', 'FontName','Times New Roman', ...
'FontSize', 20)
xlabel('x')
ylabel('f (x)')