How can you use symbolic functions as inputs and outputs of regular matlab functions

80 次查看(过去 30 天)
I want to use a symbolic expression as an input to a regular old matlab function and I want the matlab function to return a different symbolic function. For example, I want to be able to go:
syms x;
f(x)=sin(x);
new_function=function_I_will_write(f);
And then new_function(x) will output the symbolic variable x or something.
  1 个评论
Matt Grant
Matt Grant 2017-9-25
I'm finding that passing symbolic functions only works if you use the same name for the symbolic variable both inside and outside the function.
For example, the function I'm writing is supposed to output a symbolic function g(T), given some input function f(T). If I go:
syms T;
f1(T)=T;
f2(T)=my_routine(f1);
Where my_routine is:
function g = my_routine(f)
syms T;
g(T)=1+f(T)
end
then everything works fine. Ie, the output is:
f2(T) =
T + 1
But if I try to make the output a function of something other than T, such doing:
syms x;
f1(x)=x;
f2(x)=my_routine(f1);
then f2(x) comes out as a function of T instead of x, ie, I get:
f2(x) =
T + 1

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2017-9-22
In cases where function_I_will_write never compares the input to anything (e.g., tests for negative), and does not specifically test for numeric data types, it is common for passing symbolic expressions to "just work". There are cases where there are conflicts between numeric routines and symbolic routines. For example, if you have
syms x;
f(x)=sin(x);
X = sym('X', [1 10]);
result = my_routine( f(X) )
then you would have problems if my_routine is, for example,
function M = my_routine( v )
M = max( diff(v) );
because diff() of symbolic values is differentiation whereas the routine is expecting to do numeric differences equivalent to v(2:end) - v(1:end-1) for vectors.
  1 个评论
Matt Grant
Matt Grant 2017-9-25
I'm finding that passing symbolic functions only works if you use the same name for the symbolic variable both inside and outside the function.
For example, the function I'm writing is supposed to output a symbolic function g(T), given some input function f(T). If I go:
syms T;
f1(T)=T;
f2(T)=my_routine(f1);
Where my_routine is:
function g = my_routine(f)
syms T;
g(T)=1+f(T)
end
then everything works fine. Ie, the output is:
f2(T) =
T + 1
But if I try to make the output a function of something other than T, such doing:
syms x;
f1(x)=x;
f2(x)=my_routine(f1);
then f2(x) comes out as a function of T instead of x, ie, I get:
f2(x) =
T + 1

请先登录,再进行评论。


Ethan Duckworth
Ethan Duckworth 2022-4-28
If I understand what you want to do, I think it can be done by querying the input to get the variable, then using whatever syms variable inside the function you want via subs, and then changing back for the output.
Here's an example:
function out = add_x(f)
syms y;
fvar = symvar(f);
f=subs(f,fvar,y);
out= y + f;
out = subs(out,y,fvar);
end
when I run this I get the following
syms t
f(t)=t^2
g = add_x(f) % ha ha, it's not x that's added
g(t)=t^2+t

Community Treasure Hunt

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

Start Hunting!

Translated by