[Python] Trying to plot a picewise-defined function in Python

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    Just trying to plot a piecewise defined function in python but having trouble with an error. "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". I understand that the issue is python cannot analyze the truth of an entire array at once, however I don't believe a.any() or a.all() resolves the problem. Here are my two attempts at the code:

    x = linspace(0,3,1000)

    def S(x):
    if 0 <= x <= 1:
    return -1 + x - (5/9)*x**2 + (5/9)*x**3
    elif 1 <= x <= 2:
    return (14/9)*(x-1) + (10/9)*(x-1)**2 - (2/3)*(x-1)**3
    elif 2 <= x <=3:
    return 2 + (16/9)*(x-2) - (8/9)*(x-2)**2 - (1/9)*(x-2)**3

    plot(x, S(x), 'k')
    show()


    x = linspace(0,3,1000)
    plot(x, piecewise(x, [x<=1, 1<=x<=2, x>=2], [lambda x: -1 + x - (5/9)*x**2 + (5/9)*x**3,
    lambda x: (14/9)*(x-1) + (10/9)*(x-1)**2 - (2/3)*(x-1)**3,
    lambda x: 2 + (16/9)*(x-2) - (8/9)*(x-2)**2 - (1/9)*(x-2)**3]))

    Continue reading...

Compartilhe esta Página