Welcome by the way! I used to use Python a lot myself. Julia is great, but
it does require a fair amount of extra learning due to its youth. I hope
you get past the rocky start :)
```
using PyPlot
using PyCall
@pyimport scipy.integrate as integrate
function f(y, t)
if t < 25
return [y[2], -8*y[1] + 0.1*y[2]]
elseif 25 < t < 45
return [y[2], -8*y[1]]
else
return [y[2], -8*y[1] - 0.1*y[2]]
end
end
t = linspace(0, 35, 1000)
y = [0.01, 0]
y1 = integrate.odeint(f, y, t)
plot(t, y1)
xlim(0, 35)
ylim(-0.2, 0.2)
```
This is what I would do to make your code work. Some of the issues was that
you were using 0-based indexing (C like, python etc) so I just
changed all the lines that had things like y[0] -> y[1] and y[1] -> y[2].
Also you should be careful about following python idioms to closely in
Julia. Doing things like
plt = PyPlot
is likely not the correct idea. I have shown what I consider idiomatic use
of PyPlot would look like in this case.
Final question. Are you using the python integrators because you want the
LSODE codes specifically? You could of course use the pure Julia package
ODE.jl if you can deal with using a Runge-Kutta algorithm instead (Adams
methods are coming soon!)
Here is your code using that package instead just for comparison (with some
naming changes to be more awesome in my mind ;)
using ODE
```
using ODE
function f(t, y)
if t < 25
return [y[2], -8*y[1] + 0.1*y[2]]
elseif 25 < t < 45
return [y[2], -8*y[1]]
else
return [y[2], -8*y[1] - 0.1*y[2]]
end
end
tspan = linspace(0, 35, 1000)
y0 = [0.01, 0]
t, y = ode45(f, y0, tspan)
plot(t, y)
xlim(0, 35)
ylim(-0.2, 0.2)
```
Good luck!
On Monday, June 20, 2016 at 1:41:41 PM UTC-7, Henri Girard wrote:
>
> y1 (odeint arrays has nothing inside) that's why there is an error but
> from what ?
>
> Le lundi 20 juin 2016 17:32:07 UTC+2, Henri Girard a écrit :
>>
>> I am trying to convert from python to julia, but I don't know how to use
>> y1=integrate.odeint(f,t), apparently I should have add a derivativ ?
>>
>> --python----------------------
>> from scipy.integrate import odeint
>> def f(y,t):
>> if t<25:
>> return [y[1],-8*y[0]+0.1*y[1]]
>> elif 25<t<45:
>> return [y[1],-8*y[0]]
>> else:
>> return [y[1],-8*y[0]-0.1*y[1]]
>> t=np.linspace(0,35,1000)
>> # start from y=0.01, y’=0
>> y1=odeint(f,[0.01,0],t)
>> plt.plot(t,y1[:,0])
>> plt.title("Evolution temporelle")
>> plt.xlabel("Temps t (s)")
>> plt.ylabel("Intensite i (A)")
>> plt.plot(0,0.01,"ro")
>> #plt.savefig("RLC-demarrage.eps")
>> plt.show()
>> ----------ijulia----------------
>> @pyimport scipy.integrate as odeint
>> function f(y,t)
>> if t<25
>> return [y[1],-8*y[0]+0.1*y[1]]
>> elseif 25<t<45
>> return [y[1],-8*y[0]]
>> else
>> return [y[1],-8*y[0]-0.1*y[1]]
>> end
>> end;
>> t=linspace(0,35,1000)
>> y=linspace(0.01,0)
>> y1=integrate.odeint(f,y,t)
>>
>