# Error with power

**URL:** <https://discourse.julialang.org/t/error-with-power/64135>\
**Category:** New to Julia\
**Created:** [July 6, 2021, 8:26am UTC](https://discourse.julialang.org/t/error-with-power/64135 "2021-07-06T08:26:20Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 6, 2021, 8:26am UTC](https://discourse.julialang.org/t/error-with-power/64135/1 "2021-07-06T08:26:20Z")

</div>

I recently began to use Julia for an internship but I’m struggling to make a function that raises to the power of a counter.  
Here’s my code:

```julia
function f(x,y)
   return x*(1.0-x)+y;
end

function g(x,y)
   return x^2
end

function fonction(f,g,k,x0,y0)
   T(f,g)=0
   x=x0
   y=y0
   for i in 1:k
       (x,y) = T^i*(x0,y0)
   end    
   return (x,y)
end        

f(x,y)=x^2+y
g(x,y)=x+y^3
fonction(f,g,3,1,2)

```

At the end of the program, I try to test the function called “fonction” but that’s where I get the error:

```julia
ERROR: LoadError: MethodError: no method matching ^(::var"#T#5", ::Int64)
Closest candidates are:
  ^(::Union{AbstractChar, AbstractString}, ::Integer) at strings/basic.jl:718
  ^(::LinearAlgebra.Symmetric{var"#s6848", S} where {var"#s6848"<:Real, S<:(AbstractMatrix{var"#s6848"} where var"#s6848"<:var"#s6848")}, ::Integer) at C:\Users\Maxime\AppData\Local\Programs\Julia-1.6.1\share\julia\stdlib\v1.6\LinearAlgebra\src\symmetric.jl:868
  ^(::LinearAlgebra.Symmetric{var"#s6848", S} where {var"#s6848"<:Complex, S<:(AbstractMatrix{var"#s6848"} where var"#s6848"<:var"#s6848")}, ::Integer) at C:\Users\Maxime\AppData\Local\Programs\Julia-1.6.1\share\julia\stdlib\v1.6\LinearAlgebra\src\symmetric.jl:869
  ...
Stacktrace:
 [1] fonction(f::Function, g::Function, k::Int64, x0::Int64, y0::Int64)
   @ Main c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:15
 [2] top-level scope
   @ c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:23
in expression starting at c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:23

```

I need help because this is one of my first program in Julia and it seems complicated. Thank you in advance.

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [July 6, 2021, 9:06am UTC](https://discourse.julialang.org/t/error-with-power/64135/2 "2021-07-06T09:06:55Z")

</div>

What do you mean by `T^i`?

---

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 6, 2021, 9:09am UTC](https://discourse.julialang.org/t/error-with-power/64135/3 "2021-07-06T09:09:59Z")

</div>

T is a function that goes from R^2 to R, I need it to have for any k like this :

```julia
(x_k,y_k):=T^k(x_0,y_0)

```

Where x\_0 and y\_0 are initial values.

T is a dynamic system. I struggle to understand the subject myself.

---

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 6, 2021, 9:11am UTC](https://discourse.julialang.org/t/error-with-power/64135/4 "2021-07-06T09:11:10Z")

</div>

I initiated it at 0 but I don’t know if it’s the right thing to do.

---

<div class="post-metadata">

**Author:** ![ess3sq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ess3sq/32/26834_2.png) [@ess3sq](https://discourse.julialang.org/u/ess3sq)\
**Post date:** [July 6, 2021, 9:16am UTC](https://discourse.julialang.org/t/error-with-power/64135/5 "2021-07-06T09:16:10Z")

</div>

The power should go after the function call, as in `T(x_0, y_0)^k`. Also, it seems to me that the function will always be zero is defined this way. Are you sure you don’t mean to keep some kind of counter variable? Why do you need the function `T` at all? (As in, how is `T` related to its parameters `x_0`, `y_0`?

---

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 6, 2021, 9:28am UTC](https://discourse.julialang.org/t/error-with-power/64135/6 "2021-07-06T09:28:19Z")

</div>

Actually, it is said in the subject that  
`T:= (f,g)`  
I should have initiated it with these values.  
T is directly at the power `i*(x0,y0)`  
I think I need a counter only to return `(x_k,y_k):=T^k(x_0,y_0)`

---

<div class="post-metadata">

**Author:** ![ess3sq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ess3sq/32/26834_2.png) [@ess3sq](https://discourse.julialang.org/u/ess3sq)\
**Post date:** [July 6, 2021, 10:20am UTC](https://discourse.julialang.org/t/error-with-power/64135/7 "2021-07-06T10:20:37Z")

</div>

I think there might be some confusion on both ends because when you write `T(f,g)=0` you are not initializing some kind of counter/loop variable called `T`, but are instead declaring a constant function called `T` which takes two parameters and always returns `0`. Is this _really_ what you want? I am guessing not.

Therefore, when you write `(x,y) = T^i*(x0,y0)`, you are trying to take a function pointer to the function `T`, exponentiate it to `i` (which is invalid syntax) and then multiply the “result” by a tuple.

> [@Broti](#):
>
> I initiated it at 0 but I don’t know if it’s the right thing to do.

Is `T` ever supposed to be different from the initial values? You don’t seem to change its value in the loop. I must confess I am confused by your intentions, could you consider writing your algorithm in pseudo-code or something?

---

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 6, 2021, 12:17pm UTC](https://discourse.julialang.org/t/error-with-power/64135/8 "2021-07-06T12:17:20Z")

</div>

I’ll try to do my best to write it in pseudo code.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 6, 2021, 12:47pm UTC](https://discourse.julialang.org/t/error-with-power/64135/10 "2021-07-06T12:47:16Z")

</div>

If `T` is a function that goes from R^2 to R, I cannot see how its power can give the tuple according to

```julia
(x_k,y_k):=T^k(x_0,y_0)

```

---

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 6, 2021, 1:00pm UTC](https://discourse.julialang.org/t/error-with-power/64135/11 "2021-07-06T13:00:25Z")

</div>

T is a dynamic, but I didn’t understand what was it. I talked to my professor and now I understand, I have no error for now. thanks for answering.

---

<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 6, 2021, 1:01pm UTC](https://discourse.julialang.org/t/error-with-power/64135/12 "2021-07-06T13:01:29Z")

</div>

How about

```julia
julia> function iterate(f,k,x)
           for i=1:k
               x = f(x)
           end
           x
       end
iterate (generic function with 1 method)

julia> function f(x)
           [x[1]*(1 - x[1]) + x[2]; x[1]^2]
       end
f (generic function with 1 method)

julia> iterate(f,3,[1;2])
2-element Vector{Int64}:
 2
 1

```

---

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 6, 2021, 1:03pm UTC](https://discourse.julialang.org/t/error-with-power/64135/13 "2021-07-06T13:03:27Z")

</div>

I was given a simpler function by my professor to understand what he wanted from me. My final code is:

```julia
function fonction(f,g,k,x0,y0)
    x=x0
    y=y0
    T(x,y)=(x,y)
    for i in 1:k
        x_temp=x
        y_temp=y
        x=f(x,y)
        y=g(x_temp,y)
        T(x_temp,y_temp) = (x,y)
    end    
    
    return T(x,y)
end        

f(x,y)=x^2+y
g(x,y)=x+y

fonction(f,g,3,1,2)

```

Thank you to all people who answered me, I’m really grateful for that. It’s my first post on this forum and probably not the last.

---

<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 6, 2021, 3:17pm UTC](https://discourse.julialang.org/t/error-with-power/64135/14 "2021-07-06T15:17:59Z")

</div>

You should fix one main problem before calling this done. Your function `T` is not actually accomplishing anything. You first define a function `T` to be the identity, mapping the tuple `x,y` to itself. Then within the loop you redefine `T` to be a map from two dummy variables to the constant tuple `(x,y)` where `x` and `y` are the values computed in the previous two lines. This is hinted at by the warning you get when running your code.

```julia
`WARNING: Method definition T(Any, Any) in module Main at REPL[1]:4 overwritten at REPL[1]:10.
fonction (generic function with 1 method)

```

A cleaned-up version of your code without the unnecessary `T` function and removal of a few unnecessary temporary variables is

```julia
function fonction(f,g,k,x,y)
    for i in 1:k
        x_temp=x
        x=f(x,y)
        y=g(x_temp,y)
    end    
    
    return (x,y)
end        

f(x,y)=x^2+y
g(x,y)=x+y

fonction(f,g,3,1,2)

```

In the long run you’ll be better off using Vectors for working with multidimensional data like I showed in my previous post, rather than fixed-dimensional tuples like `(x,y)`. They’re more general, and all the library code for the math of \mathbb{R}^n is written in terms of Vectors: linear algebra, solutions of systems of equations, iterated maps, differential eqns, etc.

---

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 7, 2021, 9:06am UTC](https://discourse.julialang.org/t/error-with-power/64135/15 "2021-07-07T09:06:47Z")

</div>

Thank you I replaced it in my program and this works perfectly. Thank you so much !
