How to split a function according to their variables?
Show older comments
Hi. I want to split function according to their variables.
For example
f(x1,x2,y1,y2) = 10*x1 + 20*x2 + 10*y1 + 10*y2 ;
And I want to split like below.
f(x1) = 10*x1 ;
f(x2) = 20*x2 ;
f(y1) = 10*y1 ;
f(y2) = 20*y2 ;
Please help me...!
Accepted Answer
More Answers (1)
syms x1 x2 y1 y2
f(x1,x2,y1,y2) = 10*x1 + 20*x2 + 10*y1 + 10*y2 + log(x1) + 1/x1 + 1/y1 ;
fx1 = mapSymType(f, "plus", @(X) SelectPlus(X, x1))
fx2 = mapSymType(f, "plus", @(X) SelectPlus(X, x2))
fy1 = mapSymType(f, "plus", @(X) SelectPlus(X, y1))
fy2 = mapSymType(f, "plus", @(X) SelectPlus(X, y2))
function selected = SelectPlus(Expression, var)
ch = children(Expression);
selected_ch = ch(cellfun(@(X) ismember(var, symvar(X)), ch));
selected = sum([selected_ch{:}]);
if isempty(selected); selected = sym(0); end
end
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!