Effacer les filtres
Effacer les filtres

How to change MATLAB precision of variables?

6 vues (au cours des 30 derniers jours)
gautam vatsa
gautam vatsa le 27 Avr 2022
Commenté : Walter Roberson le 29 Avr 2022
I have a big code - A function file which consists of functions of multiple variables, which are then supplied to fsolve to solve for the variables in the main file. I want to change the precision of all the computations. For that what I found from internet is to use the function vpa. However my code is big (1000 lines or more). So, going by that I will have to put vpa in almost every line of the code.
Is there an alternative to do this.
Thanks in advance.
Gautam Vatsa

Réponses (2)

Walter Roberson
Walter Roberson le 27 Avr 2022
fsolve() can never work with symbolic numbers, so what you seem to be suggesting will not work. You would need to switch to vpasolve().
There are some situations where it helps to vpa() multiple times, but more often you should not vpa until the end or until you need to display results.
Because of this, a more common strategy is to use a more advanced editor to wrap all numeric constants with sym() or str2sym()... except then you have to undue that for subscripts and some places where the constant is a dimension number.
Also you end up having to change all zeros() and ones() calls that initialize variables

gautam vatsa
gautam vatsa le 29 Avr 2022
I don't want to use syms because it makes the code extremely slow. I want to use fsolve and still want to increase the precision of all my computations. Is there a way to do so??
  1 commentaire
Walter Roberson
Walter Roberson le 29 Avr 2022
Note that vpa() uses the Symbolic Toolbox to create software floating point numbers -- giving symbolic results. The only difference between that and using sym() directly is that by default sym() of a double precision value converts the value to rational; but by passing an optional parameter to sym() then sym() would convert to software floating point anyhow.
So if you thought vpa() would increase your precision but not impact your speed much, but that sym() would ruin your speed.. then NO, that is not how it works. To get the extended precision you would be working with the Symbolic toolbox either way.

Connectez-vous pour commenter.

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by