Python Forum
Line of best fit for forward bias curve?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Line of best fit for forward bias curve?
#1
Hello folks,

I have a physics report to write up and graphs need to be digitally produced. How do I add a line of best for the data below (the data points match the forward bias characteristic for a silicon diode - i.e. exponential curve):

import numpy as np
import matplotlib.pyplot as plt
plt.figure("Forward Bias Characteristic")
x = np.array([0.00, 0.53, 0.55, 0.57, 0.59, 0.61, 0.63, 0.65, 0.67, 0.69])
y = np.array([0.0, 0.0, 0.1, 0.2, 0.5, 1.0, 1.2, 2.3, 3.5, 5.4])
yerror = np.array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1,])
plt.plot(x, y, 'rx')
plt.xlabel('$Volatge (V)$')
plt.ylabel('$Current (mA)$')
Reply
#2
perhaps reading https://pyspice.fabrice-salvaire.fr/exam...curve.html will help
Reply
#3
(Feb-02-2020, 02:19 PM)Larz60+ Wrote: perhaps reading https://pyspice.fabrice-salvaire.fr/exam...curve.html will help

Hi Larz60+,

Thanks but I think the info on the link is to do with generating a characteristic curve for forward bias ... I'm simply looking to plot a best fit curve for my actual data points that I've produced in my x and y arrays ...? The info in your link is way above my very basic grasp of python.
Reply
#4
Any help?
Reply
#5
Asking for a linear regression, not sure your data will exactly fit, but here goes.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, linear_model
plt.figure("Forward Bias Characteristic")
x = np.array([0.00, 0.53, 0.55, 0.57, 0.59, 0.61, 0.63, 0.65, 0.67, 0.69])
y = np.array([0.0, 0.0, 0.1, 0.2, 0.5, 1.0, 1.2, 2.3, 3.5, 5.4])
yerror = np.array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1,])
regr = linear_model.LinearRegression()
X = x.reshape(-1,1)
regr.fit(X, y)
y_pred = regr.predict(X)
plt.plot(x, y_pred, color='blue', linewidth=3)
plt.plot(x, y, 'rx')
plt.xlabel('$Volatge (V)$')
plt.ylabel('$Current (mA)$')
Data looks like a curve, line is best fit line.
Reply
#6
(Feb-04-2020, 12:17 PM)jefsummers Wrote: Asking for a linear regression, not sure your data will exactly fit, but here goes.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, linear_model
plt.figure("Forward Bias Characteristic")
x = np.array([0.00, 0.53, 0.55, 0.57, 0.59, 0.61, 0.63, 0.65, 0.67, 0.69])
y = np.array([0.0, 0.0, 0.1, 0.2, 0.5, 1.0, 1.2, 2.3, 3.5, 5.4])
yerror = np.array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1,])
regr = linear_model.LinearRegression()
X = x.reshape(-1,1)
regr.fit(X, y)
y_pred = regr.predict(X)
plt.plot(x, y_pred, color='blue', linewidth=3)
plt.plot(x, y, 'rx')
plt.xlabel('$Volatge (V)$')
plt.ylabel('$Current (mA)$')
Data looks like a curve, line is best fit line.

It's a best fit curve I need ....

Do I have to use:

def func ()
with the return modelling an exponential? Just sharing what google has thrown up.
Reply
#7
Ok. I read first post with "line of best fit" and assumed straight line. Here's something to tinker with. Support Vector Regressions using polynomial regression and RBF. Check the scikit-learn docs on regression for more ideas if needed.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, linear_model
from sklearn.svm import SVR
plt.figure("Forward Bias Characteristic")
x = np.array([0.00, 0.53, 0.55, 0.57, 0.59, 0.61, 0.63, 0.65, 0.67, 0.69])
y = np.array([0.0, 0.0, 0.1, 0.2, 0.5, 1.0, 1.2, 2.3, 3.5, 5.4])
yerror = np.array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1,])
regr = linear_model.LinearRegression()
svr_rbf = SVR(kernel='rbf', C=100, gamma=0.1, epsilon=.1)
svr_poly = SVR(kernel='poly', C=100, gamma='auto', degree=3, epsilon=.1,
               coef0=1)
X = x.reshape(-1,1)
regr.fit(X, y)
svr_rbf.fit(X,y)
svr_poly.fit(X,y)
y_pred = regr.predict(X)
y1_pred = svr_rbf.predict(X)
y2_pred = svr_poly.predict(X)
plt.plot(x, y_pred, color='blue', linewidth=2)
plt.plot(x, y1_pred, color='green', linewidth=2)
plt.plot(x, y2_pred, color='black', linewidth=2)
plt.plot(x, y, 'rx')
plt.xlabel('$Volatge (V)$')
plt.ylabel('$Current (mA)$')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Maya] MASH Curve node in Python Tomatoprincess 1 2,733 Jun-17-2019, 03:21 PM
Last Post: Tomatoprincess
  Finding the Young's Modulues in a curve stress strain AicramM 1 4,471 Nov-03-2018, 09:01 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020