How to change MATLAB precision of variables?
5 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Walter Roberson
on 27 Apr 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
0 Comments
gautam vatsa
on 29 Apr 2022
1 Comment
Walter Roberson
on 29 Apr 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.
See Also
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!