Plot using for loop

I have ode like this

Now if I need the result for a series of different values of the parameter “p”, I was trying to use for loop to do that, here is the code

Which shows the following error!

Please help me to solve this. I also have some other concern and will share once it gets resolve.
Thank you all.

1 Like

These are just warnings and you can get rid of them by doing what is suggested in the warnings!

1 Like

I wrapped your code in a function and ran it in the REPL. There were no errors.
I am using Julia 1.6.2 on Windows 10.

Regards,
Greg

Hi Greg,
In REPL it is working But I am unable to generate a single figure!

The following code in REPL

using DifferentialEquations
f(u,p,t) = 1.01u + pu^(1.1)
u0 =1/2
tspan = (0.0,1.0)
for p in 1:5

 prob = ODEProblem(f,u0,tspan,p)
 sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
 using Plots
 P=plot(sol)
 display(P)

end

Showing 5 different figures, but I need a single graph! Don’t know how to do that.

Regards,
Phoni

using Plots
P = plot()
for p in 1:5
 prob = ODEProblem(f,u0,tspan,p)
 sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
 plot!(P,sol)
end
display(P)
1 Like

Thank you very much @Jeff_Emanuel

I was trying to plot the last value of the solution versus the recurring parameter using the following code

using DifferentialEquations

f(u,p,t) = 1.01u + pu^(1.1)

u0 =1/2

tspan = (0.0,1.0)

using Plots

PP = plot()

for p in 1:0.01:2

 prob = ODEProblem(f,u0,tspan,p)

 sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)

 SL=sol[1,:]

 S=[p SL[end]]

 scatter!(PP,S,legend=false)

end

display(PP)

savefig(“sct.png”)

which leads the following figure
sct

But according to the data stored as in “S = [p SL[end]]” it should be like
sct_tm

I was unable to resolve this problem!!

Can I use plot! command instead of scatter!? because using plot! command nothing was appear in the figure.

I made things more explicit:

Blockquote
f(u,p,t) = 1.01u + pu^(1.1)

u0 =1/2

tspan = (0.0,1.0)

x =

y =

for p in 1:0.01:2

prob = ODEProblem(f,u0,tspan,p)

sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)

SL=sol[1,:]

S=[p SL[end]]

push!(x, p)

push!(y, SL[end])

end

print(x)

PP = scatter(x, y, legend=false)

display(PP)

savefig(“sct.png”)

Blockquote

Regards,
Greg

sct

To obtain the desired result with Plots.jl, this line should be written as a Tuple:

S=(p, SL[end])

NB:
The code in previous posts should be inserted between back-ticks

Thanks @GregR and @rafael.guerra for your input