# What is causing this undefined variable Error message

**URL:** <https://discourse.julialang.org/t/what-is-causing-this-undefined-variable-error-message/122429>\
**Category:** New to Julia\
**Created:** [November 8, 2024, 6:17pm UTC](https://discourse.julialang.org/t/what-is-causing-this-undefined-variable-error-message/122429 "2024-11-08T18:17:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anika\_Tarannum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anika_tarannum/32/212011_2.png) [@Anika\_Tarannum](https://discourse.julialang.org/u/Anika_Tarannum)\
**Post date:** [November 8, 2024, 6:17pm UTC](https://discourse.julialang.org/t/what-is-causing-this-undefined-variable-error-message/122429/1 "2024-11-08T18:17:52Z")

</div>

# Define parameters

operating\_cost = [4000, 4200, 4800, 5800, 7200, 9000]  
salvage\_value = [10500, 7300, 5100, 3600, 2500, 1600]  
replacement\_cost = 15000  
maintenance\_cost = 3500  
years = 5

# Initialize DP table

# C[t][a] will store the minimum cost for year t with drone age a

C = Array{Float64}(undef, years + 1, length(operating\_cost))

# Initialize end of planning horizon

for a in 1:length(operating\_cost)  
C[years + 1, a] = 0 # Boundary condition: No future costs after year 5  
end

# DP recursion to fill the table from the last year backwards

for t in years:-1:1  
for a in 1:length(operating\_cost)  
# Cost of keeping the drone  
keep\_cost = operating\_cost[a] + C[t + 1, min(a + 1, length(operating\_cost))]

```
    # Cost of performing maintenance
    new_age_after_maintenance = max(1, a - 2)
    total_maintenance_cost = maintenance_cost + operating_cost[new_age_after_maintenance]
    maintenance_option_cost = total_maintenance_cost + C[t + 1, new_age_after_maintenance]

    # Cost of replacing the drone
    replace_cost = replacement_cost - salvage_value[a] + C[t + 1, 1]

    # Store the minimum cost for year t and age a
    C[t, a] = minimum([keep_cost, maintenance_option_cost, replace_cost])
end

```

end

# Output the minimum cost to start from year 1 with the current age of 2 years

optimal\_cost = C[1, 3] # Drone starts at age 2, so we look at the third column (age 2)  
println("The minimum cumulative cost for the optimal replacement policy is: ", optimal\_cost)

# Display the DP table with intermediate calculations

println(“Dynamic Programming Table (Minimum Costs):”)  
for t in 1:years+1  
println("Year $t: ", C[t, :])  
end

# Traceback for Optimal Path

println(“\nTracing Optimal Replacement Strategy:”)

# Define initial age at the start of the traceback section

current\_age = 3 # Initial index for age 2 (third column in DP table)

for t in 1:years  
keep\_cost = operating\_cost[current\_age] + C[t + 1, min(current\_age + 1, length(operating\_cost))]  
new\_age\_after\_maintenance = max(1, current\_age - 2)  
maintenance\_option\_cost = maintenance\_cost + operating\_cost[new\_age\_after\_maintenance] + C[t + 1, new\_age\_after\_maintenance]  
replace\_cost = replacement\_cost - salvage\_value[current\_age] + C[t + 1, 1]

```
# Determine the action taken at this stage
optimal_action_cost = C[t, current_age]
if optimal_action_cost == keep_cost
    println("Year $t: Keep the drone (age ", current_age, ")")
    current_age = min(current_age + 1, length(operating_cost))
elseif optimal_action_cost == maintenance_option_cost
    println("Year $t: Perform maintenance (reset to age ", new_age_after_maintenance, ")")
    current_age = new_age_after_maintenance
else
    println("Year $t: Replace the drone (new age 1)")
    current_age = 1
end

```

end

whenever I try this code running it gives error for age/ current\_age not defined

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [November 8, 2024, 6:36pm UTC](https://discourse.julialang.org/t/what-is-causing-this-undefined-variable-error-message/122429/2 "2024-11-08T18:36:22Z")

</div>

Hi,

Currently the code you posted is not properly formatted, making it hard to read. Please edit it, cf. instructions here:

> [@PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530):
>
> This is a short post on how to use backticks (` and ```) to quote code, so it is easy to read. This post can be linked to new users who are confused by this feature. Why By quoting your code, you get a monospaced font (which preserves indentation) and syntax highlighting. This makes it easier to read your code and help you. Displayed code looks like this: function displayed\_code(x::Int, y::Int) if x \< y println("x is smaller") else println("or not") end end Inline…

Additionally, it would also help if you would post the full error message, and reduce the code as much as possible to obtain a minimal working example (resulting in the same error). Check out

> [@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 …

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [November 8, 2024, 7:40pm UTC](https://discourse.julialang.org/t/what-is-causing-this-undefined-variable-error-message/122429/3 "2024-11-08T19:40:01Z")

</div>

For what it’s worth, your code runs for me with output

```julia
Year 1: Perform maintenance (reset to age 1)
Year 2: Replace the drone (new age 1)
Year 3: Replace the drone (new age 1)
Year 4: Keep the drone (age 1)
Year 5: Keep the drone (age 2)

```

A couple of style suggestions:

- wrap each step in a function with clear inputs and outputs (to avoid side effects and clarify intent)
- avoid nonconstant globals
