Error with power

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:

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:

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.

What do you mean by T^i?

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

(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.

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

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?

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)

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.

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?

1 Like

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

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

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

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.

How about

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
1 Like

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

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.

2 Likes

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.

`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

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.

7 Likes

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

1 Like