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 ビュー (過去 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.
Walter Roberson
Walter Roberson 2019 年 1 月 16 日
There is some additional material on the treatment of symbolic floating point at https://www.mathworks.com/help/symbolic/mupad_ref/digits.html

サインインしてコメントする。

その他の回答 (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 件のコメント
GHUFRAN AHMAD KHAN
GHUFRAN AHMAD KHAN 2019 年 1 月 16 日
can u tell that how i can do floating point error.
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