Answer the question
In order to leave comments, you need to log in
How to do a linear approximation of a function in python?
how to do a linear approximation of a function in python?
can it be done for any function?
Answer the question
In order to leave comments, you need to log in
Use polyfit. It creates a polynomial using the least squares method. You substitute the x, y values and the degree of the polynomial. In your case - 1.
from matplotlib import pyplot as plt
import numpy as np
#функция
x = np.arange(0.1, 10, 1*(10**-2))
y = np.sin(x) + np.log(x)
#полином 1 степени по функции
p = np.polyfit(x,y, 1)
#подставляем значения x к полученному полиному
ya = np.polyval(p, x)
plt.plot(x, y)
plt.plot(x, ya)
plt.show()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question