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?

2 vues (au cours des 30 derniers jours)
alpha = [0.01 0.01 0.01 0.01]
lambda = 195
for i = 1 : 4
K(i) = alpha(i).^lambda;
end

Réponse acceptée

madhan ravi
madhan ravi le 14 Jan 2019
Modifié(e) : madhan ravi le 14 Jan 2019
sym(alpha).^lambda % no loops needed
Gives:
ans =
1/1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,... % same repeats likewise
  13 commentaires
Stephen23
Stephen23 le 16 Jan 2019
Modifié(e) : Stephen23 le 16 Jan 2019
@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.

Connectez-vous pour commenter.

Plus de réponses (1)

Stephen23
Stephen23 le 14 Jan 2019
Modifié(e) : Stephen23 le 14 Jan 2019
"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 commentaires
Walter Roberson
Walter Roberson le 16 Jan 2019
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

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by