Piecewise functions

  1. Write the function fun_mat_1() that takes an x value (float), and returns f(x) (float).

    \[\begin{split}f(x) = \begin{cases} \frac{5x-1}{\sqrt{\lvert x-1 \rvert}} & x<=-1 \\ 2^{x} & x>-1 \end{cases}\end{split}\]

    Save this function into a file named fun_mat_1.py. See the following examples:

    >>> round(fun_mat_1(-104.2), 2)
    -50.89
    >>> round(fun_mat_1(-1.8), 2)
    -5.98
    >>> round(fun_mat_1(-1.0), 2)
    -4.24
    >>> round(fun_mat_1(0.0), 2)
    1.0
    >>> round(fun_mat_1(3.5), 2)
    11.31
    >>> round(fun_mat_1(10.0), 2)
    1024.0
    

    Note

    More tests are provided in the fun_mat_1.txt file

  2. Write the function fun_mat_2() that takes two values x and y (float), and returns g(x, y) (float).

    \[\begin{split}g(x,y) = \begin{cases} \frac{x^2-y^2}{e^{x+y}-1} & x>-y, \\ 2^x & x=-y, \\ \frac{\sin (x^2-y^2)}{x+y} & x<-y. \end{cases}\end{split}\]

    Save this function into a file named fun_mat_2.py. See the following examples:

    >>> round(fun_mat_2(4.2, 4.2), 4)
    0.0
    >>> round(fun_mat_2(-0.3, 0.7), 4)
    -0.8133
    >>> round(fun_mat_2(20.9, -11.1), 4)
    0.0174
    >>> round(fun_mat_2(10.0, -10.0), 4)
    1024.0
    >>> round(fun_mat_2(0.0, 0.0), 4)
    1.0
    >>> round(fun_mat_2(0.3, -0.3), 4)
    1.2311
    >>> round(fun_mat_2(-5.5, 4.2), 4)
    -0.0336
    >>> round(fun_mat_2(0.0, -11.1), 4)
    -0.0572
    

    Note

    More tests are provided in the fun_mat_2.txt file

  3. Write the function fun_mat_3() that takes two values x and y (float), and returns h(x, y) (float).

    \[\begin{split}h(x,y) = \begin{cases} x^2+y, & \mbox{si } x>=1\\ sin(x), & \mbox{si } 0<x<1\\ y*cos(x), & \mbox{si } x<=0 \end{cases}\end{split}\]

    Save this function into a file named fun_mat_3.py. See the following examples:

    >>> round(fun_mat_3(4.2, 4.2), 4)
    21.84
    >>> round(fun_mat_3(1.0, 0.7), 4)
    1.7
    >>> round(fun_mat_3(0.9, -11.1), 4)
    0.7833
    >>> round(fun_mat_3(0.15, 0.0), 4)
    0.1494
    >>> round(fun_mat_3(-5.5, 4.2), 4)
    2.9764
    >>> round(fun_mat_3(-0.7, -11.1), 4)
    -8.4897
    

    Note

    More tests are provided in the fun_mat_3.txt file

Solution

Solutions are provided in files fun_mat_1.py, fun_mat_2.py and fun_mat_3.py