Taylor function statement error

Asked 2 years ago, Updated 2 years ago, 18 views

import math

class tail: def facto(a):

    fac = 1

    if a == 0:

        fac == 1

    else:

        while a != 1:

            fac = (fac * a)

            a -= 1

    return fac


def asd(i):

    if (i % 2) == 0:

        a = 1

    else:

        a = -1

    return a


def sin(x,i):

    a = 0

    res = 0

    while a != i :

        res = res  + tail.asd(a)*(x**(2*(a+1)-1)/tail.facto(2*(a+1)-1))    

        a += 1
    return res

def cos(x,i):

    a = 0

    res = 0


    while a != i :

        res = res  + tail.asd(a)*(x**(2*(a+1)-2)/tail.facto(2*(a+1)-2))    

        a += 1
    return res

def cal(dif):
    start = 1
    while abs(abs(math.sin(math.pi * 0.1)) - abs(tail.sin(math.pi * 0.1, start))) > dif:

        start += 1

    print("for sin" + str(start) + "Error from the beginning" + str(dif) + " or less")


    start = 1
    while abs(abs(math.cos(math.pi * 0.1)) - abs(tail.cos(math.pi * 0.1, start))) > dif:

        start += 1

    print("for cos" + str(start) +"the error falls below" + str(dif) +" from the first")

dif = 0.0001

tail.cal(dif)

Traceback (most recent call last): File "/solution.py", line 87, in tail.cal(dif) TypeError: unbound method cal() must be called with tail instance as first argument (got float instance instead)

python

2022-09-21 23:16

1 Answers

It works well in my environment (Pycharm, Python interpreter 3.5.3).

In the case of sin, the error falls below 0.0001 from the second time In the case of cos, the error falls below 0.0001 from the third time

so output.


2022-09-21 23:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.