O
O
oleg_silantyev2019-05-27 11:04:31
Python
oleg_silantyev, 2019-05-27 11:04:31

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

1 answer(s)
I
Ivan, 2019-05-27
@Kurku

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()

5ceba3cec8b37269621753.jpeg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question