Why I have wrong result in (power) function?
Mostrar comentarios más antiguos
Hi all,
I have the following statement which generate wrong result:
temp1 = power(x,temp1)
The wrong result is:
temp1 =
Inf
if I change ( temp1 ) to a number it works! but f I use a variable (temp), it does not work.
Any help?
Thank you
Respuestas (1)
Star Strider
el 7 de Nov. de 2014
1 voto
The variables x and temp1 have to be numeric in core MATLAB, as it seems you are doing.
If you’re doing symbolic calculations, you need to use the Symbolic Math Toolbox.
7 comentarios
Abdulatif Alabdulatif
el 7 de Nov. de 2014
Star Strider
el 7 de Nov. de 2014
If I do not define ‘temp1’, it throws an error. Since it is not throwing an error in your code, you have probably defined ‘temp1’ as some large number earlier in your code, which is the reason it is producing an infinite result.
If you define for instance:
x = 2;
temp1 = 10;
temp1 = power(x,temp1)
produces:
temp1
1024
Note that if you define ‘temp1’ as a matrix argument, power produces a matrix as output.
Abdulatif Alabdulatif
el 8 de Nov. de 2014
Mike Hosea
el 8 de Nov. de 2014
There is nothing to solve yet. 3^25111 is a useful representation of a certain integer that happens to be too big to represent in IEEE double precision floating point. If you need a representation of the number for symbolic purposes 3^25111 will do. The log base 3 is 25111, obviously. The log base 10 is 25111*log10(3) = 11980.9918273. So that means that if the number could be represented in floating point, it would look like (10^0.9918273)*10^11980 = 9.813575e11980.
Star Strider
el 8 de Nov. de 2014
Unfortunately, you cannot unless you are willing to use logarithms.
Since 3^25111 = 10^11981 = 2^39800, this is far greater than the ability of double-precision MATLAB variables to represent.
logtemp1 = 25111*log(3)
produces:
logtemp1 =
27587.2531807449
that of course means you will have to do everything else in that calculation in log terms, then take the antilogs when you are finished.
Abdulatif Alabdulatif
el 8 de Nov. de 2014
Editada: Abdulatif Alabdulatif
el 8 de Nov. de 2014
Star Strider
el 8 de Nov. de 2014
I doubt that what you want to do is possible. At least I can’t imagine how to do it, because it would involve both division and subtraction, and that would involve raising some multiple of 7 far beyond the ability of MATLAB floating-point precision in order to do the subtraction. (I doubt if it would even be possible to factor it.)
I don’t know how you could do this with a for loop, because you could not represent 3^25111 in MATLAB at all, to the best of my knowledge. I can envision doing it in assembler with serial multiplications of 3 then serial divisions by 7, but that isn’t something I’d want to do.
If you did manage to solve this with for loops, please post your code.
Categorías
Más información sobre Number Theory en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!