Solved Task 2

This commit is contained in:
schrom01 2022-09-25 13:37:37 +02:00
parent ed6b6153bf
commit 3a38088812
2 changed files with 60 additions and 39 deletions

View File

@ -4,7 +4,7 @@ import matplotlib.pyplot as plt
plotLegend = [] plotLegend = []
def showPlot(xmin, xmax, xsteps): def showPlot(xmin, xmax, xsteps):
plt.xlim(-11, 11) plt.xlim(xmin - 1, xmax + 1)
plt.xticks(np.arange(xmin, xmax + xsteps, 1.0)) plt.xticks(np.arange(xmin, xmax + xsteps, 1.0))
plt.xlabel("x") plt.xlabel("x")
plt.ylim(-1300, 1300) plt.ylim(-1300, 1300)
@ -14,48 +14,48 @@ def showPlot(xmin, xmax, xsteps):
plt.title("Aufgabe 2") plt.title("Aufgabe 2")
plt.show() plt.show()
def polynom_function(coefficients, x): def polynom_function(a, x): #a = coefficients, x = values to calculate
result = 0 a = np.squeeze(np.asarray(a))
for power, coefficient in enumerate(coefficients): p = np.array([])
result += coefficient * x ** power 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 return result
def derivative_f(coefficients): def integral_f(a):
result = [] a = np.squeeze(np.asarray(a))
for i in range(1, len(coefficients)):
result.append(coefficients[i] * i)
return result
def integral_f(coefficients):
c = 0 c = 0
result = [c] result = np.array([c])
for i in range(0, len(coefficients)): for i in range(0, len(a)):
result.append(coefficients[i] / (i+1)) result = np.append(result, a[i] / (i + 1))
return result return result
def plot_function(x, y, legendString):
plt.plot(x, y)
plotLegend.append(legendString)
def plot_polynom_function(coefficients, xmin, xmax, xsteps): 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) x = np.arange(xmin, xmax + xsteps, xsteps)
f = np.array(polynom_function(coefficients, x)) p = polynom_function(a, x)
plt.plot(x, f) dp = polynom_function(derivative_f(a), x)
plotLegend.append('f(x)') pint = polynom_function(integral_f(a), x)
return(x,p,dp,pint)
def plot_derivative_f(coefficients, xmin, xmax, xsteps):
x = np.arange(xmin, xmax + xsteps, xsteps)
f = np.array(polynom_function(derivative_f(coefficients), x))
plt.plot(x, f)
plotLegend.append('f\'(x)')
def plot_integral_f(coefficients, xmin, xmax, xsteps):
x = np.arange(xmin, xmax + xsteps, xsteps)
f = np.array(polynom_function(integral_f(coefficients), x))
plt.plot(x, f)
plotLegend.append('F(x)')
if __name__ == "__main__":
xmin, xmax, xsteps = -10, 10, 0.1
coefficients_task_1 = [-105, 29, 110, -30, -5, 1]
plot_polynom_function(coefficients_task_1, xmin, xmax, xsteps)
plot_derivative_f(coefficients_task_1, xmin, xmax, xsteps)
plot_integral_f(coefficients_task_1, xmin, xmax, xsteps)
showPlot(xmin, xmax, xsteps)

View File

@ -0,0 +1,21 @@
import numpy as np
from roman_schenk_S1_Aufg2 import roman_schenk_Aufg2, plot_function, showPlot
if __name__ == "__main__":
#coefficients_task_1 = np.array([-105, 29, 110, -30, -5, 1]) #falsches Format
#coefficients_task_1 = np.array([[-105, 29, 110, -30, -5, 1]]) #Zeilenvektor
coefficients_task_1 = np.array([[-105], [29], [110], [-30], [-5], [1]]) #Spalten Vektor
xmin = -10
xmax = 10
[x,p,dp,pint] = roman_schenk_Aufg2(coefficients_task_1, xmin, xmax)
print("x:\n", x)
print("p:\n", p)
print("dp:\n", dp)
print("pint:\n", pint)
plot_function(x, p, 'f(x)')
plot_function(x, dp, 'f\'(x)')
plot_function(x, pint, 'F(x)')
showPlot(xmin, xmax, abs(xmax/100.0))