# How to build a symbolic function based on other symbolic functions？

89 views (last 30 days)
Shuangfeng Jiang on 10 Jul 2020
Commented: Shuangfeng Jiang on 12 Jul 2020
As the codes shown below, I would like to build f4 based on f1 and f2, but an error occurred.
syms x y
f1(x)=x+1;
f2(y)=y+2;
f3(x,y)=f1+y;% <-- nothing wrong
f4(x,y)=f1+f2% <-- where the error comes from
The error information:
Error using symfun/privResolveArgs (line 218)
Symbolic function input arguments must match.
Error in sym/privBinaryOp (line 1001)
args = privResolveArgs(A, B);
Error in + (line 7)
X = privBinaryOp(A, B, 'symobj::zipWithImplicitExpansion', '_plus');
Error in test_July_2020 (line 28)
f4(x,y)=f1+f2
Can someone please tell me why f3 can be built but f4? How to build a symfun based on other symfuns?

Walter Roberson on 10 Jul 2020
When you use a symbolic function (by name, with no parameters) with a binary operator, and the other operand does not involve symbolic functions without parameters, then MATLAB can unambiguously construct a new symbolic function that takes the same arguments of the existing symbolic function and returns the appropriate value.
When you use a symbolic function (by name, with no parameters) with a binary operator, and the other operand involves a symbolic function with exactly the same number and order of parameters, then again MATLAB can unambiguously construct a new symbolic function that takes the same arguments of the existing symbolic function and returns the appropriate value.
However, when you use a symbolic function (by name, with no parameters) with a binary operator and the other operand involves a symbolic function with a different number or order of parameters, then MATLAB cannot know what the "right" order of parameters for the function to construct would be. When you add f1 (a function of just x) and f2 (a function of just y), then should the function that is created on the fly be a function of (x,y) or of (y,x) ?
It is important to understand for this purpose that this attempted automatic function construction is done first, without looking at the left hand side of the assignment.
The assignment statement
f3(x,y) = f1 + f2
is not treated like
function result = f3(x,y)
result = f1 + f2
end
and is not treated like
f3 = @(x,y) f1 + f2
The f3(x,y) on the left hand side is not keyword abbreviation for constructing a true function.
f3(x,y) = f1 + f2
is treated as
temporary = f1 + f2
f3 = symfun(temporary, [x, y])
to emphasize, the right hand side of the f3(x,y) = f1 + f2 is fully evaluated (probably returning a symbolic expression or symbolic function), and then turned into a symbolic function with the variable names given on the left hand side.
I do not mean this as an approximation of how MATLAB works internally: I mean that this is how MATLAB processes the line.
So, when f1 + f2 is trying to figure out whether to construct a temporary function of (x,y) or of (y,x), it does not have access to the context that eventually the output is going into a symfun that will have (x,y) as the inputs: the expression has to fully evaluate on its own, and that fails because it cannot reasonably decide the parameter order for the temporary function to be emitted.
f3(x,y) = f1(x) + f2(y)
then the result of f1(x) and of f2(y) would be expressions rather than symfun, and there is no problem adding expressions. Then, the expression fully constructed and inside an internal temporary variable,
f3 = symfun(temporary, [x,y])
would have no problem.
The error only occurs when you use symfun without parameters in expressions, and you encounter a situation in which the parameters do not match between two symfun.
Shuangfeng Jiang on 12 Jul 2020
Thank you so much for your detailed explanation! That's what I really want to know.

madhan ravi on 10 Jul 2020
Edited: madhan ravi on 10 Jul 2020
f4(x,y) = f1(x) + f2(y)
madhan ravi on 10 Jul 2020
Edited: madhan ravi on 10 Jul 2020
Well, I have shown you 4 alternatives. You could choose the ones which suits you the best (IMO: the last two).

### Categories

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

Translated by