Created
April 17, 2020 18:09
-
-
Save markusrenepae/b11bb189d39163898c4c795c0f014136 to your computer and use it in GitHub Desktop.
See programmilõik käib kaasas minu kodutööga, mille eesmärgiks oli lähendada Taylori valemiga funktsiooni f(x) = ln(x).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib | |
import numpy as np | |
from math import * | |
import matplotlib.pyplot as plt | |
'''Funktsioon, mida peame lähendama: f(x) = ln(x)''' | |
def f(x): | |
output = [] | |
for elem in x: | |
output.append(log(elem)) | |
return np.asarray(output) | |
'''See funktsioon loob Taylori polünoomi konstandid. | |
Konstandid on ümardatud kümne komakoha täpsuseni, et | |
vältida segadust 64-bitiste arvude aritmeetikaga -- | |
null olgu võrdne nulliga, mitte nullilähedase väärtusega!''' | |
def coef(n): | |
value = 1/n*(-1)**(n+1) | |
return round(value,10) | |
def taylor(n): | |
def P(x): | |
total = log(4) | |
for i in range(1, n+1): | |
total += ((x-4)/4)**i * coef(i) | |
return total | |
return P | |
P = taylor(5) | |
x = np.linspace(3, 5, num=40) | |
plt.plot(x, f(x)-P(x), 'bo',label="f(x)-T_5(x)") | |
plt.legend(loc="best") | |
plt.show() | |
x = np.linspace(1, 8, num=40) | |
plt.plot(x, P(x), 'bo', label="T_5(x)") | |
plt.plot(x, f(x), 'r-', label="f(x)") | |
plt.legend(loc="upper left") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment