From 2e1bbcf7cff721d9ffead81c21a685aaef739cb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Wed, 23 Jan 2019 15:27:28 +0100 Subject: [PATCH] addes --- ex_45.py | 28 +++++++++++++++++++++++++++ ex_46.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ex_47.py | 9 +++++++++ 3 files changed, 95 insertions(+) create mode 100644 ex_45.py create mode 100644 ex_46.py create mode 100644 ex_47.py diff --git a/ex_45.py b/ex_45.py new file mode 100644 index 0000000..5fa8cbb --- /dev/null +++ b/ex_45.py @@ -0,0 +1,28 @@ +import numpy as np +from scipy import misc +import matplotlib.pyplot as plt + +from util.io import readvalue +def positive_int(s): + i = int(s) + if(i <= 0): + raise ValueError("{} is negative".format(i)) + return i + +file_input_name = readvalue("Input file name > ", str) +file_output_name = readvalue("Output file name > ", str) +cutoff = readvalue("Frequencies to cut > ", positive_int) + +fin = misc.imread(file_input_name, flatten=True) + + +transformed = np.fft.fft2(fin) + + +new_transformed = np.zeros(transformed.shape, dtype=np.complex) +new_transformed[cutoff:-cutoff, cutoff:-cutoff] = transformed[cutoff:-cutoff, cutoff:-cutoff] + + +new = np.fft.ifft2(new_transformed).real + +misc.imsave(file_output_name, new) diff --git a/ex_46.py b/ex_46.py new file mode 100644 index 0000000..552fb03 --- /dev/null +++ b/ex_46.py @@ -0,0 +1,58 @@ + +def bisec(f, a, b, eps, nmax): + """ + Takes f:[a,b] -> |R and tries to compute the null point + using bisection. + """ + + if(f(a)*f(b) >= 0): + raise ValueError("f(a)*f(b) >= 0") + + x_minus = a + x_plus = b + + if(a > b): + x_minus = b + x_plus = a + + for i in range(nmax): + x_minus, x_plus, err = bisec_one(x_minus, x_plus, f, eps) + if(err < eps): + break + if(err < eps): + return x_minus, err + raise ValueError("bisection hit nmax") + + +def bisec_one(x_minus, x_plus, f, eps): + a = x_minus + b = x_plus + + x_m = (b + a) / 2 + y_m = f(x_m) + + if(y_m < 0): + if(y_m > -1*eps): + return x_m, x_m, -y_m + + if(f(x_m)*f(b) >= 0): + return a, x_m, -y_m + return x_m, b, -y_m + + if(y_m < eps): + return x_m, x_m, y_m + + if(f(a)*f(x_m) >= 0): + return x_m, b, y_m + return a, x_m, y_m + + +if( __name__ == "__main__"): + f1 = lambda x: x + f2 = lambda x: x**3 + f3 = lambda x: -x + 1 + f4 = lambda x: -x**3 + 1 + + fs = [f1, f2, f3, f4] + for f in fs: + print(bisec(f, -12, 10, 0.001, 100)) diff --git a/ex_47.py b/ex_47.py new file mode 100644 index 0000000..b4abd9b --- /dev/null +++ b/ex_47.py @@ -0,0 +1,9 @@ +import numpy as np +from scipy.optimize import minimize + +f = lambda x: x*np.sin(7*x)*np.exp(-(x - 2)**2) + +x0 = f(np.arange(-20, 20, 0.1)).min() + +m = minimize(f, x0, method="CG") +print(m)