Problem 581. Function composition
Write a function that accepts two function handles f and g and returns the composition h. That is,
h = (f o g)(x) = f(g(x))
Example:
>> f = @(x)x^2; >> g = @(x)x+1; >> h = composeFcn(f,g); >> h(3) ans = 16
because (3+1)^2 = 16.
Solution Stats
Problem Comments
-
8 Comments
My code runs fine on my local machine, but I get error messages when I try it here.
Error using compose (line 85) First argument must be a string array, character vector, or cell array of character vectors. Error in TestPoint1 (line 3) h = compose(f,g);
To the creator of this problem: please rename this function in your test suite. "Compose" is already a built-in MATLAB function.
Changed the function name to composeFcn to avoid clashing with the compose built-in function. Let me know if you're still having trouble!
I guess the name needed changing, but be aware that if we ever re-score old entries, everyone who passed before the name change will now fail, since they will now be calling the wrong function.
Thanks, David Hruska!
function h = composeFcn(f,g)
syms x
x=g(x);
h=matlabFunction(f(x));
end
I am so confused where i was wrong?
@善翔 韩, syms is a part of symbolic toolbox and toolboxes are not available on cody.
Solution Comments
Show commentsProblem Recent Solvers240
Suggested Problems
-
Find the longest sequence of 1's in a binary sequence.
6231 Solvers
-
2409 Solvers
-
10216 Solvers
-
Construct a string from letters and counts
142 Solvers
-
331 Solvers
More from this Author2
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!