# Bounds Error In code

**URL:** <https://discourse.julialang.org/t/bounds-error-in-code/111766>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 18, 2024, 12:17pm UTC](https://discourse.julialang.org/t/bounds-error-in-code/111766 "2024-03-18T12:17:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bongodrums4659](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bongodrums4659/32/207822_2.png) [@bongodrums4659](https://discourse.julialang.org/u/bongodrums4659)\
**Post date:** [March 18, 2024, 12:17pm UTC](https://discourse.julialang.org/t/bounds-error-in-code/111766/1 "2024-03-18T12:17:58Z")

</div>

Hi!

I keep getting a bounds error and im not sure how to fix it

```julia

# Define a function to simulate worker paths
function simulate_worker_paths(solution, a0, y0, T, N)
    @unpack kgrid, ygrid, policy_c, policy_k, P = solution
    
    # Find the indices of a0 and y0 in the grids
    a0_idx = searchsortedfirst(kgrid, a0)
    y0_idx = searchsortedfirst(ygrid, y0)
    
    # Initialize arrays to store the paths
    a_paths = zeros(T, N)
    y_paths = zeros(T, N)
    c_paths = zeros(T, N)
    
    # Set the initial values
    a_paths[1, :] .= a0  
    y_paths[1, :] .= y0  
    
    # Simulate the paths
    for t in 2:T
        # Draw random values for y
        z = rand(N)
        y_idx = searchsortedfirst(cumsum(P[y0_idx, :]), z)
        y_paths[t, :] .= ygrid[y_idx]  
        # Update asset and consumption paths
        for n in 1:N
            # Check bounds before accessing elements
            if a0_idx > length(kgrid)
                a0_idx = length(kgrid)
            end
            if y_idx[n] > length(ygrid)
                y_idx[n] = length(ygrid)
            end
            a_paths[t, n] = kgrid[policy_k[a0_idx, y_idx[n]]]  
            c_paths[t, n] = policy_c[a0_idx, y_idx[n]]  
        end        

        # Update the indices for the next period
        a0_idx = searchsortedfirst(kgrid, a_paths[t, 1]) # Assuming all paths start from the same initial asset level
        y0_idx = y_idx
    end
    
    return a_paths, y_paths, c_paths
end

# Define a function to plot asset, income, and consumption paths
function plot_worker_paths(solution, a0, y0, T, N)
    # Simulate worker paths
    a_paths, y_paths, c_paths = simulate_worker_paths(solution, a0, y0, T, N)
    
    # Plot the paths
    p1 = plot(legend=:outerbottom, xlabel="Period", ylabel="Asset", title="Asset Paths")
    p2 = plot(legend=:outerbottom, xlabel="Period", ylabel="Income", title="Income Paths")
    p3 = plot(legend=:outerbottom, xlabel="Period", ylabel="Consumption", title="Consumption Paths")
    
    for n in 1:N
        plot!(p1, 1:T, a_paths[:, n], label="Worker $n")
        plot!(p2, 1:T, y_paths[:, n], label="Worker $n")
        plot!(p3, 1:T, c_paths[:, n], label="Worker $n")
    end
    
    plt = plot(p1, p2, p3, layout=(1,3), size=(900, 400))
    return plt
end

# Set initial conditions and simulation parameters
a0 = kgrid[Int(length(kgrid)/2)] # Starting point at the midpoint of asset grid
y0 = exp(mean(solution.ygrid)) # Starting point at the midpoint of income grid
T = 100 # Number of model periods
N = 5 # Number of sample paths

# Plot worker paths
plot_worker_paths(solution, a0, y0, T, N)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 18, 2024, 12:22pm UTC](https://discourse.julialang.org/t/bounds-error-in-code/111766/2 "2024-03-18T12:22:41Z")

</div>

The answer to your question is generally yes, but likely only after you’ve read

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

and adapted your post accordingly.

Key things:

- Don’t post screenshots
- Make your code reproducible (i.e. don’t use variables defined outside the code you’re posting, create dummy data if necessary)
- Tell people what the actual problem is

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [March 18, 2024, 1:42pm UTC](https://discourse.julialang.org/t/bounds-error-in-code/111766/3 "2024-03-18T13:42:08Z")

</div>

AS @nilshg says - please post the code using backticks. Anyone trying to help can then cut-and-paste into the REPL.  
Looking quickly at the screenshot you do not have any using statements to include packages.

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [March 18, 2024, 4:30pm UTC](https://discourse.julialang.org/t/bounds-error-in-code/111766/4 "2024-03-18T16:30:15Z")

</div>

I see that you’ve updated your original post to include the source code from the screenshot. Can you include the exact error message you’re getting. It’s hard to diagnose the problem if we don’t know where in this code the error gets thrown from.
