MATLAB Answers

How do you create a composite function in matlab?

55 views (last 30 days)
Buttercup12
Buttercup12 on 28 Feb 2021
Answered: Steven Lord on 28 Feb 2021
If I have two functions, like
function a=func1(x)
and
function b=func2(q,p)
as examples. Is it possible to combine them to create a composite function like func1(func2(q)) = func1∘func2(q), and if so how do you do that?

Accepted Answer

Steven Lord
Steven Lord on 28 Feb 2021
f = @sin;
g = @cos;
% define it two different ways
fg = @(x) f(g(x));
h = @(x) sin(cos(x));
% Compare the two approaches
x = 0:0.1:2*pi;
max(abs(fg(x)-h(x)))
ans = 0
Or you could write a function file.
function y = myfun3(x)
y = func1(func2(x, 3)); % fixing p = 3
end

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by