Effacer les filtres
Effacer les filtres

Can someone tell me the difference between these codes?

1 vue (au cours des 30 derniers jours)
Dan Zhu
Dan Zhu le 8 Oct 2020
Commenté : Dan Zhu le 9 Oct 2020
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?

Réponse acceptée

Ameer Hamza
Ameer Hamza le 8 Oct 2020
Modifié(e) : Ameer Hamza le 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)
  1 commentaire
Dan Zhu
Dan Zhu le 9 Oct 2020
Thanks, I'll use the second one to avoid any possible error.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 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!

Translated by