def in3(number): t = sp.symbols('t') if number == 1: p0 = 1.5 # The interval, it's suppose that a < b f = t**2 - 3 # The function tol = 1e-5 # The max error acceptable nmax = 10 # Max number iteractions elif number == 2: p0 = 3 # The interval, it's suppose that a < b f = t**2 - 2 * t - 4 # The function tol = 1e-5 # The max error acceptable nmax = 10 # Max number iteractions elif number == 3: p0 = 1.5 # The interval, it's suppose that a < b f = t**3 + 4 * t**2 - 10 # The function tol = 1e-5 # The max error acceptable nmax = 10 # Max number iteractions f_ = sp.diff(f, t) # The function f' derivative of f # The begin to start the calculations feval = sp.lambdify(t, f, "numpy") flatex = "$f(t) = " + aux.toLaTeX(f) f = aux.Funcao(feval, flatex) f_eval = sp.lambdify(t, f_, "numpy") f_latex = "$f'(t) = " + aux.toLaTeX(f_) f_ = aux.Funcao(f_eval, f_latex) return p0, f, f_, tol, nmax
def in2(number): f, t, x = master(number) f_ = sp.diff(f, t) feval = sp.lambdify(t, f, "numpy") # Transform the function to lambdify flatex = "$f(t) = " + aux.toLaTeX(f) f_eval = sp.lambdify(t, f_, "numpy") # Transform the function to lambdify f_latex = "$f'(t) = " + aux.toLaTeX(f_) f = aux.Funcao(feval, flatex) f_ = aux.Funcao(f_eval, f_latex) y = f.e(x) y_ = f_.e(x) return x, y, y_, f, f_
def in1(number): f, t, x = master(number) feval = sp.lambdify(t, f, "numpy") # Transform the function to lambdify flatex = "$f(t) = " + aux.toLaTeX(f) f = aux.Funcao(feval, flatex) y = f.e(x) return x, y, f # x e y são arrays, que indicam os pontos
def in10(number): f, t, x = master(number) f_ = sp.diff(f, t) f_ = sp.lambdify(t, f_, "numpy") FP = np.array((f_(x[0]), f_(x[-1]))) feval = sp.lambdify(t, f, "numpy") # Transform the function to lambdify flatex = "$f(t) = " + aux.toLaTeX(f) f = aux.Funcao(feval, flatex) y = f.e(x) return x, y, f, FP
def Spline_cubico(x, y): # A funcao ja retorna no modo lambdify n = len(x) - 1 h = np.zeros(n + 1) al = np.zeros(n + 1) # alpha l = np.zeros(n + 1) mu = np.zeros(n + 1) z = np.zeros(n + 1) a = y b = np.zeros(n + 1) c = np.zeros(n + 1) d = np.zeros(n + 1) for i in range(n): h[i] = x[i + 1] - x[i] for i in range(1, n): al[i] = (3 / h[i]) * (a[i + 1] - a[i]) - (3 / h[i - 1]) * (a[i] - a[i - 1]) l[0] = 1 mu[0] = 0 z[0] = 0 for i in range(1, n): l[i] = 2 * (x[i + 1] - x[i - 1]) - h[i - 1] * mu[i - 1] mu[i] = h[i] / l[i] z[i] = (al[i] - h[i - 1] * z[i - 1]) / l[i] l[n] = 1 mu[n] = 0 z[n] = 0 for j in range(n - 1, -1, -1): c[j] = z[j] - mu[j] * c[j + 1] b[j] = (a[j + 1] - a[j]) / h[j] - h[j] * (c[j + 1] + 2 * c[j]) / 3 d[j] = (c[j + 1] - c[j]) / (3 * h[j]) #return a, b, c, d t = sp.symbols('t') retorno = [] for j in range(n): f = a[j] + b[j] * (t - x[j]) + c[j] * (t - x[j])**2 + d[j] * (t - x[j])**3 f = sp.lambdify(t, f, "numpy") retorno.append((x[j], x[j + 1], f)) return aux.Funcao(lambda t: Separavel(retorno, t), "$S(t)$")
def in2(number): t = sp.symbols('t') if 1 <= number <= 5: p0 = 1.5 # The initial aproximation f = t**3 + 4 * t**2 - 10 # The function that we want to calculate the roots tol = 1e-5 # The max error acceptable nmax = 10 # Max number iteractions if number == 1: g = t - t**3 - 4 * t**2 + 10 # It doesn't converg elif number == 2: g = sp.sqrt(4 * t - 10 / t) # It doesn't converg elif number == 3: g = sp.sqrt( 10 - t**3 ) / 2 # The fixed point function, using f(x) = 0 we can get x = sqrt(10-x**3)/2 elif number == 4: g = sp.sqrt(10 / (4 + t)) # It converges quite well elif number == 5: g = t - (t**3 + 4 * t**2 - 10) / ( 3 * t**2 + 8 * t ) # It converges very well, we will see this funcion in the Newton's method g_ = sp.diff(g, t) # The function g' derivative of g feval = sp.lambdify(t, f, "numpy") flatex = "$f(t) = " + aux.toLaTeX(f) f = aux.Funcao(feval, flatex) geval = sp.lambdify(t, g, "numpy") glatex = "$g(t) = " + aux.toLaTeX(g) g = aux.Funcao(geval, glatex) g_eval = sp.lambdify(t, g_, "numpy") g_latex = "$g'(t) = " + aux.toLaTeX(g_) g_ = aux.Funcao(g_eval, g_latex) return p0, f, g, g_, tol, nmax
def Lagrange(x, y): # The variables t = sp.symbols('t') # The calculations n = len(x) L = [] for i in range(n): Li = 1 for j in range(n): if i != j: Li *= (t-x[j])/(x[i]-x[j]) L.append(Li) g = 0 for i in range(n): g += L[i] * y[i] g = sp.simplify(g) L = aux.Funcao(sp.lambdify(t, g, "numpy"), '$L(t) = ' + aux.toLaTeX(g)) return L
def Const(x, y): n = len(x) - 1 a = y #print('n = ' + str(n)) t = sp.symbols( 't') # Nao é utilizado, mas é colocado para que funcione com Separavel retorno = [] for j in range(n): f = a[j] - t * 10**(-6) f = a[j] + (t - 1)**2 - t**2 + 2 * t - 1 f = sp.lambdify(t, f, "numpy") retorno.append((x[j], x[j + 1], f)) #print('[a, b] = [' + str(x[j]) + ', ' + str(x[j+1]) + ']') #print(f(1)) for i in range(n): a, b, f = retorno[i] print(f(1)) return aux.Funcao(lambda t: Separavel(retorno, t), "$S(t)$")
def Simpson(x, y): # Aqui supomos que len(y) seja impar: # len(y) = len(x) # len(x) % 2 = 1 t = sp.symbols('t') retorno = [] n = (len(y) - 1) // 2 # Indicando que ha n funcoes for i in range(n): X = np.array([x[2 * i], x[2 * i + 1], x[2 * i + 2]]) Y = np.array([y[2 * i], y[2 * i + 1], y[2 * i + 2]]) t = sp.symbols('t') f = 0 f += (t - X[0]) * (t - X[1]) * Y[2] / ((X[2] - X[0]) * (X[2] - X[1])) f += (t - X[1]) * (t - X[2]) * Y[0] / ((X[0] - X[1]) * (X[0] - X[2])) f += (t - X[2]) * (t - X[0]) * Y[1] / ((X[1] - X[2]) * (X[1] - X[0])) f = sp.lambdify(t, f, "numpy") retorno.append((x[2 * i], x[2 * i + 2], f)) return aux.Funcao(lambda t: aux.Separavel(retorno, t), "$S(t)$")
def Hermite(x, y, y_): # The variables t = sp.symbols('t') # The calculations n = len(x) - 1 Q = np.zeros((2 * (n + 1), 2 * (n + 1))) Qs = np.zeros(2 * (n + 1)) z = np.zeros(2 * (n + 1)) for i in range(n + 1): z[2 * i] = x[i] z[2 * i + 1] = x[i] Q[2 * i][0] = y[i] Q[2 * i + 1][0] = y[i] Q[2 * i + 1][1] = y_[i] if i != 0: Q[2 * i][1] = (Q[2 * i][0] - Q[2 * i - 1][0]) / (z[2 * i] - z[2 * i - 1]) #L.append(Li) for i in range(2, 2 * (n + 1)): for j in range(2, i + 1): Q[i][j] = (Q[i][j - 1] - Q[i - 1][j - 1]) / (z[i] - z[i - j]) for i in range(2 * (n + 1)): Qs[i] = Q[i][i] multi = 1 f = Qs[0] for i in range(n): multi *= t - x[i] f += Qs[2 * i + 1] * multi multi *= t - x[i] f += Qs[2 * i + 2] * multi multi *= t - x[-1] f += Qs[2 * n + 1] * multi f = sp.simplify(f) L = aux.Funcao(sp.lambdify(t, f, "numpy"), '$L(t) = ') # + aux.toLaTeX(f)) return L
def Simpson(x, y): # Aqui supomos que len(y) seja impar: # len(y) = len(x) # len(x) % 2 = 1 t = sp.symbols('t') retorno = [] #print('len(y) = ' + str(len(y))) n = (len(y) - 1) // 2 # Indicando que ha n funcoes #print('n = ' + str(n)) for i in range(n): X = np.array([x[2 * i], x[2 * i + 1], x[2 * i + 2]]) Y = np.array([y[2 * i], y[2 * i + 1], y[2 * i + 2]]) f = Lagrange(X, Y) #print(type(f.e)) #print(type(f(x[2*i]))) #print(f(x[2*i])) #print('122') retorno.append((x[2 * i], x[2 * i + 2], f.e)) return aux.Funcao(lambda t: Separavel(retorno, t), "$S(t)$")
def in1(number): t = sp.symbols('t') if number == 1: a, b = 1, 2 # The interval, it's suppose that a < b f = t**2 - 3 # The function tol = 1e-5 # The max error acceptable nmax = 10 # Max number iteractions elif number == 2: a, b = 2, 4 # The interval, it's suppose that a < b f = t**2 - 2 * t - 4 # The function tol = 1e-5 # The max error acceptable nmax = 10 # Max number iteractions elif number == 3: a, b = 1, 2 # The interval, it's suppose that a < b f = t**3 + 4 * t**2 - 10 # The function tol = 1e-5 # The max error acceptable nmax = 10 # Max number iteractions # The begin to start the calculations feval = sp.lambdify(t, f, "numpy") flatex = "$f(t) = " + aux.toLaTeX(f) f = aux.Funcao(feval, flatex) return a, b, f, tol, nmax
def Linear(x, y): # A funcao ja retorna no modo lambdify n = len(x) - 1 a = y b = np.zeros(n + 1) h = np.zeros(n + 1) for i in range(n): h[i] = x[i + 1] - x[i] b[i] = (a[i + 1] - a[i]) / h[i] #return a, b t = sp.symbols('t') retorno = [] for j in range(n): f = a[j] + b[j] * (t - x[j]) f = sp.lambdify(t, f, "numpy") retorno.append((x[j], x[j + 1], f)) return aux.Funcao(lambda t: aux.Separavel(retorno, t), "$S(t)$")
def in1(number): f, t, a, b, n = master(number) feval = sp.lambdify(t, f, "numpy") flatex = "$f(t) = " + aux.toLaTeX(f) f = aux.Funcao(feval, flatex) return a, b, n, f