sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.37k stars 462 forks source link

desolve failed to solve an ODE whose solution implies integration limits #11653

Open e4c2077a-2707-419a-892c-dfa2c4822290 opened 13 years ago

e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago

When trying to solve a simple ODE whose rhs contains a function, Sage fails to interpret the Maxima output if this contains integration limits.

A minimal example

sage: x=var('x') # independent variable
sage: v=function('v', x) # dependent variable

if we now define a custom square pulse function

def pulse(tonset, tdur, amp):
    """
    returns a square pulse as a function of x, f(x) 
    the pulse is defined as follows:  
    t onset -- start of pulse  
    tdur   -- duration of pulse  
    amp    -- amplitude of pulse 
    """

    f(x)= amp*(sign(x-tonset)/2-sign(x-tonset-tdur)/2)
    return f

now we create a pulse function

sage: mypulse = pulse(tonset=5, tdur=5, amp=2)

and define differential equation

sage: dvdx = diff(v, x)-x -mypulse == 0 # mypulse(x) is function

To get the evolution of v we can use desolve

myvolt = desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0])

The error message is:

TypeError: 
unable to make sense of Maxima expression 
'v(x)=-(2*(at(integrate(signum(x-5)-signum(x-13),x),[x=0,v(x)=0]))-2*int\egrate(signum(x-5)-signum(x-13),x)-x^2)/2' 
in Sage

desolve_laplace leads to similar error:

sage: desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0])
TypeError: 
unable to make sense of Maxima expression 
'ilt(((laplace(signum(x-5),x,?g2733)-laplace(signum(x-13),x,?g2733)+v(0)\ )*?g2733^2+1)/?g2733^3,?g2733,x)' 
in Sage

According to Nils Bruin, the problem is that Maxima 'at' function. As described in Ticket #385, this can be a problem with the implementation of 'at' for SR.

Expressions that work

And adding the sign function to the differential function does not affect the solution.

sage: dvdx = diff(v, x)-v -sign(x) == 0 
sage: desolve(de=dvdx, ivar=x, dvar=v) 
sage: (c + integrate(e^(-x)*sgn(x), x))*e^x

However, adding initial conditions produces an output that Sage is not able to evaluate

sage: desolve(de = dvdx, ivar=x, dvar=v, ics=[0,0])

The output is:

TypeError: 
unable to make sense of Maxima expression 
'v(x)=e^x*integrate(e^-x*signum(x),x)-e^x*(at(integrate(e^-x*signum(x),x),[x=0,v(x)=0]))'
 in Sage

I guess Sage is not able to interpret the integrations limits prompted by Maxima (e.g [t=0,v(t)=0]).

Detailed information (and a more realistic example) can be found here: http://groups.google.com/group/sage-support/browse_thread/thread/8cc67d39510faca2

CC: @kcrisman @nbruin

Component: calculus

Keywords: maxima, at, desolve

Issue created by migration from https://trac.sagemath.org/ticket/11653

e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago

Description changed:

--- 
+++ 
@@ -8,7 +8,10 @@

 def pulse(tonset, tdur, amp):

-  """ returns a square pulse as a function of x, f(x) the pulse is defined as follows:  tonset -- start of pulse  tdur   -- duration of pulse  amp    -- amplitude of pulse """
+  """ returns a square pulse as a function of x, f(x) the pulse is defined as follows:  t
+  onset -- start of pulse 
+  tdur   -- duration of pulse 
+  amp    -- amplitude of pulse """

   f(x)= amp*(sign(x-tonset)/2-sign(x-tonset-tdur)/2)

@@ -30,7 +33,7 @@

 *TypeError: unable to make sense of Maxima expression 'v(x)=-(2*(at(integrate(signum(x-5)-signum(x-13),x),[x=0,v(x)=0]))-2*int\ egrate(signum(x-5)-signum(x-13),x)-x^2)/2' in Sage^*

-desolve_laplace leads to similar error: 
+desolve_laplace leads to similar error:

 sage: desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0])
e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago

Description changed:

--- 
+++ 
@@ -8,10 +8,7 @@

 def pulse(tonset, tdur, amp):

-  """ returns a square pulse as a function of x, f(x) the pulse is defined as follows:  t
-  onset -- start of pulse 
-  tdur   -- duration of pulse 
-  amp    -- amplitude of pulse """
+  """ returns a square pulse as a function of x, f(x) the pulse is defined as follows:  t onset -- start of pulse  tdur   -- duration of pulse  amp    -- amplitude of pulse """

   f(x)= amp*(sign(x-tonset)/2-sign(x-tonset-tdur)/2)

@@ -41,4 +38,12 @@

 According to Nils Bruin, the problem is that Maxima 'at' function. As described in Ticket #385, this can be a problem with the implementation of 'at' for SR.

+Strange enough, when using other functions, the solver works nicely
+
+sage: dvdx = diff(v, x)-x -sin(x) == 0 
+
+sage: desolve(de=dvdx, ivar=x, dvar=v, ics=[http://trac.sagemath.org/sage_trac/log/?revs=0 "[0,0]"]) 
+
+now Sage returns 1/2*x!^2 - cos(x) + 1
+
 Detailed information (and a real example) can be found here: http://groups.google.com/group/sage-support/browse_thread/thread/8cc67d39510faca2
e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago

Changed keywords from none to maxima, at, desolve

kcrisman commented 13 years ago

Description changed:

--- 
+++ 
@@ -1,49 +1,54 @@
 When trying to solve a simple ODE whose rhs contains a function, Sage fails to interpret the Maxima output.

-Here a minimal example is  #========================================================================= sage: x=var('x') # independent variable
+Here a minimal example is  

+```
+#===================== 
+sage: x=var('x') # independent variable
 sage: v=function('v', x) # dependent variable

-\# we define a custom square pulse function
-
+# we define a custom square pulse function
 def pulse(tonset, tdur, amp):
-
-  """ returns a square pulse as a function of x, f(x) the pulse is defined as follows:  t onset -- start of pulse  tdur   -- duration of pulse  amp    -- amplitude of pulse """
-
+"""
+returns a square pulse as a function of x, f(x) 
+the pulse is defined as follows:  
+t onset -- start of pulse  
+tdur   -- duration of pulse  
+amp    -- amplitude of pulse 
+"""
   f(x)= amp*(sign(x-tonset)/2-sign(x-tonset-tdur)/2)
-
   return f

-\# create my pulse function
-
+# create my pulse function
 sage: mypulse = pulse(tonset=5, tdur=5, amp=2)

-\# define differential equation s
-
+# define differential equation s
 sage: dvdx = diff(v, x)-x -mypulse == 0 # mypulse(x) is function

-\# get the evolution of v
-
+# get the evolution of v
 myvolt = desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0])

-#========================================================================= The error message is:
+#======= 
+The error message is:

-*TypeError: unable to make sense of Maxima expression 'v(x)=-(2*(at(integrate(signum(x-5)-signum(x-13),x),[x=0,v(x)=0]))-2*int\ egrate(signum(x-5)-signum(x-13),x)-x^2)/2' in Sage^*
-
+''TypeError: unable to make sense of Maxima expression 'v(x)=-(2*(at(integrate(signum(x-5)-signum(x-13),x),[x=0,v(x)=0]))-2*int\ egrate(signum(x-5)-signum(x-13),x)-x^2)/2' in Sage^''
+```
 desolve_laplace leads to similar error:

+```
 sage: desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0])

-*TypeError: unable to make sense of Maxima expression 'ilt(((laplace(signum(x-5),x,?g2733)-laplace(signum(x-13),x,?g2733)+v(0)\ )*?g2733<sup>2+1)/?g2733</sup>3,?g2733,x)' in Sage*
-
+''TypeError: unable to make sense of Maxima expression 'ilt(((laplace(signum(x-5),x,?g2733)-laplace(signum(x-13),x,?g2733)+v(0)\ )*?g2733^2+1)/?g2733^3,?g2733,x)' in Sage''
+```
 According to Nils Bruin, the problem is that Maxima 'at' function. As described in Ticket #385, this can be a problem with the implementation of 'at' for SR.

 Strange enough, when using other functions, the solver works nicely

+```
 sage: dvdx = diff(v, x)-x -sin(x) == 0 

 sage: desolve(de=dvdx, ivar=x, dvar=v, ics=[http://trac.sagemath.org/sage_trac/log/?revs=0 "[0,0]"]) 
-
+```
 now Sage returns 1/2*x!^2 - cos(x) + 1

 Detailed information (and a real example) can be found here: http://groups.google.com/group/sage-support/browse_thread/thread/8cc67d39510faca2
kcrisman commented 13 years ago
comment:5

Thanks, Jose! Just a few pointers:

kcrisman commented 13 years ago

Changed author from JGuzman to none

kcrisman commented 13 years ago
comment:6

As to the ticket, see the sage-support thread in question. The essential problem is that in laplace and taylor we take the answer from Maxima and send it to SR, which does parse the at correctly via the Maxima string thingie in calculus/calculus.py, but in the desolve_* functions we just coerce to .sage(), which does not. Probably changing this would fix it.

e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago

Description changed:

--- 
+++ 
@@ -1,54 +1,66 @@
 When trying to solve a simple ODE whose rhs contains a function, Sage fails to interpret the Maxima output.

-Here a minimal example is  
+Here a minimal example is

-#===================== sage: x=var('x') # independent variable sage: v=function('v', x) # dependent variable +```

-# we define a custom square pulse function +# we define a custom square pulse function + +``` def pulse(tonset, tdur, amp): """ -returns a square pulse as a function of x, f(x) -the pulse is defined as follows:
-t onset -- start of pulse
-tdur -- duration of pulse
-amp -- amplitude of pulse -"""

-# create my pulse function +now we create a pulse function + + sage: mypulse = pulse(tonset=5, tdur=5, amp=2) + +and define differential equation

-# define differential equation s + sage: dvdx = diff(v, x)-x -mypulse == 0 # mypulse(x) is function +

-# get the evolution of v +To get the evolution of v we can use desolve + + myvolt = desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0]) +

-#======= + The error message is:

-''TypeError: unable to make sense of Maxima expression 'v(x)=-(2(at(integrate(signum(x-5)-signum(x-13),x),[x=0,v(x)=0]))-2int\ egrate(signum(x-5)-signum(x-13),x)-x^2)/2' in Sage^'' -``` +TypeError: unable to make sense of Maxima expression 'v(x)=-(2(at(integrate(signum(x-5)-signum(x-13),x),[x=0,v(x)=0]))-2int\ egrate(signum(x-5)-signum(x-13),x)-x^2)/2' in Sage^ + desolve_laplace leads to similar error:

 sage: desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0])
+```

-''TypeError: unable to make sense of Maxima expression 'ilt(((laplace(signum(x-5),x,?g2733)-laplace(signum(x-13),x,?g2733)+v(0)\ )*?g2733^2+1)/?g2733^3,?g2733,x)' in Sage''
-```
+*TypeError: unable to make sense of Maxima expression 'ilt(((laplace(signum(x-5),x,?g2733)-laplace(signum(x-13),x,?g2733)+v(0)\ )*?g2733<sup>2+1)/?g2733</sup>3,?g2733,x)' in Sage*
+
 According to Nils Bruin, the problem is that Maxima 'at' function. As described in Ticket #385, this can be a problem with the implementation of 'at' for SR.

 Strange enough, when using other functions, the solver works nicely

sage: dvdx = diff(v, x)-x -sin(x) == 0 +sage: desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0]"]) +```

-sage: desolve(de=dvdx, ivar=x, dvar=v, ics=[http://trac.sagemath.org/sage_trac/log/?revs=0 "[0,0]"]) -``` now Sage returns 1/2*x!^2 - cos(x) + 1

Detailed information (and a real example) can be found here: http://groups.google.com/group/sage-support/browse_thread/thread/8cc67d39510faca2

e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago
comment:7

I re-formatted the text according to kcrisman (thanks a lot for the suggestion!). I will have a look to the code soon. I am looking forward to implement it. If I understood correctly, the only thing to do is to make desolve_* take the answer from Maxima to SR.

e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago
comment:8

I changed the summary to delimit the error more carefully. Additional, a more detailed explanation is given of cases in which Sage is able to interpret Maxima output. I guess, the problem is that Sage is not able to interpret the integration limits prompted by the maxima expression.

e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago

Description changed:

--- 
+++ 
@@ -1,48 +1,46 @@
-When trying to solve a simple ODE whose rhs contains a function, Sage fails to interpret the Maxima output.
+When trying to solve a simple ODE whose rhs contains a function, Sage fails to interpret the Maxima output if this contains integration limits.

-Here a minimal example is
+## A minimal example

sage: x=var('x') # independent variable sage: v=function('v', x) # dependent variable

+if we now define a custom square pulse function

-\# we define a custom square pulse function
-
-```
+```python
 def pulse(tonset, tdur, amp):
-"""
+    """
     returns a square pulse as a function of x, f(x) 
     the pulse is defined as follows:  
     t onset -- start of pulse  
     tdur   -- duration of pulse  
     amp    -- amplitude of pulse 
     """
+
     f(x)= amp*(sign(x-tonset)/2-sign(x-tonset-tdur)/2)
     return f

- now we create a pulse function

 sage: mypulse = pulse(tonset=5, tdur=5, amp=2)

-and define differential equation +and define differential equation

 sage: dvdx = diff(v, x)-x -mypulse == 0 # mypulse(x) is function

- To get the evolution of v we can use desolve

 myvolt = desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0])

-

The error message is:

-TypeError: unable to make sense of Maxima expression 'v(x)=-(2(at(integrate(signum(x-5)-signum(x-13),x),[x=0,v(x)=0]))-2int\ egrate(signum(x-5)-signum(x-13),x)-x^2)/2' in Sage^ +python +TypeError: unable to make sense of Maxima expression 'v(x)=-(2*(at(integrate(signum(x-5)-signum(x-13),x),[x=0,v(x)=0]))-2*int\egrate(signum(x-5)-signum(x-13),x)-x^2)/2' in Sage^ +

desolve_laplace leads to similar error:

@@ -50,17 +48,34 @@ sage: desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0])


-*TypeError: unable to make sense of Maxima expression 'ilt(((laplace(signum(x-5),x,?g2733)-laplace(signum(x-13),x,?g2733)+v(0)\ )*?g2733<sup>2+1)/?g2733</sup>3,?g2733,x)' in Sage*
+```python
+TypeError: unable to make sense of Maxima expression 'ilt(((laplace(signum(x-5),x,?g2733)-laplace(signum(x-13),x,?g2733)+v(0)\ )*?g2733^2+1)/?g2733^3,?g2733,x)' in Sage
+```

 According to Nils Bruin, the problem is that Maxima 'at' function. As described in Ticket #385, this can be a problem with the implementation of 'at' for SR.

-Strange enough, when using other functions, the solver works nicely
+## Expressions that work
+And adding the **sign** function to the differential function does not affect the solution.

-sage: dvdx = diff(v, x)-x -sin(x) == 0 +sage: dvdx = diff(v, x)-v -sign(x) == 0 sage: desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0]"]) +sage: (c + integrate(e^(-x)sgn(x), x))e^x


-now Sage returns 1/2*x!^2 - cos(x) + 1
+However, adding initial conditions produces an output that Sage is not able to evaluate

-Detailed information (and a real example) can be found here: http://groups.google.com/group/sage-support/browse_thread/thread/8cc67d39510faca2
+```
+sage: desolve(de = dvdx, ivar=x, dvar=v, ics=[0,0])
+```
+
+The output is:
+
+```python
+TypeError: unable to make sense of Maxima expression 'v(x)=e^x*integrate(e^-x*signum(x),x)-e^x*(at(integrate(e^-x*signum(x),x),[x=0,v(x)=0]))' in Sage
+
+```
+
+I guess Sage is not able to interpret the integrations limits prompted by Maxima (e.g [t=0,v(t)=0]). 
+
+Detailed information (and a more realistic example) can be found here: http://groups.google.com/group/sage-support/browse_thread/thread/8cc67d39510faca2
e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago

Description changed:

--- 
+++ 
@@ -39,7 +39,10 @@
 The error message is:

 ```python
-TypeError: unable to make sense of Maxima expression 'v(x)=-(2*(at(integrate(signum(x-5)-signum(x-13),x),[x=0,v(x)=0]))-2*int\egrate(signum(x-5)-signum(x-13),x)-x^2)/2' in Sage^
+TypeError: 
+unable to make sense of Maxima expression 
+'v(x)=-(2*(at(integrate(signum(x-5)-signum(x-13),x),[x=0,v(x)=0]))-2*int\egrate(signum(x-5)-signum(x-13),x)-x^2)/2' 
+in Sage

desolve_laplace leads to similar error: @@ -49,7 +52,10 @@


 ```python
-TypeError: unable to make sense of Maxima expression 'ilt(((laplace(signum(x-5),x,?g2733)-laplace(signum(x-13),x,?g2733)+v(0)\ )*?g2733^2+1)/?g2733^3,?g2733,x)' in Sage
+TypeError: 
+unable to make sense of Maxima expression 
+'ilt(((laplace(signum(x-5),x,?g2733)-laplace(signum(x-13),x,?g2733)+v(0)\ )*?g2733^2+1)/?g2733^3,?g2733,x)' 
+in Sage

According to Nils Bruin, the problem is that Maxima 'at' function. As described in Ticket #385, this can be a problem with the implementation of 'at' for SR. @@ -72,7 +78,10 @@ The output is:

-TypeError: unable to make sense of Maxima expression 'v(x)=e^x*integrate(e^-x*signum(x),x)-e^x*(at(integrate(e^-x*signum(x),x),[x=0,v(x)=0]))' in Sage
+TypeError: 
+unable to make sense of Maxima expression 
+'v(x)=e^x*integrate(e^-x*signum(x),x)-e^x*(at(integrate(e^-x*signum(x),x),[x=0,v(x)=0]))'
+ in Sage
e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago
comment:9

Change owner to burcin (I do not know why it changed last time !), and change the typesetting of TypeError

e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago

Description changed:

--- 
+++ 
@@ -65,7 +65,7 @@

sage: dvdx = diff(v, x)-v -sign(x) == 0 -sage: desolve(de=dvdx, ivar=x, dvar=v, ics=[0,0]"]) +sage: desolve(de=dvdx, ivar=x, dvar=v"]) sage: (c + integrate(e^(-x)sgn(x), x))e^x

e4c2077a-2707-419a-892c-dfa2c4822290 commented 13 years ago

Description changed:

--- 
+++ 
@@ -65,7 +65,7 @@

sage: dvdx = diff(v, x)-v -sign(x) == 0 -sage: desolve(de=dvdx, ivar=x, dvar=v"]) +sage: desolve(de=dvdx, ivar=x, dvar=v) sage: (c + integrate(e^(-x)sgn(x), x))e^x

kcrisman commented 13 years ago
comment:12

There are several issues here, actually, so it may take a bit to solve this. We are apparently translating several things wrongly by not going through SR, but then there is also the 'general' variable ?g2733 which I remember being still undealt with... probably won't be fixed immediately :( just because of time constraints.