i have one variable which has value in an array and i want to make the power of all the value of array by another variable but getting value zero.any possible solution?

조회 수: 1 (최근 30일)
alpha = [0.01 0.01 0.01 0.01]
lambda = 195
for i = 1 : 4
K(i) = alpha(i).^lambda;
end

채택된 답변

madhan ravi
madhan ravi 2019년 1월 14일
편집: madhan ravi 2019년 1월 14일
sym(alpha).^lambda % no loops needed
Gives:
ans =
1/1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,... % same repeats likewise
  댓글 수: 13
Stephen23
Stephen23 2019년 1월 16일
편집: Stephen23 2019년 1월 16일
@Walter Roberson: thank you for the detailed explanation. To be honest I am surprised by the documentation. You wrote (emphasis added) "If you use sym('0.01') then symbolic floating point would be used . This is not a decimal based system . The internal encoding is not documented ... "
If this is not documented, how are users supposed to know that sym('0.01') is worse than supplying an imprecise double value? You yourself might have had many opportunities to "prod it with a stick really hard", but this does not seem like a reliable way to convey such information to users, especially when the documentation contains examples like these:
inaccurateNum = sym(11111111111111111111)
accurateNum = sym('11111111111111111111')
and when it explains that "Statements like pi = sym('pi') and delta = sym('1/10') create symbolic numbers that avoid the floating-point approximations inherent in the values of pi and 1/10.", then any reasonable reader would also expect that to mean that sym('0.01') would also be exact. I am surprised that the documentation is so vague on this.

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

추가 답변 (1개)

Stephen23
Stephen23 2019년 1월 14일
편집: Stephen23 2019년 1월 14일
"any possible solution?"
Solution to what, exactly? You calculate this (the loop is not required):
>> alpha.^lambda
ans =
0 0 0 0
and the output value of zero is expected. Lets see why
>> alpha % alpha.^1
alpha =
1.0000e-002 1.0000e-002 1.0000e-002 1.0000e-002
>> alpha.*alpha % alpha.^2
ans =
1.0000e-004 1.0000e-004 1.0000e-004 1.0000e-004
>> alpha.*alpha.*alpha % alpha.^3
ans =
1.0000e-006 1.0000e-006 1.0000e-006 1.0000e-006
>> alpha.*alpha.*alpha.*alpha % alpha.^4
ans =
1.0000e-008 1.0000e-008 1.0000e-008 1.0000e-008
...etc
It is clear that with alpha.^195 you would have values beyond anything that can be represented using double floating point numbers:
>> 2*195
ans = 390
>> 1e-390
ans = 0
>> realmin % smallest DOUBLE value
ans = 2.2251e-308
If you really need to calculate this value then try a higher precision numeric class:
or the symbolic toolbox:
or change your algorithm.
  댓글 수: 4
Walter Roberson
Walter Roberson 2019년 1월 16일
Work in log space.
A^B = exp(B * log(A)) so log(A^B) = B * log(A) .
196 * log(0.01) is easy to compute in floating point: it is about -902. The smallest number that double precision can represent has a log of about -744

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

Community Treasure Hunt

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

Start Hunting!

Translated by