Main Differences Between Symbolic Variables and Symbolic Functions

33 visualizzazioni (ultimi 30 giorni)
What are the main differences between them? I know that to evaluate a symbolic variable we use subs and to evaluate a symbolic function, say f(x), we type f(x). Are there any other differences?
  1 Commento
Steven Lord
Steven Lord il 6 Feb 2022
Is there a specific concern or question you have about symbolic variables and symbolic functions?

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 6 Feb 2022
Modificato: Walter Roberson il 7 Feb 2022
  • evaluation of a symbolic expression at specific values is by using subs()
  • evaluation of a symbolic function at specific values is by using MATLAB function call syntax, like f(3)
  • you can index a symbolic expression using numeric indices. You cannot index a symbolic function with any syntax to return the individual components
  • When you diff() a symbolic expression without providing a variable name to differentiate with respect to, then MATLAB uses symvar() to find the variables, and differentiates with respect to the first one returned
  • when you diff() a symbolic function without providing a variable name to differentiate with respect to then MATLAB uses the first symbolic parameter defined for the function
  • likewise for int() and choice of variables
  • you can do arithmetic operations on symbolic expressions limited only by the sizes being compatible
  • you can do arithmetic operations on symbolic functions only if they have the same exact same list of named parameters in the same order.
  • you can create assumptions on symbolic variables and symbolic expressions. You cannot create assumptions about symbolic functions. Which is a disappointment as there are certainly times you would like to be able to say that unknown function f(x) returns real values or nonnegative values or whatever.
  • children() cannot be used to determine the body of a symbolic function or the names of the parameters. When the symbolic function returns a non-scalar, children() cannot be used to determine the individual elements. Instead, children() applied to a symbolic function gives the same result as if you were applying to a symbolic expression that is the body of the function. For example f(x)=[sin(x*2),cos(x*3)] and g=[sin(x*2),cos(x*3)] then children(f) and children(g) will have the same result. This is really a case of a similarity rather than a difference between the two. However, I remark on it to emphasize that there is no supported mechanisms to get at the components of a symbolic function that is non-scalar — no way except to invoke the function with symbolic variables the same as the parameter names, getting back the evaluated expression.

Più risposte (2)

John D'Errico
John D'Errico il 6 Feb 2022
I'm really not sure what you are asking. One is a function of another variable, the other is a variable. A variable may be a constant, it may contain an expression, etc. In MATLAB, think of a variable as a container.
syms y(x)
Here y is an explicit function of the variable x. We can differentiate it, and MATLAB understands how the functionality works.
diff(y)
ans(x) = 
A common use for such a construct is to formulate a differential equation. It makes things easy to write, here, in essentially one line (not counting the syms call before):
ysol = dsolve(diff(y) == y + x,y(1) == 2)
ysol = 
As I said, symbolic variables can be thought of as containers, just like any normal variable in MATLAB. Since x already exists in symbolic form here...
z = 3 + x
z = 
So z contains an expression. A symbolic variable may contain a simple constant, or even a rather golden one...
phi = (1 + sqrt(sym(5)))/2
phi = 
And while we can differentiate a symbolic variable, we need to be careful.
syms u v
w = u + 2*v
w = 
diff(w,u)
ans = 
1
In that derivative, did we want MATLAB to understand if the derivative of v with respect to u is non-zero? Change things slightly now...
syms v(u)
w = u + 2*v
w(u) = 
diff(w,u)
ans(u) = 
The point being, we define a symbolic function when we want to explicitly define a functional relationship. Otherwise, a variable is often adequate for most purposes.

Rishabh Singh
Rishabh Singh il 4 Feb 2022
Hi Kareem,
You can refer to following question for understanding of Symbolic variables. In simple terms symbolic toolbox mimics mathematical syntax on paper to MATLAB environment. You can perform analytical calculations over symbolic functions. You can refer to documentation for more information.
Hope this helps.
  1 Commento
Kareem Elgindy
Kareem Elgindy il 6 Feb 2022
Thanks Rishabh, however your answer still doesn't clarify the main differences between symbolic variables and symbolic functions.

Accedi per commentare.

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by