Can someone tell me the difference between these codes?
1 次查看(过去 30 天)
显示 更早的评论
syms h1(r,theta), rnu(theta,phi)
A = h1(r,theta) + rnu(theta,phi) %---> I know if I don't specify the argument, I'll get an argument not matching error.
A(r,theta,phi) = h1(r,theta) + rnu(theta,phi)
They both work and the result seem to be exactly the same. Am I missing something? Is there any difference between the above two lines?
0 个评论
采纳的回答
Ameer Hamza
2020-10-8
编辑:Ameer Hamza
2020-10-8
There are differences between the two. First creates a symbolic expression, whereas second, create a symbolic function. To see the difference, check the output of the following for the second case
syms h1(r,theta) rnu(theta,phi)
A(r,theta,phi) = h1(r,theta) + rnu(theta,phi);
A(1,2,3)
Result
>> A(1,2,3)
ans =
h1(1, 2) + rnu(2, 3)
It treated A as a function and replaced r=1, theta=2, phi=3.
Now for the first A in your code
syms h1(r,theta) rnu(theta,phi)
A = h1(r,theta) + rnu(theta,phi); %---> I know if I don't specify the argument, I'll get an argument not matching error.
A(1,2,3)
You will get an error. Here A is a variable and MATLAB interpert (1,2,3) as index. To get the same output in this case, you will need to use subs()
>> subs(A, [r theta phi], [1 2 3])
ans =
h1(1, 2) + rnu(2, 3)
更多回答(1 个)
Walter Roberson
2020-10-8
There is a difference.
syms h1(r,theta) rnu(theta,phi)
A = h1(r,theta) + rnu(theta,phi)
This returns an expression that is the sum of two function calls.
A(r,theta,phi) = h1(r,theta) + rnu(theta,phi)
This returns a symbolic function with three input parameters.
There are a lot of times when MATLAB will silently convert symbolic functions to their equivalent expressions, but there are differences. For example in the first one, the expression
A(1,2,3)
would fail because it would be trying to index the symbolic scalar as-if it were a 3 dimensional array.
Whereas for the second one, A(1,2,3) would evaluate the expression h1(1,2) + rnu(2,3)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!