How to integrate a piecewise function?

78 views
Skip to first unread message

Peng Yu

unread,
Oct 9, 2012, 4:49:08 PM10/9/12
to sage-support
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

David Joyner

unread,
Oct 9, 2012, 4:57:52 PM10/9/12
to sage-s...@googlegroups.com
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.
>
>
Reply all
Reply to author
Forward
0 new messages