Second version of exercise 2

This commit is contained in:
Hussein Kabbout 2020-09-26 15:30:04 +02:00
parent 36da730e85
commit d7a8dd8721

View File

@ -6,7 +6,7 @@ import matplotlib.pyplot as plt
STEPS = 1 STEPS = 1
def gen_polynom(range_, coefficients, derivativ=False, integral=False): def gen_polynom(x, coefficients, derivativ=False, integral=False):
if derivativ is True: if derivativ is True:
n = len(coefficients) - 1 n = len(coefficients) - 1
deriv_coefficients = coefficients deriv_coefficients = coefficients
@ -30,12 +30,16 @@ def gen_polynom(range_, coefficients, derivativ=False, integral=False):
else: else:
n = len(coefficients) - 1 n = len(coefficients) - 1
x = range_ y = 0
for coefficient in coefficients: for coefficient in coefficients:
print(str(coefficient) + "*" + str(x) + "**" + str(n)) if n < 0:
x = coefficient * range_ ** n break
elif n == 0:
y += coefficient * x
else:
y += coefficient * (x ** n)
n -= 1 n -= 1
return x return y
def calc_func(vector, xmin, xmax, derivativ=False, integral=False): def calc_func(vector, xmin, xmax, derivativ=False, integral=False):
@ -45,11 +49,11 @@ def calc_func(vector, xmin, xmax, derivativ=False, integral=False):
plt.plot(x, y, label="f(x)") plt.plot(x, y, label="f(x)")
if derivativ is True: if derivativ is True:
y = gen_polynom(x, vector, derivativ=True) y = gen_polynom(x, vector, derivativ=derivativ)
plt.plot(x, y, label="f'(x)") plt.plot(x, y, label="f'(x)")
if integral is True: if integral is True:
y = gen_polynom(x, vector, integral=True) y = gen_polynom(x, vector, integral=integral)
plt.plot(x, y, label="F(x)") plt.plot(x, y, label="F(x)")
@ -67,7 +71,7 @@ if __name__ == "__main__":
xmax = 5 xmax = 5
check_values(vector, xmin, xmax) check_values(vector, xmin, xmax)
calc_func(vector, xmin, xmax, derivativ=True) calc_func(vector, xmin, xmax)
plt.grid() plt.grid()
plt.xlabel("x") plt.xlabel("x")