[Python] Intersection of two data sets, one linear and one not

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    I need to find the intersection of a straight line and a stress-strain curve to determine the offset yield strength. I've been trying to use the following which successfully prints the two lines but places the intersection point in the wrong place. strain1 and stress1 are both rather large arrays extracted from excel and m and c are the corresponding values for the appropriate straight line for strain1 and stress1. Pls help this struggling physics student :)

    strain_offset = strain1 + 0.002
    stress_offset = m[0]*strain1 + c[0]
    yield_strength_index = np.argwhere(np.diff(np.sign(stress1 - stress_offset)))[0][0]

    def intersection_point(Ax1, Ay1, Ax2, Ay2, Bx1, By1, Bx2, By2):
    d = (By2-By1)*(Ax2-Ax1)-(Bx2-Bx1)*(Ay2-Ay1)
    if d:
    uA = ((Bx2-Bx1)*(Ay1-By1)-(By2-By1)*(Ax1-Bx1))/d
    uB = ((Ax2-Ax1)*(Ay1-By1)-(Ay2-Ay1)*(Ax1-Bx1))/d
    else:
    return None

    x_intersection = Ax1 + uA * (Ax2 - Ax1)
    y_intersection = Ay1 + uA * (Ay2 - Ay1)

    return x_intersection, y_intersection


    first = yield_strength_index
    second = first + 1
    # A points from the stress strain curve
    Ax1 = strain1[first]
    Ay1 = stress1[first]
    Ax2 = strain1[second]
    Ay2 = stress1[second]
    # B points from the offset line
    Bx1 = strain_offset[first]
    By1 = stress_offset[first]
    Bx2 = strain_offset[second]
    By2 = stress_offset[second]

    # run our function that finds the intersection point
    x_intersection, y_intersection = intersection_point(Ax1,Ay1,Ax2,Ay2,Bx1,By1,Bx2,By2)

    print(x_intersection,y_intersection)

    fig,ax = plt.subplots()


    ax.plot(strain1,stress1)
    ax.plot(strain_offset,stress_offset)
    ax.plot(x_intersection,y_intersection,'go')

    ax.set_ylim([0,50000000])
    ax.set_xlim([0,0.035])

    plt.show()


    It outputs the following graph enter image description here

    Continue reading...

Compartilhe esta Página