How to integrate a piecewise function?

Showing 1-2 of 2 messages
How to integrate a piecewise function? Peng Yu 10/9/12 1:49 PM
Hi,

The function g down below is not integrated correctly. I can not find
the corresponding page in the manual for solving this problem. Does
anybody know how to define a piecewise function and integrate it?
Thanks!

~/linux/test/sage/man/sage/symbolic/integration/integral/definite_integral$
cat ./main1.sage
#!/usr/bin/env sage

from sage.symbolic.integration.integral import definite_integral

def f(x):
  return (1-x^2)^3

def g(x):
  if x>=0:
    return (1-x^2)^3
  else:
    return 0

print definite_integral(f(x),x,-1,1)
print definite_integral(g(x),x,-1,1)

~/linux/test/sage/man/sage/symbolic/integration/integral/definite_integral$
./main1.sage
32/35
0

--
Regards,
Peng
Re: [sage-support] How to integrate a piecewise function? David Joyner 10/9/12 1:57 PM
On Tue, Oct 9, 2012 at 4:49 PM, Peng Yu <peng...@gmail.com> wrote:
> Hi,
>
> The function g down below is not integrated correctly. I can not find
> the corresponding page in the manual for solving this problem. Does
> anybody know how to define a piecewise function and integrate it?

From the documentation:

sage: f1(x) = -1
sage: f2(x) = 2
sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])

sage: f.integral()
Piecewise defined function with 2 parts, [[(0, 1/2*pi), x |--> -x],
[(1/2*pi, pi), x |--> -3/2*pi + 2*x]]
sage: f.integral(definite=True)
1/2*pi



> Thanks!
>
> ~/linux/test/sage/man/sage/symbolic/integration/integral/definite_integral$
> cat ./main1.sage
> #!/usr/bin/env sage
>
> from sage.symbolic.integration.integral import definite_integral
>
> def f(x):
>   return (1-x^2)^3
>
> def g(x):
>   if x>=0:
>     return (1-x^2)^3
>   else:
>     return 0
>
> print definite_integral(f(x),x,-1,1)
> print definite_integral(g(x),x,-1,1)
>
> ~/linux/test/sage/man/sage/symbolic/integration/integral/definite_integral$
> ./main1.sage
> 32/35
> 0
>
> --
> Regards,
> Peng
>
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To post to this group, send email to sage-s...@googlegroups.com.
> To unsubscribe from this group, send email to sage-support...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-support?hl=en.
>
>