I want to make a function from the output of Matlab Hermite function (for example, if we had an output from Hermite function [8 0 -12 0]
it would be 8x^3 - 12x
polynomial) and then integrate this function using the Simpson's 3/8 Rule.
I have already created a function in Matlab that integrate any function using this rule and also I have created function that returns coefficients of Hermite's polynomial (with the recursion relation) in the vector form.
My questions:
- If it's possible, in Hermite function I want from this output
[8 0 -12 0]
make this output8x^3 - 12x
. This output I will able to integrate. How can I do this? - Can I combine these two functions and integrate Hermite's polynomial without convention the output of the first function?
Code of Hermite polynomial function, where n is the order of the polynomial:
function h = hermite_rec(n)
if( 0==n ), h = 1;
elseif( 1==n ), h = [2 0];
else
h1 = zeros(1,n 1);
h1(1:n) = 2*hermite_rec(n-1);
h2 = zeros(1,n 1);
h2(3:end) = 2*(n-1)*hermite_rec(n-2);
h = h1 - h2;
end
Code of Simpson function, that integrate function using the Simpson 3/8 Rule. a is a lower limit of integral, b is a upper limit of integral:
n = 3;
h = (b-a)/(3*n); %3h = (b-a)/n
IS2=0;
for i=1:n
IS2 = IS2 (f(a (3*i-3)*h) 3*f(a (3*i-2)*h) 3*f(a (3*i-1)*h) f(a (3*i)*h))*3*h/8;
end
end
Thank you for any advice!
CodePudding user response:
To create a polynomial function given its coefficients, you can use polyval
(see also anonynmous functions):
p = [1 2]; % example. This represents the polynomial x 2
f = @(x) polyval(p, x); % anonymous function of x, assigned to function handle f
Now f
is a function that you can integrate numerically.
If you want to include this directly as part of your Hermite
function, just add something like this at the end:
h = @(x) polyval(p, x);
Then the Hermite
function will return a function (handle) representing the Hermite polynomial.