Azzera filtri
Azzera filtri

Handling a very big difference between numbers(ratio)

3 visualizzazioni (ultimi 30 giorni)
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 Commenti
Steven Lord
Steven Lord il 29 Ago 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
Vadim Patrick Nave il 29 Ago 2022
Thank you Steven, you are right. Sorry.
While dy is a very small number
x is very large number
% Fit line analytically:
matrix = [ sum(x.^2./dy.^2) sum(x./dy.^2);
sum(x./dy.^2) sum(1./dy.^2)];
y_sigma = y./dy.^2;
free_vector = [sum(x.*y_sigma);
sum(1.*y_sigma)];
solution=matrix\free_vector;
a=solution(1);b=solution(2);
err=sqrt(diag(inv(matrix)));
da=err(1);db=err(2);
% if there are errors in x, co-add them and re-fit
if nargin>10
dy = sqrt((a*dx).^2+dy.^2);
matrix = [ sum(x.^2./dy.^2) sum(x./dy.^2);
sum(x./dy.^2) sum(1./dy.^2)];
y_sigma = y./dy.^2;
free_vector = [sum(x.*y_sigma);
sum(1.*y_sigma)];
solution=matrix\free_vector;
a=solution(1);b=solution(2);
err=sqrt(diag(inv(matrix)));
da=err(1);db=err(2);
end

Accedi per commentare.

Risposte (1)

Infinite_king
Infinite_king il 4 Dic 2023
Modificato: Infinite_king il 4 Dic 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);
0.6448 0.1331 0.2477 0.0948 0.9077 0.0157 0.2160 0.5678 0.6035 0.4680 0.1102 0.4070 0.8393 0.2991 0.9093 0.2817 0.2833 0.9535 0.0625 0.5194 0.2113 0.5092 0.7433 0.0416 0.2276
% 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);
5.6448 5.1331 5.2477 5.0948 5.9077 5.0157 5.2160 5.5678 5.6035 5.4680 5.1102 5.4070 5.8393 5.2991 5.9093 5.2817 5.2833 5.9535 5.0625 5.5194 5.2113 5.5092 5.7433 5.0416 5.2276
For more information on how to use ‘sym’ function and ‘Symbolic Math Toolbox’, please refer the following MATLAB documentations,
Hope this is helpful.
  6 Commenti
Walter Roberson
Walter Roberson il 4 Dic 2023
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)
ans = 6
No eval() needed.
See also vpa such as
vpa(cos(sym(pi)^2), 50)
ans = 

Accedi per commentare.

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by