Storing small numbers and logarithms.

조회 수: 5 (최근 30일)
daniel adams
daniel adams 2021년 11월 30일
댓글: daniel adams 2021년 12월 1일
Hi I have the following problem, in my code I need to calculate for very large. The largeness causes matlab to store and to be zero. Does anyone know a way around this? Is there a way of taking logarithms first?
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 11월 30일
What precision do you need for output? If a and b differ by more than about 36.04 then to numeric precision, the answer is going to be the same as -min(a,b) . If they differ by N then approximately the first (3/2)*abs(N) bits are conserved

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

답변 (2개)

Walter Roberson
Walter Roberson 2021년 11월 30일
Depending on your range of values and your accuracy needs, you could try a double taylor approximation.
syms a b positive
E = log(exp(-a) + exp(-b))
E = 
Ta = taylor(E, a, 'ExpansionPoint', 10^5, 'Order', 10)
Ta = 
Tb = taylor(Ta, b, 'ExpansionPoint', 10^5, 'Order', 10)
Tb = 
Ts = simplify(expand(Tb))
Ts = 
Tsa = collect(Ts, [a])
Tsa = 
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 11월 30일
Well, this turns out to be quite inaccurate in my tests !
daniel adams
daniel adams 2021년 12월 1일
Yes cheers for the try though :)

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


Steven Lord
Steven Lord 2021년 11월 30일
How large is "very large"? Can you perform the calculation symbolically (by converting a and b into sym values before trying to pass them into exp -- if you try to perform the exp calculations in double precision then convert to sym you've already underflowed) and convert the result back to double afterwards?
a = 12345;
as = sym(a);
L = log(sym(exp(-a))) % exp(-a) underflowed in double before being converted to sym
L = 
Ls = log(exp(-as)) % No underflow
Ls = 
  댓글 수: 3
Steven Lord
Steven Lord 2021년 12월 1일
1) Yes, the symbolic exp and log functions operate element-wise just like the numeric exp and log functions.
a = 1:10;
as = sym(a);
e = exp(a);
es = exp(as);
format longg
double(e-es) % Small differences
ans = 1×10
1.0e+00 * 2.99524520677138e-16 1.79711394978391e-16 1.82756255255129e-16 -2.87415780158441e-15 -3.48635149004642e-15 -1.23596280244504e-14 -9.86975264043409e-14 2.71032958168736e-14 2.15308776210672e-13 1.37801347005174e-12
2) Yes, there is some additional overhead for the symbolic calculations. How large is "very large" in "very large dimensional vectors"?
daniel adams
daniel adams 2021년 12월 1일
Hi @Steven Lord thank you greatly for your help so far this is a problem that has really been bothering me! For more context can you see my new question ( which is the full version of what I was asking on this post ) https://uk.mathworks.com/matlabcentral/answers/1600419-using-symbolic-math-to-retain-accuracy-arrays By very large the matrices will be of size .

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by