Hoşgeldin Misafir

Python Taylor Serisi hesaplama ve çizdirme (plot)

Gauss

3 May 2019
11 Mesaj

Aktiflik

Seviye

Deneyim

TIM / GÖREV:
import math 
import numpy as np
import matplotlib.pyplot as plt
import pylab
def func(x):
    return -0.1*x**4-0.15*x**3-0.5*x**2-0.25*x+1.2
#first derivativ
def fdfunc(x):
    return -0.4*x**3-.45*x**2-x-0.25
#second order derivative
def sdfunc(x):
    return -1.2*x**2-0.9*x-1
def taylorh(f, d1_f, d2_f, x, n):
    h = x 
    e=[]
    fapp=[]
    for i in range(n):
        f_a=f(x-h) + d1_f(x-h)*h +(d2_f(x-h)*h**2)/2
        fapp.append(f_a)
        e.append(abs(f(x)-f_a))
        h=h/2
        return fapp, e
        xval, yval = taylorh(func, fdfunc , sdfunc, 5.0 ,10,)
        xr=[(1.0/(2**x))for x in range (10)]
        print (xr)
        print (yval)

        pylab.plot(xr, yval, 'kx')
        pylab.ylabel('absolute error')
        pylab.ylabel('')
        pylab.title('Plot of absolute error as h decreases')
        pylab.semilogy()