1

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:

  1. If it's possible, in Hermite function I want from this output [8 0 -12 0] make this output 8x^3 - 12x. This output I will able to integrate. How can I do this?
  2. 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!

lizsav
  • 75
  • 4

1 Answers1

1

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.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • Thank you very much! But I don't know why in my case it doesn't work. I use the same construction: `p = [1 2]; f = @(x) polyval(p, x);` and in Matlab Workspace I actually have a function f, but value is `@(x)polyval(p,x)`. And in fact it doesn't work like a function. I also tried to add it in my `Hermite` function and it doesn't work too. – lizsav May 18 '22 at 22:25
  • Can it be because `polyval()` returns integer? – lizsav May 18 '22 at 22:28
  • No, `polyval` does not necessarily return integers. And if you type `p = [1 2]; f = @(x) polyval(p, x);` then `f` does work like a function. Try `f(3)` or `integral(f, 0, 3)` – Luis Mendo May 18 '22 at 22:37