numerical solution of differential equations

40 views
Skip to first unread message

jukka....@uef.fi

unread,
Nov 17, 2025, 4:45:59 AM (7 days ago) Nov 17
to sage-support
According to the manual the following should work:

t,y=var('t,y');
g= lambda t,y:-y/(2*y^3 - 1);
ra = ode_solver();
ra.function=g;
ra.ode_solve(y_0=1,t_span=[0,1],num_points=100)

However, I get the error message

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_317/557462051.py in <module> ----> 1 ra.ode_solve(y_0=Integer(1),t_span=[Integer(0),Integer(1)],num_points=Integer(100)) /usr/lib/python3/dist-packages/sage/calculus/ode.pyx in sage.calculus.ode.ode_solver.ode_solve (build/cythonized/sage/calculus/ode.c:5124)() 412 self.y_0 = y_0 413 --> 414 dim = len(self.y_0) 415 type = isinstance(self.function,ode_system) 416 if type == 0: TypeError: object of type 'sage.rings.integer.Integer' has no len()

Also 
def f(t,y): return -y/(2*y^3 - 1);
ra.function=f;

gives the same error.

??

Dima Pasechnik

unread,
Nov 17, 2025, 11:21:42 AM (7 days ago) Nov 17
to sage-s...@googlegroups.com
On Mon, Nov 17, 2025 at 3:45 AM jukka....@uef.fi <jukka....@uef.fi> wrote:
According to the manual the following should work:

t,y=var('t,y');
g= lambda t,y:-y/(2*y^3 - 1);
ra = ode_solver();
ra.function=g;
ra.ode_solve(y_0=1,t_span=[0,1],num_points=100)


Please write  the ODE you want to solve, in the usual maths notation -
it's not clear what it is.

 

However, I get the error message

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_317/557462051.py in <module> ----> 1 ra.ode_solve(y_0=Integer(1),t_span=[Integer(0),Integer(1)],num_points=Integer(100)) /usr/lib/python3/dist-packages/sage/calculus/ode.pyx in sage.calculus.ode.ode_solver.ode_solve (build/cythonized/sage/calculus/ode.c:5124)() 412 self.y_0 = y_0 413 --> 414 dim = len(self.y_0) 415 type = isinstance(self.function,ode_system) 416 if type == 0: TypeError: object of type 'sage.rings.integer.Integer' has no len()

Also 
def f(t,y): return -y/(2*y^3 - 1);
ra.function=f;

gives the same error.

??

--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sage-support/d14332f8-44b6-4f93-9b42-0972926d2a16n%40googlegroups.com.

jukka....@uef.fi

unread,
Nov 17, 2025, 12:49:44 PM (7 days ago) Nov 17
to sage-support
y'=-y/(2*y^3-1)

Dima Pasechnik

unread,
Nov 17, 2025, 12:52:18 PM (7 days ago) Nov 17
to sage-support
even for the 1st order ODE, y must be a vector. 
(and, apparently, for y as well)

The following example works:

sage: t,y=var('t,y');
....: g= lambda t,y:[-y[0]]; # ODE y'=-y
....: ra = ode_solver();
....: ra.function=g;
....: ra.ode_solve(y_0=[1],t_span=[0,1],num_points=100)

Now, if I change g to

g= lambda t,y:[-y[0]/(2*y[0]^3 - 1)];

the example runs, but seemingly never stops. Perhaps you hit a singularity somewhere that results in
such a non-convergence.

jukka....@uef.fi

unread,
Nov 18, 2025, 4:10:02 AM (6 days ago) Nov 18
to sage-support
yes in fact the initial value should be bigger, then there are no singularities, but even then I get

-------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) /usr/lib/python3/dist-packages/sage/calculus/ode.pyx in sage.calculus.ode.ode_solver.ode_solve (build/cythonized/sage/calculus/ode.c:6834)() 554 if (status != GSL_SUCCESS): --> 555 raise RuntimeError 556 except RuntimeError: RuntimeError: During handling of the above exception, another exception occurred: ValueError Traceback (most recent call last) /tmp/ipykernel_125/2547832258.py in <module> ----> 1 ra.ode_solve(y_0=[RealNumber('3.1')],t_span=[Integer(0),Integer(1)],num_points=Integer(1000)) /usr/lib/python3/dist-packages/sage/calculus/ode.pyx in sage.calculus.ode.ode_solver.ode_solve (build/cythonized/sage/calculus/ode.c:6934)() 560 sig_free(y) 561 sig_free(scale_abs_array) --> 562 raise ValueError("error solving") 563 564 for j from 0<=j<dim: ValueError: error solving

But now I realize that my version is quite old: 'SageMath version 9.5, Release Date: 2022-01-30' Perhaps that's why it doesn't work? In fact the following works: t,y=var('t,y')
numsol=desolve_rk4( -y/(2*y^3 - 1), dvar=y, ivar=t, ics=[0,2.1],end_points=1, step=0.001)
kuva=line(numsol,axes_labels=[t,y])
kuva.show()

Dima Pasechnik

unread,
Nov 18, 2025, 3:17:47 PM (6 days ago) Nov 18
to sage-s...@googlegroups.com
On Tue, Nov 18, 2025 at 3:09 AM jukka....@uef.fi <jukka....@uef.fi> wrote:
yes in fact the initial value should be bigger, then there are no singularities, but even then I get

-------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) /usr/lib/python3/dist-packages/sage/calculus/ode.pyx in sage.calculus.ode.ode_solver.ode_solve (build/cythonized/sage/calculus/ode.c:6834)() 554 if (status != GSL_SUCCESS): --> 555 raise RuntimeError 556 except RuntimeError: RuntimeError: During handling of the above exception, another exception occurred: ValueError Traceback (most recent call last) /tmp/ipykernel_125/2547832258.py in <module> ----> 1 ra.ode_solve(y_0=[RealNumber('3.1')],t_span=[Integer(0),Integer(1)],num_points=Integer(1000)) /usr/lib/python3/dist-packages/sage/calculus/ode.pyx in sage.calculus.ode.ode_solver.ode_solve (build/cythonized/sage/calculus/ode.c:6934)() 560 sig_free(y) 561 sig_free(scale_abs_array) --> 562 raise ValueError("error solving") 563 564 for j from 0<=j<dim: ValueError: error solving

But now I realize that my version is quite old: 'SageMath version 9.5, Release Date: 2022-01-30' Perhaps that's why it doesn't work?

in the current beta, SageMath 10.8.beta9, the following works:

sage: t,y=var('t,y');
....: g= lambda t,y:[-y[0]/(2*y[0]**3 - 1)];

....: ra = ode_solver();
....: ra.function=g;
....: ra.ode_solve(y_0=[1.5],t_span=[0,1], num_points=1000)

(the values of  y_0 between [1.0] and [1.4] lead to a hang)
This is a gsl ODE  backend.
 
In fact the following works: t,y=var('t,y')
numsol=desolve_rk4( -y/(2*y^3 - 1), dvar=y, ivar=t, ics=[0,2.1],end_points=1, step=0.001)
kuva=line(numsol,axes_labels=[t,y])
kuva.show()
This is a different ODE solver backend, a Maxima one

 HTH
Dima

Reply all
Reply to author
Forward
0 new messages