# Error using Peaks function to get maximum of ODE solution

**URL:** <https://discourse.julialang.org/t/error-using-peaks-function-to-get-maximum-of-ode-solution/84631>\
**Category:** New to Julia\
**Created:** [July 22, 2022, 10:18am UTC](https://discourse.julialang.org/t/error-using-peaks-function-to-get-maximum-of-ode-solution/84631 "2022-07-22T10:18:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)\
**Post date:** [July 22, 2022, 10:18am UTC](https://discourse.julialang.org/t/error-using-peaks-function-to-get-maximum-of-ode-solution/84631/1 "2022-07-22T10:18:39Z")

</div>

I am trying to get the maximum of a plot from ODE solve using Peaks function. I need help cause i get error. Here is my code. Thanks.

using DifferentialEquations  
using Plots ; gr()

function f(du,u,p,t)  
du[1] = dx = -p[1]\*u[1]\*u[2]  
du[2] = dy = p[1]\*u[1]\*u[2] - p[2]\*u[2]  
du[3] = dz = p[2]\*u[2]  
end

u0 = [738.0;1.0;0]  
tspan = (0.0,14.0)  
p = [0.00237,0.465]  
prob = ODEProblem(f,u0,tspan,p)  
sol = solve(prob,abstol=1e-8,reltol=1e-8,Tsit5(),save\_idxs=2)  
plot(sol,linewidth=2)

# using peaks command to get the maximum of the plotted graph (using maximum command does not work in some cases)

using Peaks  
sol2 = solve(prob,abstol=1e-8,reltol=1e-8,Tsit5())  
g = (t) → sol2(t,idxs=2)  
pks1,vals1 = findmaxima(g)

---

<div class="post-metadata">

**Author:** ![liamfdoherty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamfdoherty/32/24516_2.png) [@liamfdoherty](https://discourse.julialang.org/u/liamfdoherty)\
**Post date:** [July 22, 2022, 5:08pm UTC](https://discourse.julialang.org/t/error-using-peaks-function-to-get-maximum-of-ode-solution/84631/2 "2022-07-22T17:08:19Z")

</div>

Can you provide an example of code where calling `maximum(sol.u)` does not work after running your ODE solver? I don’t see a reason why that would fail.

The Peaks code you provided seems to find the maximum of a vector, not a function. If you wanted to find a maximum of a function (as a general tip), I would suggest taking a look at SciML’s [Optimization.jl](https://github.com/SciML/Optimization.jl) package.

In the future, it is also helpful if you provide the stacktrace (i.e., the error message). That helps us figure out what part of your code is raising the error and gets to the solution a little faster.

---

<div class="post-metadata">

**Author:** ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)\
**Post date:** [July 23, 2022, 7:31am UTC](https://discourse.julialang.org/t/error-using-peaks-function-to-get-maximum-of-ode-solution/84631/3 "2022-07-23T07:31:42Z")

</div>

Thank you.
