def Check1(a, b, epsilon, roundd): global iterations iterations = 0 if Lvl3(a) * der(Lvl3, a, 1.0) > 0: return Combined1(a, b, epsilon, roundd) else: return Combined1(b, a, epsilon, roundd)
def newton(x, eps, f): iters = 1 while True: xp = x - f(x) / der(f, x) if abs(xp - x) <= eps: break x = xp iters += 1 #print("{} итераций".format(iters)) return x
def Combined1(a, b, epsilon, roundd): global iterations x1 = a - Lvl3(a) / der(Lvl3, a, 1) x2 = b - (a - b) / (Lvl3(a) - Lvl3(b)) * Lvl3(b) iterations += 1 if abs(a - b) <= epsilon: print("eps = {}\t{}\t{}\t".format(epsilon, round(x2, roundd), iterations)) return x2 else: Combined1(x1, x2, epsilon, roundd)
def wave_equation_solve_1d(a, f, x, t, x_conditions, t_conditions): """ solves a wave equation of the following form: u_tt - a**2*u_xx == f(x, t) """ n_x, n_t = len(x), len(t) h = x[1] - x[0] tau = t[1] - t[0] sigma = 1. / 3. * (1 - (h / (a * tau)) ** 2) x, t = np.meshgrid(x, t) u = np.zeros((n_x, n_t)) mu = x_conditions phi = t_conditions u[0] = mu[0](t.transpose()[0]) u[-1] = mu[1](t.transpose()[-1]) u = u.transpose() u[0] = phi[0](x[0]) u[1] = u[0] + tau * phi[1](x[0]) + 0.5 * tau ** 2 * (a ** 2 * der(phi[0], x[0], dx=h ** 2, n=2) + f(x[0], t[0])) kappa = (a * tau / h) ** 2 for j in xrange(1, len(t) - 1): d_sub = - sigma * kappa * np.ones((n_x - 1,)) d_super = np.copy(d_sub) d_main = 1. + 2 * sigma * kappa * np.ones((n_x - 1,)) diags = [d_sub, d_main, d_super] nonuniformity = np.array(f(x[j][1:-1], t[j][1:-1])) rh = np.array(kappa * (1 - 2 * sigma) * (u[j - 1][2:] - 2 * u[j - 1][1:-1] + u[j - 1][:-2]) + sigma * (u[j][2:] - 2 * u[j][1:-1] + u[j][:-2]) + (tau ** 2) * nonuniformity + 2 * u[j][1:-1] - u[j - 1][1:-1]) rh[0] -= d_sub[0] * u[j + 1][0] rh[-1] -= d_super[-1] * u[j + 1][-1] u[j + 1][1:-1] = tridiag_solve(diags, rh) return u
import numpy as np from scipy.misc import derivative as der from scipy.interpolate import lagrange as lag x = [-2.2,-0.3,0.8,1.9] f_x = [-15.18, 10.962, 1.92, -2.04] poly = lag(x, f_x) deriv = der(poly, 0, 0.001, 1, order=5) deriv2 = der(poly, 0, 0.001, 2, order=5) print("Wartość f'(0)=", deriv) print("Wartość f''(0)=",deriv2)
from scipy.misc import derivative as der import numpy as np def f(x): return np.log(np.tanh(x / (x * x + 1))) point = 0.2 d_1 = der(f, point, 0.0001, 1, order=5) d_2 = der(f, point, 0.0001, 2, order=5) d_3 = der(f, point, 0.0001, 3, order=5) print("pochodna 1 rzędu= {0},\npochodna 2 rzędu= {1},\npochodna 3 rzędu= {2}".format(d_1,d_2,d_3))
import numpy as np from scipy.misc import derivative as der from scipy.interpolate import lagrange as lag x = [0.0, 0.1, 0.2, 0.3, 0.4] f_x = [0.0, 0.078348, 0.13891, 0.192916, 0.244981] poly = lag(x, f_x) deriv = der(poly, 0.2, 0.0001, 1, order=5) print("Wartość f'(0.2)=", deriv)
import numpy as np from scipy.misc import derivative as der from matplotlib import pyplot as pp def arcsin2(x): return np.arcsin(x) ** 2 x = np.linspace(-1, 1, 200) pp.subplot(121) pp.plot(x, arcsin2(x)) pp.plot(x, der(arcsin2, x, dx=0.01, n=2), 'black') pp.plot(x, der(arcsin2, x, dx=0.01, n=4, order=5), 'y') pp.plot(x, der(arcsin2, x, dx=0.01, n=6, order=7), 'g') pp.plot(x, der(arcsin2, x, dx=0.01, n=8, order=9), 'r') pp.ylim(-10, 20000) pp.subplot(122) pp.plot(x, der(arcsin2, x, dx=0.01)) pp.plot(x, der(arcsin2, x, dx=0.01, n=3, order=5), 'black') pp.plot(x, der(arcsin2, x, dx=0.01, n=5, order=7), 'y') pp.plot(x, der(arcsin2, x, dx=0.01, n=7, order=9), 'g') pp.plot(x, der(arcsin2, x, dx=0.01, n=9, order=11), 'r') pp.ylim(-20, 20) pp.show()
def derivative(x, y): """Compute a derivative""" f = interpolate(x, y) # interpolate from scipy.misc import derivative as der return der(f, x, dx=1e-2) # compute the derivative