How to build a symbolic function based on other symbolic functions?
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (1)
madhan ravi
il 10 Lug 2020
Modificato: madhan ravi
il 10 Lug 2020
f4(x,y) = f1(x) + f2(y)
4 Commenti
Shuangfeng Jiang
il 10 Lug 2020
Modificato: Shuangfeng Jiang
il 10 Lug 2020
madhan ravi
il 10 Lug 2020
Modificato: madhan ravi
il 10 Lug 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
il 10 Lug 2020
madhan ravi
il 10 Lug 2020
Modificato: madhan ravi
il 10 Lug 2020
Well, I have shown you 4 alternatives. You could choose the ones which suits you the best (IMO: the last two).
Categorie
Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!