Escolar Documentos
Profissional Documentos
Cultura Documentos
/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 23 03:21:39 2021
import math
# https://docs.python.org/3/library/math.html
import numpy as np
import matplotlib.pyplot as plt
# https://matplotlib.org/stable/index.html
L = dominio[-1] - dominio[0]
maxim, minim = max(img), min(img)
H = maxim - minim
# eixo das abscissas
plt.plot([dominio[0] - 0.05*L, dominio[-1] + 0.05*L],
[minim - 0.025*H, minim - 0.025*H],
'k-', lw=0.5)
def any2vector(objeto):
'''
Transforma lista em vetor, matriz linha em vetor, e matriz coluna
em vetor
'''
if isinstance(objeto, list):
objeto_aux = np.array(objeto, dtype=float)
elif isinstance(objeto, type(np.array([1]))):
# Matrix 1 X 1
if np.shape(objeto) == (1, 1):
objeto_aux = objeto[0]
# Matriz linha
elif np.shape(objeto)[0] == 1:
objeto_aux = objeto[0]
# Vetor
elif len(np.shape(objeto)) == 1:
objeto_aux = objeto
# Matriz coluna
elif np.shape(objeto)[1] == 1:
objeto_aux = (objeto.T)[0]
# Objeto qualquer
elif isinstance(objeto, type(particao)):
objeto_aux = objeto
else:
print('any2vector: Objeto -> Objeto')
objeto_aux = objeto
return objeto_aux
if isinstance(funcao, type(particao)):
imf = imagem(funcao, dominio)
elif np.shape(temp1) != np.shape(temp2):
imf = False
else:
imf = temp1
return imf
######################
# RAÍZES DE EQUAÇÕES #
######################
tol = 10**(-8)
fxmin = funcao(xmin)
fxmax = funcao(xmax)
if fxmin == 0:
return [xmin]
elif fxmax == 0:
return [xmax]
elif (fxmin > 0 and fxmax > 0) or (fxmin < 0 and fxmax < 0):
print('Erro! f(xmin) e f(xmax) possuem o mesmo sinal')
return False
if (fxmei > 0 and fxmin > 0) or (fxmei < 0 and fxmin < 0):
xmin = xmeio
elif (fxmei > 0 and fxmax > 0) or (fxmei < 0 and fxmax < 0):
xmax = xmeio
return aprox
ou equivalentemente,
g(x)= x - k * funcao(x) = x, com x in dominio
tol = 10**(-8)
def g(x):
return x - k*funcao(x)
else:
print('Erro crasso!')
aprox = False
return aprox
aprox = [inicial]
if funcao(aprox[0]) == 0:
return aprox
h = 0.01
dfmei = 0.5 * (funcao(aprox[0] + h) - funcao(aprox[0] - h))/h
aprox += [aprox[0] - funcao(aprox[0])/dfmei]
num = 3
while abs(aprox[-2] - aprox[-1]) >= tol:
if num == 15:
print('Erro! A aproximação diverge')
return False
if funcao(aprox[-1]) == 0:
return aprox
num += 1
return aprox
if fx1 == 0:
aprox += [x1]
return aprox
elif fx2 == 0:
aprox += [x2]
return aprox
elif (fx1 > 0 and fx2 > 0) or (fx1 < 0 and fx2 < 0):
return 'Erro! A função não muda de sinal no intervalo
especificado'
niter = 1
while abs(aprox[-2] - aprox[-1]) >= tol and fx1 != fx2:
x3 = x2 - fx2 * (x1 - x2)/(fx1 - fx2)
fx3 = funcao(x3)
niter += 1
aprox += [x3]
if fx3 == 0:
return aprox
elif (fx3 > 0 and fx1 > 0) or (fx3 < 0 and fx1 < 0):
x1, fx1 = x3, fx3
elif (fx3 > 0 and fx2 > 0) or (fx3 < 0 and fx2 < 0):
x2, fx2 = x3, fx3
return aprox
if fx1 == 0:
aprox += [x1]
return aprox
elif fx2 == 0:
aprox += [x2]
return aprox
numiter = 3
while abs(aprox[-2] - aprox[-1]) >= tol and fx1 != fx2:
x3 = x2 - fx2 * (x1 - x2)/(fx1 - fx2)
if funcao(x3) == 0:
aprox += [x3]
return aprox
aprox += [x3]
'''
if (x3 > x1 and x1 > x2) or (x3 < x1 and x1 < x2):
plt.plot([x2, x3], [fx2, 0], 'r:', lw=0.5)
elif (x3 > x2 and x2 > x1) or (x3 < x2 and x2 < x1):
plt.plot([x1, x3], [fx1, 0], 'r:', lw=0.5)
elif (x3 < abs(x1 - x2)):
plt.plot([x1, x2], [fx1, fx2], 'r:', lw=0.5)
plt.plot([x3, x3], [0, funcao(x3)], 'r:', lw=0.5)
plt.plot([x1, x2], [fx1, fx2], 'o', color='orange',
markersize=2)
print('iteracao =', numiter)
'''
x1, fx1 = x2, fx2
x2, fx2 = x3, funcao(x3)
return aprox
#############
# DERIVAÇÃO #
#############
dominio = any2vector(dominio)
h = dominio[1] - dominio[0]
df3p = np.empty((len(dominio), ), dtype=float)
if np.shape(dominio)[0] < 3:
print('Não há pontos suficientes no domínio')
df3p = False
else:
df3p[0] = 0.5 * (- 3 * imf[0] + 4 * imf[1] - imf[2]) / h
for i in range(1, len(dominio) - 1):
df3p[i] = 0.5 * (imf[i + 1] - imf[i - 1]) / h
df3p[len(dominio) - 1] = 0.5*(imf[len(dominio) - 3]
- 4*imf[len(dominio) - 2]
+ 3*imf[len(dominio) - 1]) / h
return df3p
if np.shape(dominio)[0] < 5:
print('Não há pontos suficientes no domínio')
df5p = False
else:
df5p[0] = 0.25 * (- 25 * imf[0] + 48 * imf[1] - 36 * imf[2]
+ 16 * imf[3] - 3 * imf[4]) / (3*h)
df5p[1] = 0.25 * (- 3 * imf[0] - 10 * imf[1] + 18 * imf[2]
- 6 * imf[3] + imf[4]) / (3*h)
for i in range(2, len(dominio) - 2):
df5p[i] = (imf[i - 2] - 8 * imf[i - 1] + 8 * imf[i + 1]
- imf[i + 2]) / (12 * h)
df5p[-2] = 0.25 * (3 * imf[-1] + 10 * imf[-2] - 18 * imf[-3]
+ 6 * imf[-4] - imf[-5]) / (3*h)
df5p[-1] = 0.25 * (25 * imf[-1] - 48 * imf[-2] + 36 * imf[-3]
- 16 * imf[-4] + 3 * imf[-5]) / (3*h)
return df5p
##############
# INTEGRAÇÃO #
##############