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
Show
5 older comments
yurenchu
on 27 Apr 2017
Thanks, David Hruska!
善翔 韩
on 28 Aug 2022
function h = composeFcn(f,g)
syms x
x=g(x);
h=matlabFunction(f(x));
end
I am so confused where i was wrong?
Dyuman Joshi
on 28 Aug 2022
@善翔 韩, syms is a part of symbolic toolbox and toolboxes are not available on cody.
Solution Comments
Show commentsGroup

KR SA Joint & Collaborative Cody Event
- 35 Problems
- 2 Finishers
- Convert from Fahrenheit to Celsius
- Area of an equilateral triangle
- Find the sum of all the numbers of the input vector
- Function composition
- Sunday bonus
- Sum of odd numbers in a matrix
- Find the position of first minimum value in an integer array with numbers
- Sum of first n positive integers
- Determinant of a 3x3 Matrix
- Draw a '0' in a one matrix!
- Mersenne Primes
- Select every other element of a vector
- Find a Pythagorean triple
- Is this triangle right-angled?
- ~~~~~~~ WAVE ~~~~~~~~~
- Create an n-by-n null matrix and fill with ones certain positions
- Find max
- Calculate the derivative of a polynomial
- Extract part elements of matrix into one new matrix
- Triangle sequence
- 2048 Next Move
- Which doors are open?
- matrix zigzag
- Which values occur exactly three times?
- Binary numbers
- Counting Money
- Shuffle
- With apologies to William Blake
- MatCAT - Reconstruct X from Its X-rays
- Solve an ODE: equation B
- Longest run of consecutive numbers
- Vector push
- Five-dimensional maze
- The sliding puzzle: 3D
- One track five lanes
Problem Recent Solvers246
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!