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

조회 수: 4 (최근 30일)
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
Dyuman Joshi
Dyuman Joshi 2022년 12월 15일
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?
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2022년 12월 15일
Sorry, that was just a typo, which I correct now
syms x f(x) a
f(x) = a*x;

댓글을 달려면 로그인하십시오.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2022년 12월 15일
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
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2022년 12월 17일
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 :-)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 12월 17일
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
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2022년 12월 17일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by