Plotting functions

We can plot a function, or add two or more plots using plot and show commands

{{{id=5| plot(x^2,(x,-1,1)) /// }}} {{{id=2| P1=plot(x^2,(x,-3,3)) P2=plot(tan(x),(x,-6,6),color='red') show(P1+P2,ymin=-6,ymax=6) /// }}}

On the previous picture we can see that Sage draws vertical lines at the points of discontinuity. This can be avoided by using detect_poles directive.

{{{id=1| P2=plot(tan(x),(x,-6,6),color='green',detect_poles=True) show(P1+P2,ymin=-pi,ymax=pi) /// }}}

Polynomials

Sage is capable to simplify expressions. Among others, we can simplify polynomials (factor, expand), divide polynomials and look for zeros of polynomials. The underscore in the following computations reffers allways to previous result.

{{{id=9| P(x)=(x-2)^3*(x^2-4*x+3)*2 P(x) /// \newcommand{\Bold}[1]{\mathbf{#1}}2 \, {(x - 2)}^{3} {(x^{2} - 4 \, x + 3)} }}}

Here we multiply parentheses and factor again.

{{{id=15| expand(_) /// \newcommand{\Bold}[1]{\mathbf{#1}}2 \, x^{5} - 20 \, x^{4} + 78 \, x^{3} - 148 \, x^{2} + 136 \, x - 48 }}} {{{id=7| factor(_) /// \newcommand{\Bold}[1]{\mathbf{#1}}2 \, {(x - 3)} {(x - 2)}^{3} {(x - 1)} }}}

We can  substitute values for $x$.

{{{id=19| [P(0),P(1),P(2),P(sqrt(2))] /// \newcommand{\Bold}[1]{\mathbf{#1}}\left[-48, 0, 0, -2 \, {(\sqrt{2} - 2)}^{3} {(4 \, \sqrt{2} - 5)}\right] }}}

The last value can be computed numerically.

{{{id=21| P(sqrt(2)).n() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle 0.264068711928515 }}}

Here we divide and simplify the quotient. If we divide by the factor $(x-c)$, where $c$ is a zero of the polynomial, the remainder is zero.

{{{id=24| (P(x)/(x-1)) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle 2 \, \frac{{\left(x - 2\right)}^{3} {\left(x^{2} - 4 \, x + 3\right)}}{{\left(x - 1\right)}} }}} {{{id=23| (P(x)/(x-1)).simplify_full() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle 2 \, x^{4} - 18 \, x^{3} + 60 \, x^{2} - 88 \, x + 48 }}} {{{id=35| P(x)._maxima_().divide(x-1).sage() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left[2 \, x^{4} - 18 \, x^{3} + 60 \, x^{2} - 88 \, x + 48, 0\right] }}} {{{id=11| P(x)._maxima_().divide(x-2).sage() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left[2 \, x^{4} - 16 \, x^{3} + 46 \, x^{2} - 56 \, x + 24, 0\right] }}}

If $c$ is not a zero of the polynomial, then we get nonzero remainder. This remainder is equal to the value of the function $P(x)$ at $c$. We test this property for $c=5$.

{{{id=36| P(x)._maxima_().divide(x-5).sage() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left[2 \, x^{4} - 10 \, x^{3} + 28 \, x^{2} - 8 \, x + 96, 432\right] }}} {{{id=25| P(5) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle 432 }}}

Another method how to divide polynomials in Sage is to define the ring of polynomials, convert symbolic expression into polynomial and use quo_rem method for dividing polynomials.

Here $W$ is ring of polynomials with variable $x$ and $W(P(x))$ means "treat $P(x)$ as polynomial".

{{{id=29| W.=QQ[] W(P(x)).quo_rem(x-5) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \left(2 x^{4} - 10 x^{3} + 28 x^{2} - 8 x + 96, 432\right) }}}

Multiplicity

We define polynomial

{{{id=44| P(x)=x^5 + 9*x^4 + 36*x^3 + 80*x^2 + 96*x + 48 P(x) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle x^{5} + 9 \, x^{4} + 36 \, x^{3} + 80 \, x^{2} + 96 \, x + 48 }}}

$x=-2$ is a zero of this polynomial.

{{{id=43| P(-2) /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle 0 }}}

Thus by Theorem of Bezout, we can divide the polynomial $P(x)$ by the polynomial $x+2$.

{{{id=37| (P(x)/(x+2)).simplify_full() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle x^{4} + 7 \, x^{3} + 22 \, x^{2} + 36 \, x + 24 }}}

Let us try to perform the division once more.

{{{id=34| (_/(x+2)).simplify_full() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle x^{3} + 5 \, x^{2} + 12 \, x + 12 }}}

.. and once more ....

{{{id=38| (_/(x+2)).simplify_full() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle x^{2} + 3 \, x + 6 }}}

... and once more ...

{{{id=39| (_/(x+2)).simplify_full() /// \newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle \frac{{\left(x^{2} + 3 \, x + 6\right)}}{{\left(x + 2\right)}} }}}

The last division cannot be performed without remainder. Really, the polynomial $x^2+3x+6$ has not zero $x=-2$. To summarize, we succeded to divide the polynomial $P(x)$ by $x+2$ three times. The last quotient is $x^2+3x+6$. Hence $P(x)$ can be written as $(x+2)^3(x^2+3x+6)$.

{{{id=40| ((x+2)^3*(x^2+3*x+6)).show() ((x+2)^3*(x^2+3*x+6)).expand() ///
{\left(x + 2\right)}^{3} {\left(x^{2} + 3 \, x + 6\right)}
\newcommand{\Bold}[1]{\mathbf{#1}}\displaystyle x^{5} + 9 \, x^{4} + 36 \, x^{3} + 80 \, x^{2} + 96 \, x + 48 }}}

We can ensure that the polynomial equals $P(x)$. The underscore reffers to the previous result and bool function tests, if the equality is True.

{{{id=41| bool((_) == P(x)) /// \newcommand{\Bold}[1]{\mathbf{#1}}{\rm True} }}}

Multiplicity and sign of the polynomial

{{{id=42| P(x)=(x-2)^2*x^3*(x+1) P(x) /// \newcommand{\Bold}[1]{\mathbf{#1}}{(x - 2)}^{2} {(x + 1)} x^{3} }}}

The polynomial changes sing in the neigborhood of the zero with odd multiplicity and preserves sign in the neighborhood of the zero with even multiplicity.

{{{id=45| plot(P(x),(x,-2,3)).show(ymin=-1,ymax=4,figsize=4) /// }}} {{{id=47| /// }}}