Graphing a Function


#1



Hi, I'm new to these forums so I'm not positive if this is the right place to ask a question not specifically about an issue with the Python lessons here. I've been learning Python through codeacademy, and upon completing the functions lesson, I realized there was a complicated function that I needed to graph and that wolframalpha was unable to assist me with. I realized that with my newfound knowledge of python functions I could write the function with Python. I succeeded in this, but I am unable to figure out how to graph my function. I don't understand numpy very well, but I ran my program with jupyter and tried to use the numpy commands but it didn't work.

tl:dr; what's the simplest way to graph a function that I've defined in python?

I'll link the code for my function below. The program prints " g('x value') = 'function output' "


def g(x):
    if (x)<2:
        result1 = (-x)+1
        if result1<2:
            result = (-1 * result1)+1
            print("g(" + str(x) + ") = " + str(result)) 
        elif result1 == 2:
            result = 1
            print("g(" + str(x) + ") = " + str(result)) 
        elif 2<result1<=4:
            result = ((result1) * -1 / 2) + 4
            print("g(" + str(x) + ") = " + str(result)) 
        elif 4<result1:
            result = (result1)-2
            print("g(" + str(x) + ") = " + str(result)) 
    elif (x) == 2:
        result1 = 1
        if result1<2:
            result = (-1 * result1)+1
            print("g(" + str(x) + ") = " + str(result)) 
        elif result1 == 2:
            result = 1
            print("g(" + str(x) + ") = " + str(result)) 
        elif 2<result1<=4:
            result = ((result1) * -1 / 2) + 4
            print("g(" + str(x) + ") = " + str(result)) 
        elif 4<result1:
            result = (result1)-2
            print("g(" + str(x) + ") = " + str(result))
    elif 2<(x)<=4:
        result1 = ((x) * -1 / 2) + 4
        if result1<2:
            result = (-1 * result1)+1
            print("g(" + str(x) + ") = " + str(result)) 
        elif result1 == 2:
            result = 1
            print("g(" + str(x) + ") = " + str(result)) 
        elif 2<result1<=4:
            result = ((result1) * -1 / 2) + 4
            print("g(" + str(x) + ") = " + str(result)) 
        elif 4<result1:
            result = (result1)-2
            print("g(" + str(x) + ") = " + str(result))
    elif 4<(x):
        result1 = x - 2
        if result1<2:
            result = (-1 * result1)+1
            print("g(" + str(x) + ") = " + str(result)) 
        elif result1 == 2:
            result = 1
            print("g(" + str(x) + ") = " + str(result)) 
        elif 2<result1<=4:
            result = ((result1) * -1 / 2) + 4
            print("g(" + str(x) + ") = " + str(result)) 
        elif 4<result1:
            result = (result1)-2
            print("g(" + str(x) + ") = " + str(result))


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.