Can we distinguish between variables and parameters in a symbolic function?

Hello,
I have a simple (perhaps naive, if so my appology) question. Consider the following
syms x f(x) x
f(x) = a*x;
Is there a way to distinguish between 'x' and 'a'? If I use symvar(f) it just gives the information about all vars and aparetly
cannot distinguish between x and a.
Any idea?
Thanks in advance,
Babak

2 Comments

symvar determines symbolic variables in the expression. Since you have not defined a as a symbolic variable in the above code, symvar won't classify a as an output.
What is the data type of a?
Sorry, that was just a typo, which I correct now
syms x f(x) a
f(x) = a*x;

Sign in to comment.

 Accepted Answer

syms f(x) a m n z
f(x)=a*x
f(x) = 
y=symvar(f)
y = 
I understand what you mean by 'cannot distinguish between x and a'
But, this is how syms variable are expressed in arrays. For example -
z=[m n]
z = 
x and a are expressed similarly in the above expression obtained from symvar
However, you can convert the symbolic expression to string and obtain seperate variables -
z=symvar(char(f))
z = 2×1 cell array
{'a'} {'x'}

6 Comments

Thanks Dyuman!
Well, still there is no way to distinguish between 'x' and 'a'.
If MATLAB does not have such a capability then I believe MathWorks should include this. In high level mathematical analysis as well as high level pogramming this is needed (My opinion. They decide about it).
How exactly do you want to distinguish between x and a?
for instance, it could be through a logical variable using a function I call 'isparameter' as bellow
symvar(f)
[x, a]
isparameter(symvar(f))
[0, 1]
What in your opinion is a remarkable difference between a variable and a parameter in symbolic computations that would justify to distinguish between the two ? I don't see any.
If you want to distinguish between them, I suggest you give them adequate names, e.g. var_a or para_a.
Hi Torsten and Dyuman,
After I thought deeper I came to believe that you are right. Thanks for your time.
Math is somehow different than programming :-)

Sign in to comment.

More Answers (1)

f_variables = argnames(f)
f_param = setdiff(symvar(f), f_variables)
This is not the same thing as "all variables mentioned that are not parameters". symvar does not report any "bound" variables or any variables being used as functions.
A bound variable is like x in
int(f(x), x, a, b)
provided that f does not itself contain x then you could substitute any other variable name without affecting the output, like
int(f(Dummy), Dummy, a, b)
int() and symsum() and symprod() can all use bound variables.

1 Comment

Hi Torsten,
So, it is actually possible to do it in matlab. Nice!
You also answered another question of me (somehow you read my mind!).
Thanks a lot for the very useful helps!
Babak

Sign in to comment.

Categories

Find more on Programming 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!