Pass a symbolic function as input to a function without passing its inputs

3 Ansichten (letzte 30 Tage)
Hello,
I have a question and I believe the best is to go to an example from the begining. Consider the following
syms x y
syms mu(x,y)
syms par [1 3]
mu(x,y)=par(1)*x^2*y+par(2)*x*y+par(3);
test(mu,par)
where test.m is defined as bellow
function test(f,par)
gradient(f,par); %Works
diff(f,x) ; %does not work
end
If I try g = test(mu,par) it works well. However, I cannot calculate stuff like diff(f,x) simply because inside the 'test.m' function it is not defined. Of course, I could pass x,y as nputs of test.m as bellow
function test(f,par,x,y)
gradient(f,par); %Works
diff(f,x) %Works
end
My question: Is there a way not to pass parameters (like vector par above) and state variables (like x and y above) and only pass the function mu? Of course, inside the test.m x and y are not recognized. However, there is a nice mtlab command 'argnames' which let us know the inputs of a function. So, I am wondering whether I can only pass the function 'mu' to test.m and then inside test.m use the command 'argnames' to fins the inputs of 'mu'. Is this possible? I mean to say that I wish to have a test.m to be defined as bellow
function test(f,par)
USE ARGNAMES COMMAND TO INVIKE THE INPUTS OF f. THEN DO THE FOLLOWING
diff(f,x)
end

Antworten (1)

Matt J
Matt J am 26 Dez. 2022
Bearbeitet: Matt J am 26 Dez. 2022
You seem to have answered your own question...
function [g,d]=Test(f,par)
g=gradient(f,par);
args=argnames(f);
d=diff(f,args(1)) ;
end

Community Treasure Hunt

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

Start Hunting!

Translated by