# Repeat with y as k

``````d = .05
n = .01
g = .015
s = .3
k=6
yn( k, s, d, g,) = k-(d+n+g)*k + s*k^(1/3)
``````

I have this function, and it calculates yn= 6.09514 . Now I would like to replace k with yn, and find a new yn, and repeat that 50 times.

Use a `for`-loop?

``````julia> function do_stuff(N)
d = 0.05
n = 0.01
g = 0.015
s = 0.3
k = 6.0
yn(k, s, d, g) = k - (d + n + g) * k + s * k^(1/3)
for _ in 1:N
k = yn(k, s, d, g)
end
return k
end
do_stuff (generic function with 1 method)

julia> do_stuff(1)
6.095136177849642

julia> do_stuff(50)
7.839026641775517
``````
4 Likes

Thanks, that was easy, Iโll be sure to remember than in the future. Now I need to put each value of do_stuff in a data frame, that shows each value of do_stuff.

``````function do_stuffdf(N)
d = 0.05
n = 0.01
g = 0.015
s = 0.3
k = 6.0
yn(k, s, d, g) = k - (d + n + g) * k + s * k^(1/3)

df = DataFrame([Float64], [:k], 0)
for _ in 1:N
k = yn(k, s, d, g)
push!(df, (k,))
end
return df
end
``````

e.g.

1 Like

itโs saying k not defined, not sure why, since I started with k=6 and keep repeating k.

Ok, yeah I put in the whole think, but it still only gives me one point.

Oh, to get the DataFrame just define the function as posted and do:

``````mydataframe = do_stuffdf(50)
``````
1 Like

[There was a warning that you should not push Float64 into a DataFrame? I changed `push!(df, k)`to `push!(df, (k,))` and the warning is gone.]
One point?

``````julia> do_stuffdf(50)
50ร1 DataFrame
โ Row โ k       โ
โ     โ Float64 โ
โโโโโโโผโโโโโโโโโโค
โ 1   โ 6.09514 โ
โ 2   โ 6.186   โ
โ 3   โ 6.27277 โ
โ 4   โ 6.35558 โ
โ 5   โ 6.43461 โ
โ 6   โ 6.51001 โ
โ 7   โ 6.58192 โ
โ 8   โ 6.6505  โ
โฎ
โ 42  โ 7.7578  โ
โ 43  โ 7.76985 โ
โ 44  โ 7.7813  โ
โ 45  โ 7.79218 โ
โ 46  โ 7.80253 โ
โ 47  โ 7.81236 โ
โ 48  โ 7.8217  โ
โ 49  โ 7.83059 โ
โ 50  โ 7.83903 โ
``````
1 Like

Cool, work perfect, except that it starts with k =0, instead of k=6

The first `k` was missing. So this is an alternative, and `k` starts with 6.0

``````julia> function do_stuffdf(N)
d = 0.05
n = 0.01
g = 0.015
s = 0.3
k = 6.0
yn(k, s, d, g) = k - (d + n + g) * k + s * k^(1/3)

df = DataFrame(k = k)
for _ in 1:N
k = yn(k, s, d, g)
push!(df, (k,))
end
return df
end
do_stuffdf (generic function with 1 method)

julia> do_stuffdf(50)
51ร1 DataFrame
โ Row โ k       โ
โ     โ Float64 โ
โโโโโโโผโโโโโโโโโโค
โ 1   โ 6.0     โ
โ 2   โ 6.09514 โ
โ 3   โ 6.186   โ
โ 4   โ 6.27277 โ
โ 5   โ 6.35558 โ
โ 6   โ 6.43461 โ
โ 7   โ 6.51001 โ
โ 8   โ 6.58192 โ
โฎ
โ 43  โ 7.7578  โ
โ 44  โ 7.76985 โ
โ 45  โ 7.7813  โ
โ 46  โ 7.79218 โ
โ 47  โ 7.80253 โ
โ 48  โ 7.81236 โ
โ 49  โ 7.8217  โ
โ 50  โ 7.83059 โ
โ 51  โ 7.83903 โ
``````

AWESOME!!! Thatโs it.

If you want to create the DataFrame at top-level, you can put `k` in the context of the definition of `yn`:

``````julia> let k = 6.0
global yn, setk
function yn()
d = 0.05
n = 0.01
g = 0.015
s = 0.3
r = k
k = k - (d + n + g) * k + s * k^(1/3)
return r
end

setk(kinit) = (k = kinit)
end
setk (generic function with 1 method)

julia> setk(6.0)
6.0

julia> dfyn = DataFrame(yn = [yn() for i in 1:50])
50ร1 DataFrame
โ Row โ yn      โ
โ     โ Float64 โ
โโโโโโโผโโโโโโโโโโค
โ 1   โ 6.0     โ
โ 2   โ 6.09514 โ
โ 3   โ 6.186   โ
โ 4   โ 6.27277 โ
โ 5   โ 6.35558 โ
โ 6   โ 6.43461 โ
โ 7   โ 6.51001 โ
โ 8   โ 6.58192 โ
โฎ
โ 42  โ 7.74512 โ
โ 43  โ 7.7578  โ
โ 44  โ 7.76985 โ
โ 45  โ 7.7813  โ
โ 46  โ 7.79218 โ
โ 47  โ 7.80253 โ
โ 48  โ 7.81236 โ
โ 49  โ 7.8217  โ
โ 50  โ 7.83059 โ
``````