Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
Need help/advice with piecewise non linear curve fitting
Post Body

Hello all, python noob here. I am trying to implement curve fitting for a piecewise non-linear function using np.piecewise, and the results I am getting are like this. The black one is the original data and the blue one is the "fitted curve". As you can see, the fit is not at all proper. The function is supposed to be like: y=0 when x< threshold, and a quadratic function for x>threshold (please see the code below for the exact function). The problem is, its not updating the value of threshold beyond the initial default value i.e. 1. I tried to solve this by placing bounds on the threshold in my curve_fit function but that gives an error as "missing 1 required positional argument", which I am unable to figure out. So my questions are:

  1. Is np.piecewise the right way to go about this or is there some other module which is better suited for what I want to do?
  2. If np.piecewise is right, then what can I do to solve the threshold problem?

Please let me know if my question is not clear. I am extremely new to python so I am not quite caught up with the lingo yet. Everything I am doing is through googling. Here's my code:

def piecewise_linear(id_current_th, a_th, vth_th, vgs):
    return np.piecewise(id_current_th, [id_current_th < vth_th, id_current_th > vth_th], [lambda id_current_th: 0, lambda id_current_th: a_th * vgs ** 2 - 2 * a_th * vth_th * vgs   a_th * vth_th ** 2])  #vth_th is the threshold


def func_id_current(vgs, a, vth):
    return a * vgs ** 2 - 2 * a * vth * vgs   a * vth ** 2

def transfer_characteristics_threshold(vgs_th, id_current_th):
    vgs_th1 = pd.Series(vgs_th)
    id_current_th1 = pd.Series(id_current_th)
    vgs_th = vgs_th1.squeeze()

    id_current_th_new = np.array(id_current_th)
    vgs_th_new=np.array(vgs_th)


    params, cov = curve_fit(piecewise_linear, np.transpose(vgs_th_new), np.transpose(id_current_th_new))

    a_th, vth_th = params[0], params[1]
    id_current_th_fit1 = a_th * vgs_th ** 2 - 2 * a_th * vth_th * vgs_th  a_th * vth_th ** 2

    plt.subplot(2, 2, 4)
    plt.plot(vgs_th1, id_current_th1, marker='o', ms=3, color='black', label="vgs-original")
    plt.plot(vgs_th1, id_current_th_fit1, color='blue',label="")
    plt.xlabel('Vgs(V)')
    plt.ylabel('Id(A)')
    plt.legend(loc='best', fancybox=True, shadow=True)
    plt.grid(True)
    return a_th * 2, vth_th

Author
Account Strength
100%
Account Age
4 years
Verified Email
Yes
Verified Flair
No
Total Karma
30,247
Link Karma
5,643
Comment Karma
22,995
Profile updated: 1 day ago
Posts updated: 1 month ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago