Handling a very big difference between numbers(ratio)
5 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I was working on a code that demands operating with statistics of long numbers (such as 2000000) vs the errors(such as 0.0003) so different equations that demands both of the numbers are equal 0 or Inf, how can I improve my situation?
I tried to use vpa, it helps but it's not a magic pill and the problems appers later in the code.
Is there anyway to operate with such numbers in terms of division without getting results as Inf or 0?
Thank you and a blessed week,
Vadim
5 件のコメント
Steven Lord
2022 年 8 月 29 日
I was working on a code that demands operating with statistics of long numbers (such as 2000000) vs the errors(such as 0.0003) so different equations that demands both of the numbers are equal 0 or Inf, how can I improve my situation?
It's likely to be difficult if not impossible to offer any specific suggestions without seeing the specific equations you're using.
回答 (1 件)
Infinite_king
2023 年 12 月 4 日
編集済み: Infinite_king
2023 年 12 月 4 日
Hi Vadim Patrick Nave,
I understand that you want to work with very large and very small numbers and perform arithmetic operations on them without running into ‘Inf’ or ‘nan’ values.
I suggest you to use ‘sym’ function which was available in ‘Symbolic Math Toolbox’. First convert the number to a symbolic number or matrix to symbolic matrix. Then you can perform simple arithmetic operations and finally you can use ‘double’ function to convert the answer to double.
Refer below code snippet,
% let x be a matrix of numbers
x = rand(5);
disp(x);
% now convert the matrix to symbolic matrix
x_sym = sym(x);
% now perform simple arithmetic operations
% op 1
% op 2
% for example, addition
x_sym = x_sym + 5;
% now convert the values to double
% make sure the numbers are within range of double
res = double(x_sym);
disp(res);
For more information on how to use ‘sym’ function and ‘Symbolic Math Toolbox’, please refer the following MATLAB documentations,
- https://www.mathworks.com/help/symbolic/sym.html
- https://www.mathworks.com/products/symbolic.html
- https://www.mathworks.com/help/symbolic/sym.double.html
Hope this is helpful.
6 件のコメント
Walter Roberson
2023 年 12 月 4 日
x = 5;
x_sym = sym(x);
x_sym = x_sym * 5;
x_sym = x_sym + 5;
x_sym = x_sym / 5;
% how to evaluate x_sym, assuming the resulting value is within ranage of
% double.
double(x_sym)
No eval() needed.
vpa(cos(sym(pi)^2), 50)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!