MATLAB: One function-handle for one function-handle f with g = doublethefun(f) as separate function file

matlab function handle-function

Hi all,
I need your help.
here is a question: function doublethefun should be defined as separate function file and should be so defined like this:
g = doublethefun(f)
should be one function-handle for one function-handle f and
g(x) = 2*f(x)
The function g should represent also the double of f
I'm quite new in this field and don't know how to do it.
My try looks like this:
function f = doublethefun(ff, x)
f= 2*ff(x);
end

Best Answer

  • You want doublethefun to return a function handle whose output is double the output of the function handle it receives as input? You're close. As written your code evaluates the input function handle, doubles the value it returns, and returns the numeric value as output. In addition, it doesn't have the syntax your homework assignment requires. The assignment states doublethefun should accept only one input, and you've defined it to accept two.
    Since you've made an effort, and since it's hard to offer a hint that doesn't just show you the answer, I'll show you.
    function g = doublethefun(f)
    g = @(x) 2*f(x);
    end
    g is a function handle (the @(x) part defines it to be an anonymous function that accepts one input) that calls f (the f(x) part), multiplies the output of that call to f by 2 (the 2* part), and returns the result as output. Try:
    mydoublefun = doublethefun(@(x) x.^2)
    mydoublefun(3) % Returns 2*(3.^2) = 18