How to build a symbolic function based on other symbolic functions?
Show older comments
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?
Accepted Answer
More Answers (1)
madhan ravi
on 10 Jul 2020
Edited: madhan ravi
on 10 Jul 2020
f4(x,y) = f1(x) + f2(y)
4 Comments
Shuangfeng Jiang
on 10 Jul 2020
Edited: Shuangfeng Jiang
on 10 Jul 2020
madhan ravi
on 10 Jul 2020
Edited: madhan ravi
on 10 Jul 2020
Because they are symfuns.
Alternatively:
f1_arg = symvar(f1);
f2_arg = symvar(f2);
f4(f1_arg, f2_arg) = f1(f1_arg) + f2(f2_arg)
%or
f4 = formula(f1) + formula(f2);
f4_arg = num2cell(symvar(f4));
f4(f4_arg{:}) = f4
%or
f4 = formula(f1) + formula(f2);
f4 = symfun(f4, symvar(f4))
Shuangfeng Jiang
on 10 Jul 2020
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 Operators and Elementary Operations 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!