Handling a very big difference between numbers(ratio)
Show older comments
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 Comments
Chunru
on 29 Aug 2022
Have you considered the log scale?
Mathieu NOE
on 29 Aug 2022
hi
why not working with log of your numbers / ratio ? this way the "difference" is much reduced.
Vadim Patrick Nave
on 29 Aug 2022
Steven Lord
on 29 Aug 2022
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.
Vadim Patrick Nave
on 29 Aug 2022
Answers (1)
Infinite_king
on 4 Dec 2023
Edited: Infinite_king
on 4 Dec 2023
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 Comments
Walter Roberson
on 4 Dec 2023
Edited: Walter Roberson
on 4 Dec 2023
and final you can use ‘eval’ function to convert the answer to double.
No you cannot. There is no documented meaning for eval() of a symbolic expression.
syms x
f = piecewise(1 <= x & x <= 5, x, x^2)
char(f)
eval(f)
Symbolic expressions displayed to the user are in a user-friendly-ish language, not in a programming language. They are not expressions in MATLAB, and by the time they reach the user, they are not expressions in the internal MuPAD programming language. They can use constructs that are not accepted by MATLAB, such as the above in-fix notation use of the "in" operator, and such as the :: construction . The order of parameters they show might not be the same as the order that a numeric function of the same name expects, and might not be the same as the order that a symbolic function of the same name expects, and might not be the same as the order that the internal MuPAD programming language expects.
Infinite_king
on 4 Dec 2023
Hi Walter Roberson,
Here we are not using symbolic varialbes. we are using symbolic numbers.
Walter Roberson
on 4 Dec 2023
Still no defined meaning for eval() of a symbolic number.
If you have a symbolic expression, you can vpa() it and hope that the result is a numeric approximation of the result (will not always be, especially if the value involves a very bumpy integral)
If you have a symbolic expression that has no "free variables" in it, you can double() it and hope a numeric approximation could be created (will not always work for values that theoretically exist but are tough to compute.)
Walter Roberson
on 4 Dec 2023
So let us take a closer look to the documentation of eval() that you linked to https://www.mathworks.com/help/matlab/ref/eval.html
Syntax
And what is an expression?
expression — Expression to evaluate
character vector | string scalar
Notice those are not symbolic expressions or symbolic numbers.
You will not find eval() documented anywhere in the Symbolic Toolbox. https://www.mathworks.com/help/symbolic/index.html
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)
Infinite_king
on 4 Dec 2023
Thanks for the information Walter.
Categories
Find more on Matrix Indexing 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!