# 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 │
``````