필터 지우기
필터 지우기

How to change MATLAB precision of variables?

조회 수: 6 (최근 30일)
gautam vatsa
gautam vatsa 2022년 4월 27일
댓글: Walter Roberson 2022년 4월 29일
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

답변 (2개)

Walter Roberson
Walter Roberson 2022년 4월 27일
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 2022년 4월 29일
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
Walter Roberson
Walter Roberson 2022년 4월 29일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by