62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
plotLegend = []
|
|
|
|
def showPlot(xmin, xmax, xsteps):
|
|
plt.xlim(xmin - 1, xmax + 1)
|
|
plt.xticks(np.arange(xmin, xmax + xsteps, 1.0))
|
|
plt.xlabel("x")
|
|
plt.ylim(-1300, 1300)
|
|
plt.ylabel("y")
|
|
plt.grid(markevery=1)
|
|
plt.legend(plotLegend)
|
|
plt.title("Aufgabe 2")
|
|
plt.show()
|
|
|
|
def polynom_function(a, x): #a = coefficients, x = values to calculate
|
|
a = np.squeeze(np.asarray(a))
|
|
p = np.array([])
|
|
for x_value in x:
|
|
result = 0
|
|
for power, coefficient in enumerate(a):
|
|
result += coefficient * x_value ** power
|
|
p = np.append(p, result)
|
|
return p
|
|
|
|
def derivative_f(a):
|
|
a = np.squeeze(np.asarray(a))
|
|
result = np.array([])
|
|
for i in range(1, len(a)):
|
|
result = np.append(result, a[i] * i)
|
|
return result
|
|
|
|
def integral_f(a):
|
|
a = np.squeeze(np.asarray(a))
|
|
c = 0
|
|
result = np.array([c])
|
|
for i in range(0, len(a)):
|
|
result = np.append(result, a[i] / (i + 1))
|
|
return result
|
|
|
|
def plot_function(x, y, legendString):
|
|
plt.plot(x, y)
|
|
plotLegend.append(legendString)
|
|
|
|
def is_valid_vector(vector):
|
|
shape = np.shape(vector)
|
|
if(len(shape) == 2):
|
|
return (shape[0] == 1 and shape[1] >= 1) or (shape[0] >= 1 and shape[1] == 1)
|
|
else:
|
|
return False
|
|
|
|
def roman_schenk_Aufg2(a, xmin, xmax):
|
|
if not is_valid_vector(a):
|
|
raise Exception('Fehler! a ist kein gültiger Spalten- oder Zeilenvektor!')
|
|
xsteps = abs(xmax/100.0)
|
|
x = np.arange(xmin, xmax + xsteps, xsteps)
|
|
p = polynom_function(a, x)
|
|
dp = polynom_function(derivative_f(a), x)
|
|
pint = polynom_function(integral_f(a), x)
|
|
return(x,p,dp,pint)
|