def algorithm(point, lmbda): fhelper.func = "x**2" # raw_input("Enter function: ") x_range = np.arange(-50, 50) fig = plt.figure() ax = fig.add_subplot(111) plt_func, = ax.plot(x_range, [fhelper.f(value) for value in x_range], label=fhelper.func) plt.legend(handles=[plt_func]) # Algorithm df_result = fhelper.df(point) while abs(df_result) > abs(lmbda * 0.1): df_result = fhelper.df(point) point -= lmbda * df_result print("Extremalstelle bei x = %f" % point) # Plot approximation point1 = -40 x_range_tan = np.arange(point1 - 20, point1 + 20) ax.plot(x_range_tan, fhelper.tan(x_range_tan, point1)) point2 = -20 x_range_tan = np.arange(point2 - 20, point2 + 20) ax.plot(x_range_tan, fhelper.tan(x_range_tan, point2)) x_range_tan = np.arange(point - 20, point + 20) ax.plot(x_range_tan, fhelper.tan(x_range_tan, point)) plt.grid(True) ax.set_xlabel("x") ax.set_ylabel("y") plt.savefig("doc/gradient.png")
def bisect(a, b): c = (a + b) / 2 ax.plot((c, c), (0, fhelper.f(c) + 30), 'r-') if abs(fhelper.df(c)) < 0.001: return c elif fhelper.df(c) < 0: return bisect(c, b) else: return bisect(a, c)
def main(): fhelper.func = "math.sqrt(100**2+(2*100-x)**2)/2.1+math.sqrt(100**2+x**2)/4.1" x_range = np.arange(-150, 400) fig = plt.figure() ax = fig.add_subplot(111) plt_func, = ax.plot(x_range, [fhelper.f(value) for value in x_range], label='Equation (6)') plt.legend(handles=[plt_func]) plt.grid(True) ax.set_xlabel("Way [x]") ax.set_ylabel("Time [t]") plt.savefig("doc/lifeguard_100.png")
def main(): fhelper.func = raw_input("Enter a function: ") a = int(input("Enter x-LEFT: ")) b = int(input("Enter x-RIGHT: ")) x_range = np.arange(-50, 50) fig = plt.figure() global ax ax = fig.add_subplot(111) plt_func, = ax.plot(x_range, [fhelper.f(value) for value in x_range], label=fhelper.func) plt.legend(handles=[plt_func]) ax.plot((a, a), (0, fhelper.f(a) + 30), 'r-') ax.plot((b, b), (0, fhelper.f(b) + 30), 'r-') result = bisect(a, b) print(result) plt.grid(True) ax.set_xlabel("x") ax.set_ylabel("y") plt.savefig("doc/bisektion.png")