A function handle constructed by a symbolic function and another function handle

Hello,
I have a simple problem but unfortunately could not figure out how to fix it. Consider the following
syms sigma(x)
par=sym('par', [1 2]);
sigma(x)=par(1)+par(2)*x;
f=@(x)x;
Now, imagine I want to construct a function handle H using f and sigma as bellow:
sigma = matlabFunction(sigma);
H = @(x,par) sigma(x,par)+f(x);
Now, you see my problem. For matlab H has 2 inputs: x and par. But, I am struggling to define H(x,par(1),par(2)) = sigma(x,par(1),par(2))+f(x)
Thanks for your help in advance!
Babak

 Accepted Answer

You can do this without symbolic variables.
The definition is correct, just call the function with appropriate inputs -
sigma = @(x,par) par(1)+par(2)*x;
f=@(x)x;
H = @(x,par) sigma(x,par)+f(x)
H = function_handle with value:
@(x,par)sigma(x,par)+f(x)
H(1,[1 2])
ans = 4

4 Comments

Thanks Dyuman,
Unfortunately, I cannot define sigma as a function handle initially (the reason is that I need to work on its derivatives first ...). So, I am seeking an answer to my question which considers sigma being defined as a
symbolic function (as I defined). When I convert sigma to a function handle using matlabFunction then I get
@(x,par1,par2)par1+par2.*x
Unfortunately, this does not let me to do what I intend to do
I see. In that case, modify the symbolic function to anonymous function conversion as follows -
syms sigma(x)
par=sym('par', [1 2]);
sigma(x)=par(1)+par(2)*x;
f=@(x)x;
%Generate function with vector input arguments
sigma = matlabFunction(sigma,"Vars",{x par})
sigma = function_handle with value:
@(x,in2)in2(:,1)+in2(:,2).*x
H = @(x,par) sigma(x,par)+f(x)
H = function_handle with value:
@(x,par)sigma(x,par)+f(x)
H(1,[1 2])
ans = 4
This is a wonderful answer!
Thanks for your great help!

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!