On Fri, Nov 30, 2012 at 10:12 PM, Jason Grout
<[email protected]> wrote:
> A friend just asked me about piecewise functions in Sage (how to construct,
> plot, differentiate, integrate, etc., them). I came up with two answers for
> plotting:
>
> * use python functions:
>
> def f(t):
> return t^2*(0<=t<=1) + (t-1)*(1<t<=2)+(sin(t))*(2<t<=3)
> plot(f, (0,5), exclude=(0,1,2,3))
>
>
> * Use unit_step or heaviside:
>
> def interval(x,a,b):
> return unit_step(x-a)*unit_step(b-x)
>
> f(t)=(t^2-2*t+1)*interval(t,1,2)+(t-1)*interval(t,2,3)+sin(t^2)*interval(t,3,4)
> show(plot(f,(t,0,5),exclude=range(5),fill=True))
> show(integrate(f,(t,0,5)))
> show(diff(f,t))
>
> Is there something better? I tried to get piecewise to work, but I couldn't
> plot, integrate, etc., the function.
>
> f=piecewise([((1,2), x^2), ((2,3), sin(x))])
> plot(f, (x,0,3)) # error, but plot(f) works...
> integrate(f, (x,1,3)) # error, but integrate(f) works
> diff(f,x) # error, but diff(f) gives a warning and an output
sage: x = var("x")
sage: f = piecewise([[(1,2), x^2], [(2,3), 1-x]])
sage: f.plot()
works for me. Piecewise works best for piecewise polynomials.
(And, as has been said many times, it needs to be re-written.)
>
> Thanks,
>
> Jason
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> Visit this group at http://groups.google.com/group/sage-support?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"sage-support" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
Visit this group at http://groups.google.com/group/sage-support?hl=en.