i've find anothe way to solve my problems with rect-function: rect=lambda x: Piecewise([ [(-infinity,-1),(lambda x:0)], [(- 1, 1),(lambda x:1)], [( 1, infinity),(lambda x:0)] ])(x);
now: plot(rect,-4,4) works, and: f=lambda x: rect(x)*x^2; plot(f,-4,4) works too :-) but now if want to use my function again, i can not: g(x)=1+f(x) so i must use only: g=lambda x:1+f(x) (and show(plot(g,-4,4),ymin=0) or numerical_integral(g,-4,4) works fine). definition like "g(x)=1+f(x)" is more comfort for me ( On Sep 15, 8:25 pm, "David Joyner" <[EMAIL PROTECTED]> wrote: > On Mon, Sep 15, 2008 at 11:10 AM, Sand Wraith <[EMAIL PROTECTED]> wrote: > > > On Sep 13, 2:35 am, Jason Merrill <[EMAIL PROTECTED]> wrote: > >> On Sep 12, 4:48 pm, Sand Wraith <[EMAIL PROTECTED]> wrote: > > >> > Hi all! Help please again :-) > > >> > here is worksheet describes my problem: > > >> >http://75.75.6.176/home/pub/8/ > > >> > so, at the last stem i have wrong result: 0 instead of 2/3. > > >> > what i am doing wrong? > > >> It looks like there are a few problems here, but the main thing is > >> that when you call myrect(x), it just returns 0. > > >> def rect(tau=0,t=0): > >> if (t==tau) or (t==-tau): > >> return 0.5 > >> elif (t>-tau) and (t<tau): > >> return 1 > >> else: > >> return 0; > > >> def myrect(x): > >> return rect(1,x); > > >> sage: myrect(x) > >> 0 > > >> The reason is that when you compare a symbolic variable, x, to a > >> number, 1, and force sage to come up with a True or False answer, as > >> if and elif do, the answer is basically always False. > > >> sage: bool(x == 1) > >> False > >> sage: bool(x < 1) > >> False > >> sage: bool(x > 1) > >> False > >> sage: bool(x < -1) > >> False > > >> etc. > > >> Because of this, when you call myrect(x), things fall down to the last > >> branch of your definition. When an expression appears as an argument > >> to a function, it is evaluated *before* the function is called. For > >> instance, look at > > >> sage: plot(myrect(x),(x,-3,3)) # The line segment y == 0 > > >> In this case, myrect(x) evaluates to 0 *before* plot has a chance to > >> pass in any values, and the same thing is happening to integral. > > >> I'd like to tell you that you can do what you want using piecewise, or > >> something like that, but actually I don't see any way at all to make > >> integral, which needs something that can act like a SymbolicExpression > >> as its first argument, do what you want. Maybe someone else will > >> know. > > >> Regards, > > >> JM > > > I have redefine rect function: > > rect=Piecewise([ > > [(-10,-1),(lambda x:0)], > > [(- 1, 1),(lambda x:1)], > > [( 1, 10),(lambda x:0)] > > ]); > > > and i have another two questions: > > > 1) rect.plot() - is it the only way of plotting? i'd like to use > > Yes, this is the only way to plot rect at the moment. > > > plot(rect,-4,4), but it leads to error: > > >>verbose 0 (3729: plot.py, __call__) there were 4 extra arguments > >>(besides <function <lambda> at 0xa9382cc>) > >>Traceback (click to the left for traceback) > >>... > >>UnboundLocalError: local variable 'G' referenced before assignment > > > 2)How can i define another function like rect*f(x) ? and plot it? > > For some reason, * is not working. You can just redefine your > function: for example, > > rect2 = Piecewise([[(-10,-1),(lambda x:0)], [(- 1, 1),(lambda > x:sin(x))], [( 1, 10),(lambda x:0)]]) > > --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---
