# Solving a differential equation in a for loop

**URL:** <https://discourse.julialang.org/t/solving-a-differential-equation-in-a-for-loop/110828>\
**Category:** New to Julia\
**Tags:** differentialequation\
**Created:** [February 27, 2024, 7:43am UTC](https://discourse.julialang.org/t/solving-a-differential-equation-in-a-for-loop/110828 "2024-02-27T07:43:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mishal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mishal/32/214462_2.png) [@Mishal](https://discourse.julialang.org/u/Mishal)\
**Post date:** [February 27, 2024, 7:43am UTC](https://discourse.julialang.org/t/solving-a-differential-equation-in-a-for-loop/110828/1 "2024-02-27T07:43:08Z")

</div>

Hi Guys

I have done this before on Matlab however for some reason I can’t get it to run on Julia. I am getting the below error, I think it has something to do with regard to the for loop. Can someone please assist?

ERROR: ArgumentError: invalid index: 5.8e7 of type Float64

```julia
using DifferentialEquations
using Plots
function statespace!(dx, x, p,t)
    l1,l2,m,k,I,c = p
    dx[1] = x[2]
    dx[2] = (-k*x[1]*l1^2-c*x[2])/(m*l2^2+I)
end

x0 = [0.0, 0.025] # initial conditions: rad and rad/s
tspan = (0,1) # time span
k = [58*1000000.00,60*1000000] # N/m
m = 18000.00 # kg
I = 8500*5.15^2.00

for i in k
    p = (5.15,1.5,m,k[i],I,0.04*(k[i]+m)) # parameters: l1,l2,m,k,I,c
    prob = ODEProblem(statespace!, x0, tspan, p)
    sol = solve(prob)
    F_buffer = sol[1,:].*k[i]/1000
    plotly()
    plot(sol[1,:], xlabel="Time", ylabel="Displacement (m)")
    plot(F_buffer,xlabel = "Increment",ylabel = "Force in kN (Buffer)")
end 

```

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [February 27, 2024, 8:01am UTC](https://discourse.julialang.org/t/solving-a-differential-equation-in-a-for-loop/110828/2 "2024-02-27T08:01:49Z")

</div>

> [@Mishal](#):
>
> `k = [58*1000000.00,60*1000000] # N/m`

this is a vector of floats.  
here you are trying to index an array using a float which is not allowed in Julia.

> [@Mishal](#):
>
> ```julia
> for i in k
> p = (5.15,1.5,m,k[i],I,0.04*(k[i]+m)) # parameters: l1,l2,m,k,I,c
> ...
> F_buffer = sol[1,:].*k[i]/1000
> 
> ```

I think youre trying to do the following

```julia
for i in eachindex(k)

```

instead

note the the for loop loops over the elements of the array, not its indices

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [February 27, 2024, 9:07am UTC](https://discourse.julialang.org/t/solving-a-differential-equation-in-a-for-loop/110828/3 "2024-02-27T09:07:57Z")

</div>

@ [Salmon](https://discourse.julialang.org/u/Salmon) has already pointed out the issue.

I did some small changes and pasted result here for your given code.

```julia

using DifferentialEquations
using Plots
function statespace!(dx, x, p,t)
    l1,l2,m,k,I,c = p
    dx[1] = x[2]
    dx[2] = (-k*x[1]*l1^2-c*x[2])/(m*l2^2+I)
end

x0 = [0.0, 0.025] # initial conditions: rad and rad/s
tspan = (0,1) # time span
k = [58*1000000.00,60*1000000] # N/m
m = 18000.00 # kg
I = 8500*5.15^2.00

plt_1, plt_2 = plot(), plot()

for i in 1:length(k)
    p = (5.15,1.5,m,k[i],I,0.04*(k[i]+m)) # parameters: l1,l2,m,k,I,c
    prob = ODEProblem(statespace!, x0, tspan, p)
    sol = solve(prob)
    F_buffer = sol[1,:].*k[i]/1000

    plot!(plt_1, sol[1,:], xlabel="Time", ylabel="Displacement (m)")
    plot!(plt_2, F_buffer,xlabel = "Increment",ylabel = "Force in kN (Buffer)")
end

plot(plt_1, plt_2, size = (1600, 850), thickness_scaling = 1.7, dpi = 300)

```

Result:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/4/24b781615e4ec131d1e8a71f0176961e84d968e6.png)

---

<div class="post-metadata">

**Author:** ![mgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgp/32/28599_2.png) [@mgp](https://discourse.julialang.org/u/mgp)\
**Post date:** [February 27, 2024, 10:11am UTC](https://discourse.julialang.org/t/solving-a-differential-equation-in-a-for-loop/110828/4 "2024-02-27T10:11:16Z")

</div>

`for i in length(k)` is equivalent to `for i = 2`, so it is only running the last version. I think `for i in eachindex(k)` is what you need.

I don’t use Plots.jl but you are plotting both lines on the same axis. The magnitude of one is 60\_000 times the other which is making one of them appear to be unchanging. I think you want to put the two plots on different axes.

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [February 27, 2024, 10:20am UTC](https://discourse.julialang.org/t/solving-a-differential-equation-in-a-for-loop/110828/5 "2024-02-27T10:20:45Z")

</div>

Thank you for pointing that out I have edited the code. 🙂

---

<div class="post-metadata">

**Author:** ![Mishal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mishal/32/214462_2.png) [@Mishal](https://discourse.julialang.org/u/Mishal)\
**Post date:** [February 28, 2024, 8:16am UTC](https://discourse.julialang.org/t/solving-a-differential-equation-in-a-for-loop/110828/6 "2024-02-28T08:16:51Z")

</div>

Thank you all for the assistance
