how can I do subtraction between two symbolic functions with different arguments?
조회 수: 9 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
답변 (3개)
VBBV
2022년 11월 12일
syms X(alpha,beta)
syms Y(theta)
eqn = X(alpha,beta)-Y(theta) % define symvars for X and Y
댓글 수: 0
Walter Roberson
2022년 11월 12일
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.
댓글 수: 0
John D'Errico
2022년 11월 12일
편집: John D'Errico
2022년 11월 12일
syms X(alpha,beta)
syms Y(theta)
syms delta
Y(delta)
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)
class(X)
So MATLAB knows that X is a symfun.
class(result1)
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))
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
whos result2
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
So the simple looking X-Y is a problem. MATLAB is now confused, for the reason @Walter Roberson explains.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!