Default User
Default User

Reputation: 11

How I access value of diff_sol in given example(on terminal)? when I try it says diff_sol undefined

for j in 1:10
    δ= y[j,:]

    diff_prob = ODEProblem(Proteo_Deg, u0,t_span,δ)

    diff_sol = solve(diff_prob)
end

Upvotes: 1

Views: 35

Answers (1)

Sundar R
Sundar R

Reputation: 14705

A for loop creates a local scope, and so new variables that you introduce in a loop only exist within that loop.

A common way around this is to initialize the variable before the loop. (Outside of the REPL, you generally want to avoid global variables, so for eg. if this is in a script file, place both the initialization and then the loop within a function.) One thing to consider is what your goal with the loop is. Currently, you're computing 10 ODE solutions, but then throwing them out because they're all assigned to the same variable diff_sol. Maybe you want to save them in an array?

diff_solutions = Array{ODESolution}(undef, 10)
for j in 1:10 
  δ = y[j,:]
  diff_prob = ODEProblem(Proteo_Deg, u0,t_span,δ)
  diff_solutions[j] = solve(diff_prob)
end

Now, diff_solutions contains all 10 ODE solutions and is available to access outside of the loop too. If this is in a function, you can then return diff_solutions after the loop.

Upvotes: 1

Related Questions