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

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


$$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.

  • 1
    $\begingroup$ 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. $\endgroup$ – nicoguaro May 31 '17 at 15:23
  • 1
    $\begingroup$ For the more general case, it might be better to use chebfun. $\endgroup$ – 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.

If you want it symbolically you can use something like:

syms x
f = x + sin(x);
g = x - sin(x);
equ = simplify(f*g); 
| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.