Search code examples
matlabnumerical-integration

Is there a way to integrate the log of a function, f(x) in Matlab without defining eg l = log(f(x)?


I have the following code:

x = 0:0.001:2.5;
gamma_l = @(x) 2*x;

And I want to integrate the following:

integral( log(gamma_l), 0 , 0.6 )

But it gives me the error:

Undefined function 'log' for input arguments of type 'function_handle'.

I know that I could just define:

gamma_l_l = @(x) log(2*x);
integral( gamma_l_l, 0 , 0.6 )

Because it works in this way. However, I would like to know why the first case does not work. And if there is a way to integrate the function without defining a new function.


Solution

  • Your variable gamma_l is an anonymous function, and the log function is not designed to accept function handles as an input. Instead, you need to define a second anonymous function that evaluates gamma_l for a given value, then passes the numeric result to log, like so:

    result = integral(@(x) log(gamma_l(x)), 0, 0.6);