# Mathematical Explanation of log(x^20) and 20 log(x) Plot

**URL:** <https://discourse.julialang.org/t/mathematical-explanation-of-log-x-20-and-20-log-x-plot/101075>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 2, 2023, 10:47am UTC](https://discourse.julialang.org/t/mathematical-explanation-of-log-x-20-and-20-log-x-plot/101075 "2023-07-02T10:47:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 2, 2023, 10:47am UTC](https://discourse.julialang.org/t/mathematical-explanation-of-log-x-20-and-20-log-x-plot/101075/1 "2023-07-02T10:47:07Z")

</div>

Hi all,

There is this weird thing since the formula saying that:

`log (a^b) = b log(a)`

But the plot says different:

 ![Capture d’écran_2023-07-02_17-39-27](https://global.discourse-cdn.com/julialang/original/3X/d/f/df42be8c6b26981a752a5764dc29057a8c760ee0.png)

 ![Capture d’écran_2023-07-02_17-38-39](https://global.discourse-cdn.com/julialang/original/3X/7/5/75175144c3324db8cb6becca90bee8e978a33ff6.png)

But if we use calculator, the plot is correct, since we can plot `log((-5)^20)` just the same as the plot above. But x can’t be negative in `20 log(x)`

```julia
using Plots, LaTeXStrings
#plotlyjs()

f(x) = 20log(x) 
g(x) = 20/x
h(x) = log((x)^20)

plot(f, -10,20, ylims=(-100,100), label=L"y = 20 \ln (x)")
#plot(h, -10,20, ylims=(-100,100), label=L"y = \ln(x^{20})")
plot!(g, label=L"y=\frac{20}{x}")

```

Can anyone explain why the plot of supposedly same equation differs?

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [July 2, 2023, 12:03pm UTC](https://discourse.julialang.org/t/mathematical-explanation-of-log-x-20-and-20-log-x-plot/101075/2 "2023-07-02T12:03:14Z")

</div>

Given \ln(a^b) = b \ln(a) , let a=x and b=20. Then \ln(x^{20}) = 20 \ln x, not 20/x.

EDIT: The title of the post gets it right: log(x^20) and 20 log(x).

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 2, 2023, 12:11pm UTC](https://discourse.julialang.org/t/mathematical-explanation-of-log-x-20-and-20-log-x-plot/101075/3 "2023-07-02T12:11:42Z")

</div>

In short:  
Log is not defined for negative numbers and 0.  
Therefor the equivalence alog(b)=log(b^a) holds only for positive numbers b.

---

<div class="post-metadata">

**Author:** ![vmpyr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vmpyr/32/46406_2.png) [@vmpyr](https://discourse.julialang.org/u/vmpyr)\
**Post date:** [July 2, 2023, 12:13pm UTC](https://discourse.julialang.org/t/mathematical-explanation-of-log-x-20-and-20-log-x-plot/101075/4 "2023-07-02T12:13:18Z")

</div>

This happens because negative values of `x` are a valid argument for `ln(x^20)` because the value inside `ln()` will remain positive anyway. However, for `ln(x)` negative values of `x` are invalid, hence the graph is plotted only on positive side of `x-axis`.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [July 2, 2023, 12:23pm UTC](https://discourse.julialang.org/t/mathematical-explanation-of-log-x-20-and-20-log-x-plot/101075/5 "2023-07-02T12:23:17Z")

</div>

Its not too rare that common known formulas are a bit imprecise and don’t hold in all cases, more often than not this happens when absoulute values are involved.

A better version of that formula would be

```plaintext
log (a^b) = b log(iseven(b) ? abs(a) : a)

```

but that is not as neat and harder to remember.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 2, 2023, 1:12pm UTC](https://discourse.julialang.org/t/mathematical-explanation-of-log-x-20-and-20-log-x-plot/101075/6 "2023-07-02T13:12:58Z")

</div>

\frac{20}{x} is the differentiation of both the \ln (x^{20}) and 20 \ln (x), I put it in the plot to show the difference of plots of \ln (x^{20}) and 20 \ln (x), even if the differentiation is the same

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 2, 2023, 1:16pm UTC](https://discourse.julialang.org/t/mathematical-explanation-of-log-x-20-and-20-log-x-plot/101075/7 "2023-07-02T13:16:05Z")

</div>

If I plot 20 \ln (|x|) the plot will be similar to \ln (x^{20}). It is true that it should be made positive to make the plots similar.
