How do I solve/plot a system of symbolic functions?
5 views (last 30 days)
Show older comments
Apparently the Symbolic Math Toolbox™ currently does not support composite symbolic functions, or symbolic functions that are functions of another symbolic functions. I have a system of symbolic functions that I want to solve, however I am unable to figure out a solution. For example, my main function in simple terms is some like Y = C1 + integral(C2 dx [0 x]) - integral(Z dx [0 x]) where C1 and C2 are constants and Z is some function of Y, like Z = C3Y where C3 is a constant, although not this simple. I’ve tried to solve this even bringing Y in terms of nothing but itself and x, but obviously even if I do this it’s still a function dependent on a function (itself).
Answers (1)
Walter Roberson
on 11 Feb 2022
The ability of one symbolic function to refer to another symbolic function is not going to be added anytime soon.
The technology used to define symbolic functions is that
NAME(VAR) = EXPRESSION
is handled as
temporary = evaluate EXPRESSION as far as it goes
NAME = symfun(temporary, VAR)
which is close to
temporary = evaluate EXPRESSION as far as it goes
NAME = @(Value) subs(temporary, sym('VAR'), Value)
in that NAME becomes an object with an feval method that consists of subs() the input value for the named parameter in the already-evaluated EXPRESSION.
Notice in particular that:
- when EXPRESSION is being evaluated for the purpose of building the symbolic function, then the value of VAR (the named parameter) is not somehow reserved or frozen, that MATLAB more or less ignores the fact that you are defining a function until after EXPRESSION has been evaluated with whatever value of VAR currently exists
- NAME (the function) does not come into existence until after EXPRESSION is evaluated
Example 1:
syms y
x = y
F1(x) = x/y
This would proceed as
syms x
x = y
temporary = x/y --> temporary = y/y --> temporary = sym(1)
F1 = symfun(temporary, x) --> F1 = symfun(temporary, y) --> F1(y) = sym(1)
%which would be close to F1 = @(y) subs(sym(1), y)
Example 2:
syms x
F1(x) = sym(0)
F1(x) = F1(x) + x * F1(x-1)
This would proceed as
syms x
temporary = sym(0)
F1 = symfun(temporary, x) --> similar to F1 = @(x) subs(sym(0), sym('x'), x)
temporary = F1(x) + x*F1(x-1) --> temporary = sym(0) + x * F1(x-1) -->
temporary = sym(0) + x * sym(0) --> temporary = sym(0) + sym(0) --> temporary = sym(0)
F1 = symfun(temporary, x) --> F1(x) = sym(0)
%which would be close to F1 = @(x) subs(sym(0), sym('x'), x)
The statement
F1(x) = F1(x) + x * F1(x-1)
looks like it is defining F1 recursively, but it is not: the right hand side is fully evaluated to an expression before the new F1(x) is assigned to, overwriting the old F1 completely.
Now... imagine that MATLAB did permit recursive definitions, such as
syms n
Factorial(n) = n * Factorial(n-1)
.. then when would the recursion stop? You could get around the problem by forcing people to write something like
syms n
Factorial(n) = piecewise(n == 0 | n == 1, 1, n * Factorial(n-1))
... though that doesn't vectorize properly.
Some symbolic languages permit definitions that might look like
Factorial(0) := 1
Factorial(1) := 1
Factorial(n) = n * Factorial(n-1)
It is valid for programming languages to allow definitions to accumulate... but they require a quite different implementation technogy than MATLAB uses.
1 Comment
Walter Roberson
on 11 Feb 2022
So what do you do if you want to define functions in terms of each other, F1 depends upon F2, F2 depends upon F1 ?
- In some cases you can expand one of the functions into an expression, probably piecewise(), and then express the other function in terms of the expression. It might end up with a complicated function with parts that look redundant, but sometimes you can get it to work. Sometimes the bounds involved are such that there is no actual recursion
- In some cases, for your practical purposes, you can approximate towards one or both ends of the function, in a way that permits you to get rid of the recursion
- In most cases, you effectively have to give up finding a closed form symbolic solution, and instead need to create an expression of differences and solve for the zero. You might not be able to express the proper y symbolically, but for any given set of other parameters you might be able to find it numerically. You might end up having to do numeric integration of a number of these numeric solutions, instead of symbolic integration. Especially when you are dealing with integral equations, a lot of the time there really isn't any closed form solution anyhow.
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!