Effacer les filtres
Effacer les filtres

Numerical Output from a Symbolic Expression

1 vue (au cours des 30 derniers jours)
Ammar Taha
Ammar Taha le 4 Avr 2021
Commenté : Star Strider le 4 Avr 2021
I'm coding the Newton method for root finding using function handles and symbolic expression to determine the derevative of f(x), the output after four iteration is : 665857/470832, which is a correct answer but not formatted the right way. I understand that this is due to the subs function, but I really don't know what is happening in the background.
For reference I got the right format by using >>vpa(ans,5);
So why Matlab is doing that?!

Réponse acceptée

Star Strider
Star Strider le 4 Avr 2021
So why Matlab is doing that?!
Because the results of symbolic expressions and operations remain symbolilc.
Another option would be:
double(ans)
if you want to use the result in numerical calculations outside of the symbolic environment.
  2 commentaires
Ammar Taha
Ammar Taha le 4 Avr 2021
Ok, thanks.
Star Strider
Star Strider le 4 Avr 2021
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 4 Avr 2021
Modifié(e) : Walter Roberson le 4 Avr 2021
sympref('floatingpointoutput', true)
As to "why":
When you use a double precision value in a symbolic expression, the Symbolic Toolbox processes the double precision number with sym() to convert it to symbolic number. The default conversion is processing with the 'r' (rational) conversion, which creates rational numbers (unless it can find a simple square root or multiple of pi)
You can force individual double precision numbers to be processed differently by calling sym() yourself with one of the other conversion flags.
However... what you should probably be doing instead is taking the symbolic expression of your function and its derivative and using matlabFunction() to convert them into double precision functions, and then this would no longer be relevant.

Community Treasure Hunt

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

Start Hunting!

Translated by