diff --git a/Abgabe_Schenk_Brandenberger_Serie1.zip b/Abgabe_Schenk_Brandenberger_Serie1.zip new file mode 100644 index 0000000..b31d4c1 Binary files /dev/null and b/Abgabe_Schenk_Brandenberger_Serie1.zip differ diff --git a/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg1.py b/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg1.py new file mode 100644 index 0000000..02c3eb7 --- /dev/null +++ b/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg1.py @@ -0,0 +1,50 @@ +import numpy as np +import matplotlib.pyplot as plt + +xmin, xmax, xsteps = -10, 10, 0.1 +plotLegend = [] + +def showPlot(): + plt.xlim(-11, 11) + 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 1") + plt.show() + +def function_f(x): + return x ** 5 - 5 * x ** 4 - 30 * x ** 3 + 110 * x ** 2 + 29 * x - 105 + +def derivative_f(x): + return 5 * x ** 4 - 20 * x ** 3 - 90 * x ** 2 + 220 * x + 29 + +def integral_f(x): + c = 0 + return (1/6) * x ** 6 - x ** 5 - (30/4) * x ** 4 + (110/3) * x ** 3 + (29/2) * x ** 2 - 105 * x + c + +def plot_function_f(): + x = np.arange(xmin, xmax + xsteps, xsteps) + f = np.array(function_f(x)) + plt.plot(x, f) + plotLegend.append('f(x)') + +def plot_derivative_f(): + x = np.arange(xmin, xmax + xsteps, xsteps) + f = np.array(derivative_f(x)) + plt.plot(x, f) + plotLegend.append('f\'(x)') + +def plot_integral_f(): + x = np.arange(xmin, xmax + xsteps, xsteps) + f = np.array(integral_f(x)) + plt.plot(x, f) + plotLegend.append('F(x)') + +if __name__ == "__main__": + plot_function_f() + plot_derivative_f() + plot_integral_f() + showPlot() \ No newline at end of file diff --git a/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg2.py b/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg2.py new file mode 100644 index 0000000..8c2bf57 --- /dev/null +++ b/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg2.py @@ -0,0 +1,61 @@ +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 schenk_brandeberger_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) diff --git a/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg2_skript.py b/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg2_skript.py new file mode 100644 index 0000000..6dbe017 --- /dev/null +++ b/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg2_skript.py @@ -0,0 +1,21 @@ +import numpy as np +from schenk_brandeberger_S1_Aufg2 import schenk_brandeberger_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] = schenk_brandeberger_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)) \ No newline at end of file diff --git a/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg3.py b/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg3.py new file mode 100644 index 0000000..20c5a34 --- /dev/null +++ b/Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg3.py @@ -0,0 +1,73 @@ +import timeit +import numpy as np + +def fact_rec(n): + # y = fact_rec(n) berechnet die Fakultät von n als fact_rec(n) = n * fact_rec(n -1) mit fact_rec(0) = 1 + # Fehler, falls n < 0 oder nicht ganzzahlig + if n < 0 or np.trunc(n) != n: + raise Exception('The factorial is defined only for positive integers') + if n <=1: + return 1 + else: + return n*fact_rec(n-1) + +def fact_for(n): + if n < 0 or np.trunc(n) != n: + raise Exception('The factorial is defined only for positive integers') + result = 1 + for i in range(1, n + 1): + result *= i + return result + +def test_same_value(maxn): + test_successful = True + for n in range(maxn + 1): + if(fact_rec(n)) != fact_for(n): + print("Test (same value) failed at: ", n) + test_successful = False + if test_successful: + print("Test (same value) successful. Max n: ", maxn) + return test_successful + +def compaire_execution_times(n, execution_count): + print("Starting Test to comaire execution times:") + time_rec = np.mean(np.array(timeit.repeat("fact_rec(" + str(n) + ")", "from __main__ import fact_rec", number=execution_count))) + time_for = np.mean(np.array(timeit.repeat("fact_for(" + str(n) + ")", "from __main__ import fact_for", number=execution_count))) + factor = time_rec / time_for + print("time recursively: ", time_rec) + print("time with for loop: ", time_for) + print("execution with for loop is ", factor, " times faster.") + + # mit einer For-Schleife ist die Ausführung etwa 9 mal schneller. Wenn die Fakultät rekursiv berechnet wird muss die Funktion n mal aufgerufen werden + # und es müssen entsprechend viele zwischenergebnisse gespeichert werden bis die Berechnung abgeschlossen ist. + # Mit einer For-Schleife kann jeweils das letzte zwischenergebnis verworfen / überschrieben werden. + +def find_upper_limit_int(min_n, max_n): + print("Starting Test upper Limit with int:") + for n in range(min_n, max_n + 1): + try: + print(n, ": ", fact_for(n)) + except Exception as e: + print("Failed at n = ", n, "Error Message:\n", str(e)) + + # Für Integer gibt es keine Obergrenze. Die Werte werden berechnet. + +def find_upper_limit_float(min_n, max_n): + print("Starting Test upper Limit with float:") + for n in range(min_n, max_n + 1): + try: + print(n, ": ", float(fact_for(n))) + except Exception as e: + print("Failed at n = ", n, "Error Message:\n", str(e)) + + # Für Float gibt es eine Obergrenze. Wird diese überschritten können die Werte nicht mehr als Float ausgegeben werden. + + + +if __name__ == "__main__": + test_same_value(50) + compaire_execution_times(500, 100) + find_upper_limit_int(190, 200) + find_upper_limit_float(170, 171) + +