·

Engenharia Elétrica ·

Cálculo Numérico

· 2023/1

Envie sua pergunta para a IA e receba a resposta na hora

Fazer Pergunta

Texto de pré-visualização

Universidade Federal Rural de Pernambuco - UFRPE UACSA Disciplina: C´alculo Num´erico Professor: Marcelo Flamarion Lista 4 1. Problemas: 1, 2, 3, 4, 5, 6 da se¸c˜ao 4.3. 2. Problemas: 1, 2, 3, 4, 5, 6, 11, 12, 13, 15, 16 da se¸c˜ao 4.4. Referˆencias [1] BURDEN, Richard L.; FAIRES, J D.; BURDEN, Annette M. An´alise Num´erica - Tradu¸c˜ao da 10a edi¸c˜ao norte-americana. Cengage Learning Brasil, 2016. Seção 3.1 1) a) O código utilizado para polinômio de grau 2 é: # interpolação grau 2 CODIGO 1 import math import numpy as np def f(x): #definindo a função f(x) return math.cos(x) def elimgauss(A,b): #algoritmo da eliminação gaussiana s = len(b) - 1 c = [0]*(s+1) for k in range (0,s,1): # transforma a matriz A em triangular superior for i in range (k+1,s+1,1): m = A[i][k]/A[k][k] A[i][k] = 0 for j in range (k+1,s+1,1): A[i][j] = A[i][j] - m*A[k][j] b[i] = b[i] - m*b[k] c[s] = b[s]/A[s][s] for i in range (s-1,-1,-1): # laço para calcular os outros coeficientes soma_aux = 0 for j in range (i+1,s+1,1): soma_aux = soma_aux + A[i][j]*c[j] c[i] = (b[i] - soma_aux)/(A[i][i]) return c x = [0.6,0.9,0] #definindo o vetor x m = len(x) y = [0]*m xn = 0.45 b = [0]*m for i in range (0,m,1): #definindo o vetor y y[i] = f(x[i]) b[i] = f(x[i]) A = np.zeros((m,m)) P = [0]*m erro = [0]*m A[0][0] = x[0]**2 A[1][0] = x[1]**2 A[2][0] = x[2]**2 A[0][1] = x[0] A[1][1] = x[1] A[2][1] = x[2] A[0][2] = 1 A[1][2] = 1 A[2][2] = 1 a = elimgauss(A,b) for i in range (0,m,1): P[i] = a[2] + a[1]*x[i] + a[0]*(x[i]**2) erro[i] = abs(y[i] - P[i]) Pn = a[0] + a[1]*xn + a[2]*(xn**2) er = max(erro) print("Os coeficientes são", a) print("O valor do polinomio em x =", xn, ": P =",Pn ) print("O maior erro absoluto é ", er) O resultado é: b) Alterando apenas a função f(x) do código anterior: c) d) Para função do primeiro grau, o código, usando os dois pontos mais próximos de 0,45, é: a) # interpolação grau 1 CODIGO 2 import math def f(x): #definindo a função f(x) return math.cos(x) x = [0, 0.6] #definindo o vetor x m = len(x) y = [0]*m xn = 0.45 for i in range (0,m,1): #definindo o vetor y y[i] = f(x[i]) a = (y[0]-y[1])/(x[0]-x[1]) b = y[1] - a*x[1] P = [0]*m erro = [0]*m for i in range (0,m,1): P[i] = a*x[i] + b erro[i] = abs(y[i] - P[i]) Pn = a*xn + b er = max(erro) print("Os coeficientes são: a = ", a,", b = ", b) print("O valor do polinomio em x =", xn, ": P =",Pn ) print("O maior erro absoluto é ", er) O resultado é: b) Mudando apenas a função f(x): c) d) 2. Do código 3, que é uma modificação da 1 (mudando o vetor x): # interpolação grau 2 CODIGO 3 import math import numpy as np def f(x): #definindo a função f(x) return math.sin(math.pi*x) def elimgauss(A,b): #algoritmo da eliminação gaussiana s = len(b) - 1 c = [0]*(s+1) for k in range (0,s,1): # transforma a matriz A em triangular superior for i in range (k+1,s+1,1): m = A[i][k]/A[k][k] A[i][k] = 0 for j in range (k+1,s+1,1): A[i][j] = A[i][j] - m*A[k][j] b[i] = b[i] - m*b[k] c[s] = b[s]/A[s][s] for i in range (s-1,-1,-1): # laço para calcular os outros coeficientes soma_aux = 0 for j in range (i+1,s+1,1): soma_aux = soma_aux + A[i][j]*c[j] c[i] = (b[i] - soma_aux)/(A[i][i]) return c x = [1,1.25,1.6] #definindo o vetor x m = len(x) y = [0]*m xn = 1.4 b = [0]*m for i in range (0,m,1): #definindo o vetor y y[i] = f(x[i]) b[i] = f(x[i]) A = np.zeros((m,m)) P = [0]*m erro = [0]*m A[0][0] = x[0]**2 A[1][0] = x[1]**2 A[2][0] = x[2]**2 A[0][1] = x[0] A[1][1] = x[1] A[2][1] = x[2] A[0][2] = 1 A[1][2] = 1 A[2][2] = 1 a = elimgauss(A,b) for i in range (0,m,1): P[i] = a[2] + a[1]*x[i] + a[0]*(x[i]**2) erro[i] = abs(y[i] - P[i]) Pn = a[0] + a[1]*xn + a[2]*(xn**2) er = max(erro) print("Os coeficientes são", a) print("O valor do polinomio em x =", xn, ": P =",Pn ) print("O maior erro absoluto é ", er) O resultado é: b) c) d) Para polinômio de grau, modificando o vetor x do código 2, temos o código 4 abaixo: a) # interpolação grau 1 CODIGO 4 import math def f(x): #definindo a função f(x) return math.sin(math.pi*x) x = [1.25, 1.6] #definindo o vetor x m = len(x) y = [0]*m xn = 1.4 for i in range (0,m,1): #definindo o vetor y y[i] = f(x[i]) a = (y[0]-y[1])/(x[0]-x[1]) b = y[1] - a*x[1] P = [0]*m erro = [0]*m for i in range (0,m,1): P[i] = a*x[i] + b erro[i] = abs(y[i] - P[i]) Pn = a*xn + b er = max(erro) print("Os coeficientes são: a = ", a,", b = ", b) print("O valor do polinomio em x =", xn, ": P =",Pn ) print("O maior erro absoluto é ", er) O resultado é: b) c) d) 14. Meu código não estava funcionando corretamente, então segue a resolução manual: a) Determinando os pontos para f(x) com 4 casas decimais: n 0 1 2 x 0 Pi/6 Pi/4 f(x) = 𝑒−2𝑥 ∗ 𝑠𝑒𝑛(3𝑥) 0 0,3509 0,147 O polinômio de Lagrange é dado por: 𝑝2(𝑥) = 𝐿0(𝑥)𝑦0 + 𝐿1(𝑥)𝑦1 + 𝐿2(𝑥)𝑦2 𝑦0𝐿0(𝑥) = 𝑦0 (𝑥 − 𝑥1)(𝑥 − 𝑥2) (𝑥0 − 𝑥1)(𝑥0 − 𝑥2) = 0 ∗ (𝑥 − 𝜋 6) (𝑥 − 𝜋 4) (0 − 𝜋 6) (𝑥0 − 𝜋 4) = 0 𝑦1𝐿1(𝑥) = 𝑦1 (𝑥 − 𝑥0)(𝑥 − 𝑥2) (𝑥1 − 𝑥0)(𝑥1 − 𝑥2) = 0,3509 ∗ (𝑥 − 0) (𝑥 − 𝜋 4) (𝜋 6 − 0) (𝜋 6 − 𝜋 4) = −2,5598(𝑥2 − 0,7854𝑥) 𝑦2𝐿2(𝑥) = 𝑦2 (𝑥 − 𝑥0)(𝑥 − 𝑥1) (𝑥2 − 𝑥0)(𝑥2 − 𝑥1) = 0,147 ∗ (𝑥 − 0) (𝑥 − 𝜋 6) (𝜋 4 − 0)(𝜋 4 − 𝜋 6) = 0,7149(𝑥2 − 0,5236𝑥) 𝑝2(𝑥) = 𝐿0(𝑥)𝑦0 + 𝐿1(𝑥)𝑦1 + 𝐿2(𝑥)𝑦2 = −2,5598(𝑥2 − 0,7854𝑥) + 0,7149(𝑥2 − 0,5236𝑥) 𝑝2(𝑥) = −1,8449𝑥2 + 1,6362𝑥 O erro para n+1 = 3 pontos é: 𝑒 = 𝑓3(𝜉) 3! (𝑥 − 𝑥0)(𝑥 − 𝑥1)(𝑥 − 𝑥2) ; 𝑓(𝑥) = 𝑒−2𝑥 ∗ 𝑠𝑒𝑛(3𝑥) ; 𝑓3 = 𝑒−2𝑥(46𝑠𝑒𝑛(3𝑥) + 9 cos(3𝑥)) Derivando mais uma vez para determinar o máximo do intervalo: n 0 1 2 x 0 Pi/6 Pi/4 d3/ dx3 (f(x)) 9 16,1423 5,4387 Derivando a função mais uma vez e igualando a zero, temos: 𝑓4 = 𝑒−2𝑥(120 cos(3𝑥) − 119𝑠𝑒𝑛(3𝑥)) = 0 → 120 cos(3𝑥) = 119𝑠𝑒𝑛(3𝑥) → 120 119 = 𝑡𝑔(3𝑥) → 𝑥 = 0,2632 𝑓3(0,2632) = 23,0387 𝑒 = 23,0387 6 (𝑥 − 0) (𝑥 − 𝜋 6) (𝑥 − 𝜋 6) = 3,8398(𝑥3 − 1,309𝑥2 + 0,4112𝑥) b) Determinando os pontos para f(x) com 4 casas decimais: n 0 1 2 x 3 3,2 3,5 f(x) = log10(𝑥) 0,4771 0,5051 0,5441 O polinômio de Lagrange é dado por: 𝑝2(𝑥) = 𝐿0(𝑥)𝑦0 + 𝐿1(𝑥)𝑦1 + 𝐿2(𝑥)𝑦2 𝑦0𝐿0(𝑥) = 𝑦0 (𝑥 − 𝑥1)(𝑥 − 𝑥2) (𝑥0 − 𝑥1)(𝑥0 − 𝑥2) = 0,4771 ∗ (𝑥 − 3,2)(𝑥 − 3,5) (3 − 3,2)(3 − 3,5) = 4,771(𝑥2 − 6,7𝑥 + 11,2) 𝑦1𝐿1(𝑥) = 𝑦1 (𝑥 − 𝑥0)(𝑥 − 𝑥2) (𝑥1 − 𝑥0)(𝑥1 − 𝑥2) = 0,5051 ∗ (𝑥 − 3)(𝑥 − 3,5) (3,2 − 3)(3,2 − 3,5) = −8,4183(𝑥2 − 6,5𝑥 + 10,5) 𝑦2𝐿2(𝑥) = 𝑦2 (𝑥 − 𝑥0)(𝑥 − 𝑥1) (𝑥2 − 𝑥0)(𝑥2 − 𝑥1) = 0,5441 ∗ (𝑥 − 3)(𝑥 − 3,2) (3,5 − 3)(3,5 − 3,2) = 3,6273(𝑥2 − 6,2𝑥 + 9,6) 𝑝2(𝑥) = 4,771(𝑥2 − 6,7𝑥 + 11,2) − 8,4183(𝑥2 − 6,5𝑥 + 10,5) + 3,6273(𝑥2 − 6,2𝑥 + 9,6) 𝑝2(𝑥) = −0,02𝑥2 + 0,264𝑥 − 0,1349 O erro para n+1 = 3 pontos é: 𝑒 = 𝑓3(𝜉) 3! (𝑥 − 𝑥0)(𝑥 − 𝑥1)(𝑥 − 𝑥2) ; 𝑓(𝑥) = log10(𝑥) ; 𝑓′ = 1 𝑥 log(10) = 1 log(10) 𝑥−1 𝑓′′ = − 1 log(10) 𝑥−2 ; 𝑓3 = 2 log(10) ∗ 𝑥−3 O maior valor da derivada a terceira é quando x se aproxima de zero, portanto: 𝑓3(𝜉) = 𝑓3(3) = 2 log(10) ∗ 33 = 0,0322 𝑒 = 0,0322 6 (𝑥 − 3)(𝑥 − 3,2)(𝑥 − 3,5) = 0,0054(𝑥3 − 9,7𝑥2 + 31,3𝑥 − 33,6) c) Determinando os pontos para f(x) com 4 casas decimais: n 0 1 2 x -0,3 0 0,3 f(x) = 𝑒𝑥 + 𝑒−𝑥 2,0907 2 2,0907 O polinômio de Lagrange é dado por: 𝑝2(𝑥) = 𝐿0(𝑥)𝑦0 + 𝐿1(𝑥)𝑦1 + 𝐿2(𝑥)𝑦2 𝑦0𝐿0(𝑥) = 𝑦0 (𝑥 − 𝑥1)(𝑥 − 𝑥2) (𝑥0 − 𝑥1)(𝑥0 − 𝑥2) = 2,0907 ∗ (𝑥 − 0)(𝑥 − 0,3) (−0,3 − 0)(−0,3 − 0,3) = 11,615(𝑥2 − 0,3𝑥) 𝑦1𝐿1(𝑥) = 𝑦1 (𝑥 − 𝑥0)(𝑥 − 𝑥2) (𝑥1 − 𝑥0)(𝑥1 − 𝑥2) = 2 ∗ (𝑥 + 0,3)(𝑥 − 0,3) (0 + 0,3)(0 − 0,3) = −22,2222(𝑥2 − 0,09) 𝑦2𝐿2(𝑥) = 𝑦2 (𝑥 − 𝑥0)(𝑥 − 𝑥1) (𝑥2 − 𝑥0)(𝑥2 − 𝑥1) = 2,0907 ∗ (𝑥 + 0,3)(𝑥 − 0) (0 + 0,3)(0,3 − 0) = 11,615(𝑥2 + 0,3𝑥) 𝑝2(𝑥) = 11,615(𝑥2 − 0,3𝑥) − 22,2222(𝑥2 − 0,09) + 11,615(𝑥2 + 0,3𝑥) 𝑝2(𝑥) = 1,0078𝑥2 + 2 O erro para n+1 = 3 pontos é: 𝑒 = 𝑓3(𝜉) 3! (𝑥 − 𝑥0)(𝑥 − 𝑥1)(𝑥 − 𝑥2) ; 𝑓(𝑥) = 𝑒𝑥 + 𝑒−𝑥 ; 𝑓3 = 𝑒𝑥 − 𝑒−𝑥 O maior valor da derivada a terceira é quando x tem o maior módulo, então: 𝑓3(𝜉) = 𝑓3(0,3) = 0,609 𝑒 = 0,609 6 (𝑥 + 0,3)(𝑥 − 0)(𝑥 − 0,3) = 0,1015(𝑥3 − 0,09𝑥) d) Determinando os pontos para f(x) com 4 casas decimais: n 0 1 2 3 x 0,001 0,3 0,5 1 f(x) = 𝑐𝑜𝑠(2 ∗ ln(3𝑥)) 0,5832 0,9779 0,6888 -0,5863 O polinômio de Lagrange é dado por: 𝑝2(𝑥) = 𝐿0(𝑥)𝑦0 + 𝐿1(𝑥)𝑦1 + 𝐿2(𝑥)𝑦2 + 𝐿3(𝑥)𝑦3 𝑦0𝐿0(𝑥) = 𝑦0 (𝑥 − 𝑥1)(𝑥 − 𝑥2)(𝑥 − 𝑥3) (𝑥0 − 𝑥1)(𝑥0 − 𝑥2)(𝑥0 − 𝑥3) = 0,5832 ∗ (𝑥 − 0,3)(𝑥 − 0,5)(𝑥 − 1) (0,001 − 0,3)(0,001 − 0,5)(0,001 − 1) = −3,9122(𝑥3 − 1,8𝑥2 + 0,95𝑥 − 0,15) 𝑦1𝐿1(𝑥) = 𝑦1 (𝑥 − 𝑥0)(𝑥 − 𝑥2)(𝑥 − 𝑥3) (𝑥1 − 𝑥0)(𝑥1 − 𝑥2)(𝑥1 − 𝑥3) = 0,9779 ∗ (𝑥 − 0,001)(𝑥 − 0,5)(𝑥 − 1) (0,3 − 0,001)(0,3 − 0,5)(0,3 − 1) = 23,3612(𝑥3 − 1,501𝑥2 + 0,5015𝑥) 𝑦2𝐿2(𝑥) = 𝑦2 (𝑥 − 𝑥0)(𝑥 − 𝑥1)(𝑥 − 𝑥3) (𝑥2 − 𝑥0)(𝑥2 − 𝑥1)(𝑥2 − 𝑥3) = 0,6888 ∗ (𝑥 − 0,001)(𝑥 − 0,3)(𝑥 − 1) (0,5 − 0,001)(0,5 − 0,3)(0,5 − 1) = −13,8036(𝑥3 − 1,301𝑥2 + 0,3013𝑥) 𝑦3𝐿3(𝑥) = 𝑦3 (𝑥 − 𝑥0)(𝑥 − 𝑥1)(𝑥 − 𝑥2) (𝑥3 − 𝑥0)(𝑥3 − 𝑥1)(𝑥3 − 𝑥2) = −0,5863 ∗ (𝑥 − 0,001)(𝑥 − 0,3)(𝑥 − 0,5) (1 − 0,001)(1 − 0,3)(1 − 0,5) = −1,6768(𝑥3 − 0,801𝑥2 + 0,1508𝑥) Somando todos os termos: 𝑝2(𝑥) = 3,9686𝑥3 − 8,7216𝑥2 + 3,5872𝑥 + 0,5795 O erro para n+1 = 4 pontos é: 𝑒 = 𝑓4(𝜉) 4! (𝑥 − 𝑥0)(𝑥 − 𝑥1)(𝑥 − 𝑥2)(𝑥 − 𝑥3) ; 𝑓(𝑥) = 𝑐𝑜𝑠(2 ∗ ln(3𝑥)) 𝑓4 = 4[9𝑠𝑒𝑛(2 ∗ ln(3𝑥)) + 7 cos(2 ∗ 𝑙𝑛(3𝑥) )] 𝑥4 O maior valor da derivada a quarta é quando x se aproxima de zero e a derivada tende a infinito. Portanto o erro tende a infinito quando se aproxima do ponto x = 0. 15. Do código 3 alterando o vetor x e a função: a) b) c) d) Ambas as interpolações retornaram valor de erro absoluto nulos portanto para este caso em específico ambos são adequados. Seção 3.3 7. a) Do código abaixo, têm-se o seguinte polinômio: b) Adicionando mais um ponto: 8. a) b) 9. a) OBS: o código vale apenas para tabelas com 4 pontos b) Novo código c) novo código 16. 𝑓[𝑥0, 𝑥1, 𝑥2] = 𝑓[𝑥0, 𝑥1] − 𝑓[𝑥1, 𝑥2] 𝑥0 − 𝑥1 → 50 7 = 𝑓[𝑥0, 𝑥1] − 10 0 − 0.4 → 𝑓[𝑥0, 𝑥1] = 50 7 𝑓[𝑥1, 𝑥2] = 𝑓[𝑥1] − 𝑓[𝑥2] 𝑥1 − 𝑥2 → 10 = 𝑓[𝑥1] − 6 0,4 − 0,7 → 𝑓[𝑥1] = 3 𝑓[𝑥0, 𝑥1] = 𝑓[𝑥0] − 𝑓[𝑥1] 𝑥0 − 𝑥1 → 50 7 = 𝑓[𝑥0] − 3 0 − 0,4 → 𝑓[𝑥0] = 1 7 Seção 3.5 1. O código é: #spline cubico natural x = [0,1,2] y = [0,1,2] n = len(x) h = [0]*n alfa = [0]*n a = y for i in range (0,n-1,1): h[i] = x[i+1] - x[i] for i in range (1,n-1,1): alfa[i] = (3/h[i])*(a[i+1]-a[i]) -(3/h[i-1])*(a[i] - a[i-1]) l = [1]*n z = [0]*n mi = [0]*n c = [0]*n b = [0]*n d = [0]*n for i in range (1,n-1,1): l[i] = 2*(x[i+1] - x[i-1])-h[i-1]*mi[i-1] mi[i] = h[i]/l[i] z[i] = (alfa[i] - h[i-1]*z[i-1])/l[i] l[n-1] = 1 z[n-1] = 0 mi[n-1] = 0 for j in range (n-2,-1,-1): c[j] = z[j] - mi[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]) print("Os coeficientes são: ") print("a = ",a) print("b = ",b) print("c = ",c) print("d = ",d) O resultado é: 𝑆(𝑥) = {0 + 1 ∗ (𝑥 − 0) + 0 ∗ (𝑥 − 0)2 + 0 ∗ (𝑥 − 0)3, 0 ≤ 𝑥 < 1 1 + 1 ∗ (𝑥 − 1) + 0 ∗ (𝑥 − 1)2 + 0 ∗ (𝑥 − 1)3, 1 ≤ 𝑥 ≤ 2 𝑆(𝑥) = { 𝑥, 0 ≤ 𝑥 < 1 (𝑥 − 1), 1 ≤ 𝑥 ≤ 2 3. a) 𝑆(𝑥) = {17,56492 + 3,1341(𝑥 − 8,3), 8,3 ≤ 𝑥 ≤ 8,6 b) 𝑆(𝑥) = {0,2236 + 2,1723(𝑥 − 0,8), 0,8 ≤ 𝑥 ≤ 1 c) 𝑆(𝑥) = { −0,02475 + 1,0324(𝑥 + 0,5) + 0 ∗ (𝑥 + 0,5)2 + 6,502(𝑥 + 0,5)3, −0,5 ≤ 𝑥 < −0,25 0.3349 + 2,2515 ∗ (𝑥 + 0,25) + 4,8765 ∗ (𝑥 + 0,25)2 − 6,052 ∗ (𝑥 + 0,25)3, −0,25 ≤ 𝑥 ≤ 0 d) 𝑆(𝑥) = { −0,6205 + 3,4551(𝑥 − 0,1) − 8,996(𝑥 − 0,1)3, 0,1 ≤ 𝑥 < 0,2 −0,284 + 3,1852 ∗ (𝑥 − 0,2) − 2,6987 ∗ (𝑥 − 0,2)2 − 0,9463 ∗ (𝑥 − 0,2)3, 0,2 ≤ 𝑥 ≤ 0,3 0,0066 + 2,617(𝑥 − 0,3) − 2,9826(𝑥 − 0,3)2 + 9,9421(𝑥 − 0,3)3, 0,3 ≤ 𝑥 ≤ 0,4 11. Sabendo que: N x a = y b c d 0 0 1 2 0 -1 1 1 2 -1 -3 1 2 2 𝑏0 = 1 ℎ0 (𝑎1 − 𝑎0) − ℎ0 3 (𝑐1 + 2𝑐0) → 2 = 1 (1 − 0) (2 − 1) − 1 − 0 3 (𝑐1 + 2 ∗ 0) → 𝑐1 = −3 𝑠0 ′(1) = 𝑠1 ′(1) → 2 − 3𝑥2 = 𝑏1 + 2𝑐1(𝑥 − 1) + 3𝑑1(𝑥 − 1)2 → 𝑏1 = −1 𝑠1 ′′(2) = 0 → 2𝑐1 + 6𝑑1(𝑥 − 1) = 0 → 𝑑1 = 1 12. Sabendo que: N x a = y b c d 0 1 1 B = 1/4 0 D = 1/4 1 2 1 b = 1 -3/4 d = 1/8 2 3 (1) 𝐵 = 1 ℎ0 (𝑎1 − 𝑎0) − ℎ0 3 (𝑐1 + 2𝑐0) = 1 2 − 1 (1 − 1) − 2 − 1 3 (− 3 4 + 2 ∗ 0) = 1 4 (2) 𝑠0 ′(2) = 𝑠1 ′(2) → 𝐵 − 3𝐷(𝑥 − 1)2 = 𝑏 − 3 2 (𝑥 − 2) + 3𝑑(𝑥 − 1)2 → 1 4 − 3𝐷 = 𝑏 (3) 𝑠1 ′′(3) = 0 → − 3 2 + 6𝑑(3 − 1) = 0 → 𝑑 = 1 8 (4) 𝑑0 = 𝑐1 − 𝑐0 3ℎ0 → 𝐷 = − 3 4 − 0 3 ∗ (2 − 1) = − 1 4 = 𝐷 𝐷𝑎 (2) 1 4 − 3 (− 1 4) = 𝑏 = 1 13. Sabendo que: N x a = y b c d 0 1 0 3 2 -1 1 2 a = 4 b = 4 c = -1 d = 1/3 2 3 (1) 𝑠0 ′(2) = 𝑠1 ′(2) → 3 + 4(𝑥 − 1) − 3(𝑥 − 1)2 = 𝑏 + 2𝑐(𝑥 − 2) + 3𝑑(𝑥 − 2)2 → 3 + 4 − 3 = 𝑏 → 𝑏 = 4 (2) 𝑠0 ′′(2) = 𝑠1 ′′(2) → 4 − 6(𝑥 − 1) = 2𝑐 + 6𝑑(𝑥 − 2) → 4 − 6 = 2𝑐 → 𝑐 = −1 (3) 𝑏0 = 1 ℎ0 (𝑎1 − 𝑎0) − ℎ0 3 (𝑐1 + 2𝑐0) → 3 = 1 2 − 1 (𝑎 − 0) − 2 − 1 3 (−1 + 2 ∗ 2) 3 = 𝑎 − 1 → 𝑎 = 4 (4) 𝑠0 ′(1) = 𝑠1 ′(3) → 3 + 4(𝑥 − 1) − 3(𝑥 − 1)2 = 𝑏 + 2𝑐(𝑥 − 2) + 3𝑑(𝑥 − 2)2 3 = 𝑏 + 2𝑐 + 3𝑑 → 3 = 4 + 2 ∗ (−1) + 3𝑑 → 𝑑 = 1 3 14. Sabendo que: N x a = y b c d 0 0 1 B = 0 2 -2 1 1 1 b = -2 -4 7 2 2 (1) 𝑠0 ′(1) = 𝑠1 ′(1) → 𝐵 + 4𝑥 − 6𝑥2 = 𝑏 − 8(𝑥 − 1) + 21(𝑥 − 1)2 → 𝐵 + 4 − 6 = 𝑏 (2) 𝑠0 ′′(1) = 𝑠1 ′′(1) → 4 − 12𝑥 = −8 + 42(𝑥 − 1) → 4 − 12 = −8 → 𝑛ã𝑜 𝑓𝑜𝑖 ú𝑡𝑖𝑙 (3) 𝐵 = 1 ℎ0 (𝑎1 − 𝑎0) − ℎ0 3 (𝑐1 + 2𝑐0) = (1 − 1) − 1 3 (−4 + 2 ∗ 2) = 0 𝐷𝑒 (1): 𝑏 = −2 𝑓′(0) = 𝑠0 ′(0) = 4𝑥 − 6𝑥2 = 0 𝑓′(2) = 𝑠1 ′(2) = −2 − 8(2 − 1) + 21(2 − 1)2 = 11 Seção 8.1 3. O código para interpolação grau 1, 2 e 3 é: x = [1, 1.1, 1.3, 1.5, 1.9, 2.1] y = [1.84, 1.96, 2.21, 2.45, 2.94, 3.18] def elimgauss(A,b): s = len(b) - 1 c = [0]*(s+1) for k in range (0,s,1): # transforma a matriz A em triangular superior for i in range (k+1,s+1,1): m = A[i][k]/A[k][k] A[i][k] = 0 for j in range (k+1,s+1,1): A[i][j] = A[i][j] - m*A[k][j] b[i] = b[i] - m*b[k] c[s] = round(b[s]/A[s][s],3) for i in range (s-1,-1,-1): # laço para calcular os coeficientes soma_aux = 0 for j in range (i+1,s+1,1): soma_aux = soma_aux + A[i][j]*c[j] c[i] = round((b[i] - soma_aux)/(A[i][i]),3) return c def mmq1(x,y): #regressão polinomial grau 1 n = len(x) A = [[0,0],[0,0]] b = [0]*2 somax = 0 somay = 0 somax2 = 0 somaxy= 0 for i in range (0, n, 1): somax = somax + x[i] somax2 = somax2 + (x[i])**2 somay = somay + y[i] somaxy = somaxy + x[i]*y[i] A[0][0] = n A[0][1] = somax A[1][0] = somax A[1][1] = somax2 b[0] = somay b[1] = somaxy a = elimgauss (A,b) print("Os coeficientes são para o polinomio de grau 1 são: ", a) def mmq2(x,y): n = len(x) A = [[0,0,0],[0,0,0],[0,0,0]] b = [0]*3 somax = 0 somay = 0 somax2 = 0 somax3 = 0 somax4 = 0 somaxy= 0 somax2y = 0 A[0][0] = n for i in range (0, n, 1): somax = somax + x[i] somax2 = somax2 + (x[i])**2 somax3 = somax3 + (x[i])**3 somax4 = somax4 + (x[i])**4 somay = somay + y[i] somaxy = somaxy + x[i]*y[i] somax2y = somax2y + y[i]*((x[i])**2) A[0][1] = somax A[1][0] = somax A[1][1] = somax2 A[0][2] = somax2 A[2][0] = somax2 A[1][2] = somax3 A[2][1] = somax3 A[2][2] = somax4 b[0] = somay b[1] = somaxy b[2] = somax2y a = elimgauss (A,b) print("Os coeficientes são para o polinomio de grau 2 são: ", a) def mmq3(x,y): n = len(x) A = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] b = [0]*4 somax = 0 somay = 0 somax2 = 0 somax3 = 0 somax4 = 0 somax5 = 0 somax6 = 0 somaxy= 0 somax2y = 0 somax3y = 0 A[0][0] = n for i in range (0, n, 1): somax = somax + x[i] somax2 = somax2 + (x[i])**2 somax3 = somax3 + (x[i])**3 somax4 = somax4 + (x[i])**4 somax5 = somax5 + (x[i])**5 somax6 = somax6 + (x[i])**6 somay = somay + y[i] somaxy = somaxy + x[i]*y[i] somax2y = somax2y + y[i]*((x[i])**2) somax3y = somax3y + y[i]*((x[i])**3) A[0][1] = somax A[1][0] = somax A[1][1] = somax2 A[0][2] = somax2 A[2][0] = somax2 A[1][2] = somax3 A[2][1] = somax3 A[0][3] = somax3 A[3][0] = somax3 A[2][2] = somax4 A[1][3] = somax4 A[3][1] = somax4 A[2][3] = somax5 A[3][2] = somax5 A[3][3] = somax6 b[0] = somay b[1] = somaxy b[2] = somax2y b[3] = somax3y a = elimgauss (A,b) print("Os coeficientes são para o polinomio de grau 3 são: ", a) mmq1(x,y) mmq2(x,y) mmq3(x,y) O resultado é: { 𝑃1 = 0,62𝑥 + 1,22 𝑃2 = 0,596𝑥2 + 1,254𝑥 − 0,011 𝑃3 = 0,628𝑥3 + 1,186𝑥2 + 0,035𝑥 − 0,01 4. O resultado usando o código anterior é: { 𝑃1 = 0,93𝑥 + 0,528 𝑃2 = 1.011𝑥2 − 0,325𝑥 + 1,147 𝑃3 = 1.001𝑥3 − 0,002𝑥2 − 0,011𝑥 + 1,021 5. Do código anterior, para os itens a, b e c: { 𝑎) 𝑃1 = −194,141𝑥 + 72,085 𝑏) 𝑃2 = 1.228𝑥2 − 1,141𝑥 + 6,618 𝑐) 𝑃3 = 3,481𝑥3 − 2,409𝑥2 + 6,851𝑥 − 0,014 O código para os itens d e e é: import math x = [4, 4.2, 4.5, 4.7, 5.1, 5.5, 5.9, 6.3, 6.8, 7.1] y = [102.56, 113.18, 130.11, 142.05, 167.53, 195.14, 224.87, 256.73, 299.5, 326.72] def elimgauss(A,b): s = len(b) - 1 c = [0]*(s+1) for k in range (0,s,1): # transforma a matriz A em triangular superior for i in range (k+1,s+1,1): m = A[i][k]/A[k][k] A[i][k] = 0 for j in range (k+1,s+1,1): A[i][j] = A[i][j] - m*A[k][j] b[i] = b[i] - m*b[k] c[s] = round(b[s]/A[s][s],3) for i in range (s-1,-1,-1): # laço para calcular os coeficientes soma_aux = 0 for j in range (i+1,s+1,1): soma_aux = soma_aux + A[i][j]*c[j] c[i] = round((b[i] - soma_aux)/(A[i][i]),3) return c def exp1(x,y): #regressão polinomial grau 1 n = len(x) A = [[0,0],[0,0]] b = [0]*2 yn = [0]*n for i in range (0,n,1): yn[i] = math.log(y[i]) somax = 0 somay = 0 somax2 = 0 somaxy= 0 for i in range (0, n, 1): somax = somax + x[i] somax2 = somax2 + (x[i])**2 somay = somay + yn[i] somaxy = somaxy + x[i]*yn[i] A[0][0] = n A[0][1] = somax A[1][0] = somax A[1][1] = somax2 b[0] = somay b[1] = somaxy a = elimgauss (A,b) a[0] = math.exp(a[0]) print("A função interpoladora é: ", a[0],"*e^(x*", a[1],")") def exp2(x,y): #regressão polinomial grau 1 n = len(x) A = [[0,0],[0,0]] b = [0]*2 xn = [0]*n yn = [0]*n for i in range (0,n,1): yn[i] = math.log(y[i]) xn[i] = math.log(x[i]) somax = 0 somay = 0 somax2 = 0 somaxy= 0 for i in range (0, n, 1): somax = somax + xn[i] somax2 = somax2 + (xn[i])**2 somay = somay + yn[i] somaxy = somaxy + xn[i]*yn[i] A[0][0] = n A[0][1] = somax A[1][0] = somax A[1][1] = somax2 b[0] = somay b[1] = somaxy a = elimgauss (A,b) a[0] = math.exp(a[0]) print("A função interpoladora é: ", a[0],"*x^", a[1]) exp1(x,y) exp2(x,y) O resultado é: 6. Para a, b e c: Para d e e: 7. Seção 8.2 1. A) Usando o código abaixo, usando n pontos: import math import numpy as np def f(x): return x**2 + 3*x +2 def transposta(M): return list(map(lambda *i: [j for j in i], *M)) def sobredeterminado(n,a0,an): x = [0]*n # n = numero de pontos y = [0]*n x[0] = a0 h = (an-a0)/(n-1) A = np.zeros((n,2)) At = np.zeros((2,n)) b = np.zeros((n,1)) for i in range (0,n,1): #definindo o conjunto de pontos if i < n-1: x[i+1] = x[i] + h y[i] = f(x[i]) for i in range (0,n,1): A[i][0] = 1 A[i][1] = x[i] b[i][0] = y[i] At = transposta(A) aux = np.dot(At,A) aux2 = np.dot(At,b) aux3 = np.linalg.inv(aux) x = np.dot(aux3,aux2) return x sol = sobredeterminado(4,0,1) print("O polinômio é: P(x) = ",sol[0],"*x + ",sol[1]) O resultado é: b) c) d) e) f) 2. Usando n = 8 pontos: a) b) c) d) e) f) Seção 8.5