From 969878593e24a3dbdab40d8807ed93ef7642a117 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 29 Sep 2022 21:00:21 +0200 Subject: [PATCH] Created ZIP --- Abgabe_Schenk_Brandenberger_Serie1.zip | Bin 0 -> 3402 bytes .../schenk_brandeberger_S1_Aufg1.py | 50 ++++++++++++ .../schenk_brandeberger_S1_Aufg2.py | 61 +++++++++++++++ .../schenk_brandeberger_S1_Aufg2_skript.py | 21 +++++ .../schenk_brandeberger_S1_Aufg3.py | 73 ++++++++++++++++++ 5 files changed, 205 insertions(+) create mode 100644 Abgabe_Schenk_Brandenberger_Serie1.zip create mode 100644 Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg1.py create mode 100644 Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg2.py create mode 100644 Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg2_skript.py create mode 100644 Abgabe_Schenk_Brandenberger_Serie1/schenk_brandeberger_S1_Aufg3.py diff --git a/Abgabe_Schenk_Brandenberger_Serie1.zip b/Abgabe_Schenk_Brandenberger_Serie1.zip new file mode 100644 index 0000000000000000000000000000000000000000..b31d4c1c8fbe30d48258f19dfaf1a6fb53b7c735 GIT binary patch literal 3402 zcmb`KS5#B$7KIb(MQSKYN2&;f;-M2jdPG2)G-(EcbOJ~dDTfkzCv+28+--o0Bul!OR5?^Y(Di@^|vS1M_x<`MbgV9n4_WkG~x>=k(Q!X;M2Vb}o@$BL8$( zuix~F7g^l7t4oZTE323IoMyMnn)Hi9)dbY}D<-sGKP$0a;PDc5pi}FR=9n%v%96hj zR!w|6bI4p0!xpNcl09+2+tGCeUVu%0jA7dXpm!nmQ=0PdV3o5YN`dPo6XZP<3H>ue zyc^kC%ED3ahfj|qbEL4#HEhA+NyLUgfeJAL)a1U^+hxy@a~tv;kDxfF5>vQ3BzD90 z5+inF-M%O`KS-dOIL>-|j?UR~?#9v%VX_sF2&dk>h?rfct`beC|rL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HcmV?d00001 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) + +