This commit is contained in:
Daniel Knüttel 2019-01-23 15:27:28 +01:00
parent 0c62b308cc
commit 2e1bbcf7cf
3 changed files with 95 additions and 0 deletions

28
ex_45.py Normal file
View File

@ -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)

58
ex_46.py Normal file
View File

@ -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))

9
ex_47.py Normal file
View File

@ -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)