Clear Filters
Clear Filters

Can someone tell me the difference between these codes?

1 view (last 30 days)
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?

Accepted Answer

Ameer Hamza
Ameer Hamza on 8 Oct 2020
Edited: Ameer Hamza on 8 Oct 2020
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)

More Answers (1)

Walter Roberson
Walter Roberson on 8 Oct 2020
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)

Tags

Community Treasure Hunt

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

Start Hunting!