HM1_Aufgabenserie1/roman_schenk_S1_Aufg2.py

62 lines
1.7 KiB
Python
Raw Normal View History

2022-09-24 20:18:08 +02:00
import numpy as np
import matplotlib.pyplot as plt
plotLegend = []
def showPlot(xmin, xmax, xsteps):
2022-09-25 13:37:37 +02:00
plt.xlim(xmin - 1, xmax + 1)
2022-09-24 20:18:08 +02:00
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()
2022-09-25 13:37:37 +02:00
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)
2022-09-24 20:18:08 +02:00
return result
2022-09-25 13:37:37 +02:00
def integral_f(a):
a = np.squeeze(np.asarray(a))
2022-09-25 11:38:20 +02:00
c = 0
2022-09-25 13:37:37 +02:00
result = np.array([c])
for i in range(0, len(a)):
result = np.append(result, a[i] / (i + 1))
2022-09-25 11:38:20 +02:00
return result
2022-09-25 13:37:37 +02:00
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)
2022-09-24 20:18:08 +02:00
x = np.arange(xmin, xmax + xsteps, xsteps)
2022-09-25 13:37:37 +02:00
p = polynom_function(a, x)
dp = polynom_function(derivative_f(a), x)
pint = polynom_function(integral_f(a), x)
return(x,p,dp,pint)