# Multiplying two functions in MATLAB

I want the product of two functions $f(x),g(x)$

$$f(x) = x + \sin(x)\\ g(x) = x - \sin(x)$$

then

$$f(x)g(x) = x^2 - \sin^{2}(x)$$

How could I compute the product of two functions?.

I tried searching through Google which says to use inline or something like this

>> f = @(x) x - sin(x)
f =

@(x) x - sin (x)

>> g = @(x) x + sin(x)
g =

@(x) x + sin (x)

>> h = @(x) f(x)*g(x)
h =

@(x) f (x) * g (x)


But could not get the product.

Also, will the syntax change if I give the functions as the arguments of another function say $fun$ and will require products of functions inside the function $fun$?

I am using MATLAB 2013a.

• I just tried it, and the snippet of code works for me >> h(1) gives 0.29193 and >> 1 - sin(1)^2 gives 0.29193. I have Octave, but it should not be different. – nicoguaro May 31 '17 at 15:23
• For the more general case, it might be better to use chebfun. – nicoguaro May 31 '17 at 15:31

If you want a purely numerical result the MATLAB trick is to use a dot . like: f(x).*g(x) It takes care of pointwise multiplication for matrices.
syms x