how can I do subtraction between two symbolic functions with different arguments?

9 vues (au cours des 30 derniers jours)
jeap
jeap le 12 Nov 2022
Modifié(e) : John D'Errico le 12 Nov 2022
Hi, everyone! I have a question here. When I run commands as bellow:
syms X(alpha,beta)
syms Y(theta)
X-Y
An error turns out:
Error using symfun/privResolveArgs (line 239)
Symbolic function input arguments must match.
Error in sym/privBinaryOp (line 1134)
args = privResolveArgs(A, B);
Error in - (line 7)
X = privBinaryOp(A, B, 'symobj::zipWithImplicitExpansion', '_subtract');
It seems like X has different argument name as Y, interruption occurs at line 238 in symfun.m:
if isa(arg,'symfun') && ~isequal(fvars,arg.vars)
error(message('symbolic:symfun:InputMatch'));
else
argout{k} = symfun(argout{k},fvars);
end
So, the questions is that how can I do subtraction between two symbolic functions with different symvars?
THANK YOU EVERYONE!

Réponses (3)

VBBV
VBBV le 12 Nov 2022
syms X(alpha,beta)
syms Y(theta)
eqn = X(alpha,beta)-Y(theta) % define symvars for X and Y
eqn = 

Walter Roberson
Walter Roberson le 12 Nov 2022
You cannot. The symbolic function feature is specifically designed to prevent operations between symbolic functions that have different parameters.
The reason for this is that when you perform an operation on a symbolic function, the result has to be a symbolic function, and if the parameters of the component functions are different then you cannot uniquely define what parameter list should be used for the symfun that results from the operation.
You need to convert the functions to symbolic expressions by invoking them with parameters, and then you need to symfun() the result with the parameter order that is appropriate for the combined function.

John D'Errico
John D'Errico le 12 Nov 2022
Modifié(e) : John D'Errico le 12 Nov 2022
syms X(alpha,beta)
syms Y(theta)
syms delta
Y(delta)
ans = 
X and Y are symfuns. MATLAB understands that they can be evaluated for any argument passed in. So if we pass delta to Y, we get Y(delta). If we do this, where we resolve the arguments to X and Y, there is no problem.
result1 = X(alpha,beta) - Y(theta)
result1 = 
class(X)
ans = 'symfun'
So MATLAB knows that X is a symfun.
class(result1)
ans = 'sym'
But result1 is not a symfun, now now just a simple symbolic expression. So MATLAB was able to subtract the two terms, and get a result. It is just no longer a symfun.
That is, MATLAB understands that X is a function of alpha and beta, Y a function of theta. But once you evaluate Y(theta), for example, the result is no longer a symfun.
class(Y(theta))
ans = 'sym'
Do you see there is no problem in performing arithmetic operations between those functions, once you explicitly assign arguments to them? They are no longer symfuns, but just symbolic expressions.
Try this now:
syms Z(alpha,beta)
result2 = X+Z
result2(alpha, beta) = 
whos result2
Name Size Bytes Class Attributes result2 1x1 8 symfun
So what happens when MATLAB performx X+Z? MATLAB is smart enough to decide that both sub-expressions are symfuns, AND THEY HAVE THE SAME ARGUMENT LIST. Ok, we can handle that operation. However, I have a funny feeling that had I defined W, as
syms W(beta,alpha)
Now I could not just add or subtract W with either X or Z. The argument list does not match, and we will see a failure.
Note the subtle difference between the above, and
result3 = X - Y
Error using symfun/privResolveArgs
Symbolic function input arguments must match.

Error in sym/privBinaryOp (line 1216)
args = privResolveArgs(A, B);

Error in - (line 7)
X = privBinaryOp(A, B, 'symobj::zipWithImplicitExpansion', '_subtract');
So the simple looking X-Y is a problem. MATLAB is now confused, for the reason @Walter Roberson explains.

Tags

Produits


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by