Buscar

Aula 4 - Ajuste de Curvas - ATUALIZADO

Prévia do material em texto

Computação Numérica
Aula de Laboratório 4: Ajuste de Curvas
Professor Filipe Taveiros
A tabela ao lado apresenta
a evolução da população
de um determinado país
ao longo dos anos, em
milhões de habitantes.
Exemplo contextualizado
Ano População
1872 9.9
1890 14.3
1900 17.4
1920 30.6
1940 41.2
1950 51.9
1960 70.2
1970 93.1
1980 119
1991 146.2
1996 157.1
1. Use o Scilab para gerar um
gráfico de pontos (sem linhas)
deste conjunto de dados.
2. Avalie e compare os métodos
de ajuste de curva estudados.
Aquecimento
Ano População
1872 9.9
1890 14.3
1900 17.4
1920 30.6
1940 41.2
1950 51.9
1960 70.2
1970 93.1
1980 119
1991 146.2
1996 157.1
Aquecimento
Scilab 5.5.1 Console
Aquecimento
//Definição do conjunto de dados
Scilab 5.5.1 Console
Aquecimento
//Definição do conjunto de dados
-->x=[1872 1890 1900 1920 1940 1950 1960 1970 1980 1991 1996];
Scilab 5.5.1 Console
Aquecimento
//Definição do conjunto de dados
-->x=[1872 1890 1900 1920 1940 1950 1960 1970 1980 1991 1996];
-->y=[9.9 14.3 17.4 30.6 41.2 51.9 70.2 93.1 119 146.2 157.1];
Scilab 5.5.1 Console
Aquecimento
//Definição do conjunto de dados
-->x=[1872 1890 1900 1920 1940 1950 1960 1970 1980 1991 1996];
-->y=[9.9 14.3 17.4 30.6 41.2 51.9 70.2 93.1 119 146.2 157.1];
-->//Plot do gráfico discreto
Scilab 5.5.1 Console
Aquecimento
//Definição do conjunto de dados
-->x=[1872 1890 1900 1920 1940 1950 1960 1970 1980 1991 1996];
-->y=[9.9 14.3 17.4 30.6 41.2 51.9 70.2 93.1 119 146.2 157.1];
-->//Plot do gráfico discreto
-->plot(x,y,'.r') //Opção pontos discretos vermelhos
Scilab 5.5.1 Console
Aquecimento
Aquecimento
Atenção: Não feche esta janela!
Aquecimento
Métodos estudados:
1. Regressão linear
2. Regressão polinomial
3. Interpolação de Lagrange
4. Interpolação de Newton
Aquecimento
Comecemos pela regressão linear!
Scilab 5.5.1 Console
Aquecimento
Comecemos pela regressão linear!
Scilab 5.5.1 Console
𝑦 = 𝑎𝑥 + 𝑏
𝑎 =
𝑛 𝑥𝑖𝑦𝑖 − 𝑥𝑖 𝑦𝑖
𝑛 𝑥𝑖
2 − ( 𝑥𝑖)
2
𝑏 =
 𝑦𝑖 − 𝑥𝑖 𝑎
𝑛
Aquecimento
Comecemos pela regressão linear!
-->sx=sum(x); sy = sum(y); sxq = sum(x.^2); sxy = sum(x.*y);
Scilab 5.5.1 Console
𝑦 = 𝑎𝑥 + 𝑏
𝑎 =
𝑛 𝑥𝑖𝑦𝑖 − 𝑥𝑖 𝑦𝑖
𝑛 𝑥𝑖
2 − ( 𝑥𝑖)
2
𝑏 =
 𝑦𝑖 − 𝑥𝑖 𝑎
𝑛
Aquecimento
Comecemos pela regressão linear!
-->sx=sum(x); sy = sum(y); sxq = sum(x.^2); sxy = sum(x.*y);
Scilab 5.5.1 Console
𝑦 = 𝑎𝑥 + 𝑏
𝑎 =
𝑛 𝑥𝑖𝑦𝑖 − 𝑥𝑖 𝑦𝑖
𝑛 𝑥𝑖
2 − ( 𝑥𝑖)
2
𝑏 =
 𝑦𝑖 − 𝑥𝑖 𝑎
𝑛
Aquecimento
Comecemos pela regressão linear!
-->sx=sum(x); sy = sum(y); sxq = sum(x.^2); sxy = sum(x.*y);
-->a = (n*sxy - sx*sy)/(n*sxq-sx*sx);
Scilab 5.5.1 Console
𝑦 = 𝑎𝑥 + 𝑏
𝑎 =
𝑛 𝑥𝑖𝑦𝑖 − 𝑥𝑖 𝑦𝑖
𝑛 𝑥𝑖
2 − ( 𝑥𝑖)
2
𝑏 =
 𝑦𝑖 − 𝑥𝑖 𝑎
𝑛
Aquecimento
Comecemos pela regressão linear!
-->sx=sum(x); sy = sum(y); sxq = sum(x.^2); sxy = sum(x.*y);
-->a = (n*sxy - sx*sy)/(n*sxq-sx*sx);
Scilab 5.5.1 Console
𝑦 = 𝑎𝑥 + 𝑏
𝑎 =
𝑛 𝑥𝑖𝑦𝑖 − 𝑥𝑖 𝑦𝑖
𝑛 𝑥𝑖
2 − ( 𝑥𝑖)
2
𝑏 =
 𝑦𝑖 − 𝑥𝑖 𝑎
𝑛
Aquecimento
Comecemos pela regressão linear!
-->sx=sum(x); sy = sum(y); sxq = sum(x.^2); sxy = sum(x.*y);
-->a = (n*sxy - sx*sy)/(n*sxq-sx*sx);
-->b = (sy - sx*a)/n;
Scilab 5.5.1 Console
𝑦 = 𝑎𝑥 + 𝑏
𝑎 =
𝑛 𝑥𝑖𝑦𝑖 − 𝑥𝑖 𝑦𝑖
𝑛 𝑥𝑖
2 − ( 𝑥𝑖)
2
𝑏 =
 𝑦𝑖 − 𝑥𝑖 𝑎
𝑛
Aquecimento
Comecemos pela regressão linear!
-->sx=sum(x); sy = sum(y); sxq = sum(x.^2); sxy = sum(x.*y);
-->a = (n*sxy - sx*sy)/(n*sxq-sx*sx);
-->b = (sy - sx*a)/n;
-->y1 = a*x + b;
Scilab 5.5.1 Console
𝑦 = 𝑎𝑥 + 𝑏
𝑎 =
𝑛 𝑥𝑖𝑦𝑖 − 𝑥𝑖 𝑦𝑖
𝑛 𝑥𝑖
2 − ( 𝑥𝑖)
2
𝑏 =
 𝑦𝑖 − 𝑥𝑖 𝑎
𝑛
Aquecimento
Comecemos pela regressão linear!
-->sx=sum(x); sy = sum(y); sxq = sum(x.^2); sxy = sum(x.*y);
-->a = (n*sxy - sx*sy)/(n*sxq-sx*sx);
-->b = (sy - sx*a)/n;
-->y1 = a*x + b;
Scilab 5.5.1 Console
𝑦 = 𝑎𝑥 + 𝑏
𝑎 =
𝑛 𝑥𝑖𝑦𝑖 − 𝑥𝑖 𝑦𝑖
𝑛 𝑥𝑖
2 − ( 𝑥𝑖)
2
𝑏 =
 𝑦𝑖 − 𝑥𝑖 𝑎
𝑛
Aquecimento
Comecemos pela regressão linear!
-->sx=sum(x); sy = sum(y); sxq = sum(x.^2); sxy = sum(x.*y);
-->a = (n*sxy - sx*sy)/(n*sxq-sx*sx);
-->b = (sy - sx*a)/n;
-->y1 = a*x + b;
-->plot(x,y1)
Scilab 5.5.1 Console
𝑦 = 𝑎𝑥 + 𝑏
𝑎 =
𝑛 𝑥𝑖𝑦𝑖 − 𝑥𝑖 𝑦𝑖
𝑛 𝑥𝑖
2 − ( 𝑥𝑖)
2
𝑏 =
 𝑦𝑖 − 𝑥𝑖 𝑎
𝑛
Ajuste Linear
Regressão Linear
O quanto essas curvas se parecem?
Regressão Linear
O quanto essas curvas se parecem?
No uso estatístico geral, correlação se refere a
medida da relação entre duas variáveis. Este
coeficiente exprime o quanto a tendência geral
de uma curva está relacionada com outra.
Regressão Linear
O quanto essas curvas se parecem?
Scilab 5.5.1 Console
No uso estatístico geral, correlação se refere a
medida da relação entre duas variáveis. Este
coeficiente exprime o quanto a tendência geral
de uma curva está relacionada com outra.
Regressão Linear
O quanto essas curvas se parecem?
-->correl(y,y1)
Scilab 5.5.1 Console
No uso estatístico geral, correlação se refere a
medida da relação entre duas variáveis. Este
coeficiente exprime o quanto a tendência geral
de uma curva está relacionada com outra.
Regressão Linear
O quanto essas curvas se parecem?
-->correl(y,y1)
ans =
0.9354146 
Scilab 5.5.1 Console
No uso estatístico geral, correlação se refere a
medida da relação entre duas variáveis. Este
coeficiente exprime o quanto a tendência geral
de uma curva está relacionada com outra.
Regressão Polinomial
Regressão Polinomial



























































n
i
ii
n
i
ii
n
i
i
n
i
i
n
i
i
n
i
i
n
i
i
n
i
i
n
i
i
n
i
i
n
i
i
yx
yx
y
a
a
a
xxx
xxx
xxn
1
2
1
1
2
1
0
1
4
1
3
1
2
1
3
1
2
1
1
2
1
 
Exemplo de 2ª ordem
Regressão Polinomial
Scinotes
Regressão Polinomial
function a = Lpolinomial(x,y,k)
Scinotes
Regressão Polinomial
function a = Lpolinomial(x,y,k)
for j=0:k //Laço que varre as colunas
Scinotes
Regressão Polinomial
function a = Lpolinomial(x,y,k)
for j=0:k //Laço que varre as colunas
for i=0:k //Laço que varre as linhas
Scinotes
Regressão Polinomial
function a = Lpolinomial(x,y,k)
for j=0:k //Laço que varre as colunas
for i=0:k //Laço que varre as linhas
A(i+1,j+1)=sum(x.^(j+i));
Scinotes
Regressão Polinomial
function a = Lpolinomial(x,y,k)
for j=0:k //Laço que varre as colunas
for i=0:k //Laço que varre as linhas
A(i+1,j+1)=sum(x.^(j+i));
end
Scinotes
Regressão Polinomial
function a = Lpolinomial(x,y,k)
for j=0:k //Laço que varre as colunas
for i=0:k //Laço que varre as linhas
A(i+1,j+1)=sum(x.^(j+i));
end
b(j+1,1)=sum(y.*x.^(j));
Scinotes
Regressão Polinomial
function a = Lpolinomial(x,y,k)
for j=0:k //Laço que varre as colunas
for i=0:k //Laço que varre as linhas
A(i+1,j+1)=sum(x.^(j+i));
end
b(j+1,1)=sum(y.*x.^(j));
end
Scinotes
Regressão Polinomial
function a = Lpolinomial(x,y,k)
for j=0:k //Laço que varre as colunas
for i=0:k //Laço que varre as linhas
A(i+1,j+1)=sum(x.^(j+i));
end
b(j+1,1)=sum(y.*x.^(j));
end
a=sislingauss(A,b);
Scinotes
Regressão Polinomial
function a = Lpolinomial(x,y,k)for j=0:k //Laço que varre as colunas
for i=0:k //Laço que varre as linhas
A(i+1,j+1)=sum(x.^(j+i));
end
b(j+1,1)=sum(y.*x.^(j));
end
a=sislingauss(A,b);
endfunction
Scinotes
Regressão Polinomial
function a = Lpolinomial(x,y,k)
for j=0:k //Laço que varre as colunas
for i=0:k //Laço que varre as linhas
A(i+1,j+1)=sum(x.^(j+i));
end
b(j+1,1)=sum(y.*x.^(j));
end
a=sislingauss(A,b);
endfunction
Scinotes
O que é isso 
mesmo???
Solução retroativa
function [x] = retroativa(A,b)
[l,c] = size(A);
for i=c:-1:1
soma = 0;
for j=i+1:c 
soma = soma + x(j)*A(i,j);
end
x(i) = (b(i) - soma)/A(i,i);
end
endfunction
Scinotes
function [x] = sislingauss(A,b)
[l,c] = size(A);
Aa = [A b]; //matriz aumentada 
for i=1:l-1 //Início do 
escalonamento
pivo=Aa(i,i);
for j=i+1:c
m = -Aa(j,i)/pivo;
Aa(j,:) = Aa(j,:) + m* Aa(i,:);
end
end
A = Aa(:,1:c); 
b = Aa(:,c+1);
x = retroativa(A,b); //Solução
endfunction
Scinotes
Regressão Polinomial
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 2
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 2
--> a2 = Lpolinomial(x,y,2)
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 2
--> a2 = Lpolinomial(x,y,2)
--> y2 = a2(3)*x.^2 + a2(2)*x + a2(1);
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 2
--> a2 = Lpolinomial(x,y,2)
--> y2 = a2(3)*x.^2 + a2(2)*x + a2(1);
--> plot(x,y2,'m') //Cor magenta
Scilab 5.5.1 Console
Ajuste Polinomial
Regressão Polinomial
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 3
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 3
--> a3 = Lpolinomial(x,y,3)
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 3
--> a3 = Lpolinomial(x,y,3)
--> y3 = a3(4)*x.^3 + a3(3)*x.^2 + a3(2)*x + a3(1);
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 3
--> a3 = Lpolinomial(x,y,3)
--> y3 = a3(4)*x.^3 + a3(3)*x.^2 + a3(2)*x + a3(1);
--> plot(x, y3, 'k') //Cor preta
Scilab 5.5.1 Console
Ajuste Polinomial
Regressão Polinomial
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 4
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 4
--> a4 = Lpolinomial(x,y,4)
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 4
--> a4 = Lpolinomial(x,y,4)
--> y4 = a4(5)*x.^4 + a4(4)*x.^3 + a4(3)*x.^2 + a4(2)*x + a4(1);
Scilab 5.5.1 Console
Regressão Polinomial
//Regressão polinomial de grau 4
--> a4 = Lpolinomial(x,y,4)
--> y4 = a4(5)*x.^4 + a4(4)*x.^3 + a4(3)*x.^2 + a4(2)*x + a4(1);
--> plot( x, y4, 'r' ) //Cor vermelho
Scilab 5.5.1 Console
Ajuste Polinomial
Regressão MMQ
para funções genéricas
• Até o momento todos os modelos que
empregamos no ajuste seguiam o padrão de
um polinômio.
𝑦 = 𝑎𝑚𝑥
𝑚 + 𝑎𝑚−1𝑥
𝑚−1 +⋯+ 𝑎2𝑥
2 + 𝑎1𝑥 + 𝑎0
Podemos empregar um modelo genérico com funções 
g(x) quaisquer:
𝑦 = 𝑎0𝑔0 𝑥 + 𝑎1𝑔1 𝑥 +⋯𝑎𝑘𝑔𝑘(𝑥)
Regressão Polinomial
• Aplicando a teoria dos mínimos quadrados, podemos concluir que a solução do 
problema se resume a solução do seguinte sistema de equações
 𝑔0 𝑥𝑖
2 𝑔0(𝑥𝑖)𝑔1(𝑥𝑖) ⋯ 𝑔0(𝑥𝑖)𝑔𝑘(𝑥𝑖)
 𝑔1(𝑥𝑖)𝑔0(𝑥𝑖) 𝑔1 𝑥𝑖
2 ⋯ 𝑔1(𝑥𝑖)𝑔𝑘(𝑥𝑖)
⋮ ⋮ ⋱ ⋮
 𝑔𝑘(𝑥𝑖)𝑔0(𝑥𝑖) 𝑔𝑘(𝑥𝑖)𝑔1(𝑥𝑖) ⋯ 𝑔𝑘 𝑥𝑖
2
𝑎0
𝑎1
⋮
𝑎𝑘
=
 𝑦𝑖𝑔0(𝑥𝑖)
 𝑦𝑖𝑔1(𝑥𝑖)
⋮
 𝑦𝑖𝑔𝑘(𝑥𝑖)
MMQ genérico
function a = mmqgenerico(g,y)
[L C] = size(g);
k = L-1;
for j=1:k+1 //Laço que varre as colunas
for i=1:k+1 //Laço que varre as linhas
A(i,j)=sum(g(i,:).*g(j,:));
end
b(j,1)=sum(y.*g(j,:));
end
a=sislingauss(A,b);
endfunction
Scinotes
Regressão MMQ
para funções genéricas
Vamos testar o seguinte modelo:
𝑦 = 𝑎0 log(𝑥) + 𝑎1𝑥 + 𝑎2 cos(𝑥)
Scilab 5.5.1 Console
MMQ genérico
//Regressão com o modelo y = a0*log(x) + a1*x + a2*cos(x)
Scilab 5.5.1 Console
MMQ genérico
//Regressão com o modelo y = a0*log(x) + a1*x + a2*cos(x)
-->g = [log(x); x; cos(x)]; 
Scilab 5.5.1 Console
MMQ genérico
//Regressão com o modelo y = a0*log(x) + a1*x + a2*cos(x)
-->g = [log(x); x; cos(x)]; 
-->ag = mmqgenerico(g,y)
Scilab 5.5.1 Console
MMQ genérico
//Regressão com o modelo y = a0*log(x) + a1*x + a2*cos(x)
-->g = [log(x); x; cos(x)]; 
-->ag = mmqgenerico(g,y)
ag =
- 340.95302 
1.3640459 
8.0750257 
Scilab 5.5.1 Console
MMQ genérico
//Regressão com o modelo y = a0*log(x) + a1*x + a2*cos(x)
-->g = [log(x); x; cos(x)]; 
-->ag = mmqgenerico(g,y)
ag =
- 340.95302 
1.3640459 
8.0750257 
-->yg = ag'*g;
Scilab 5.5.1 Console
MMQ genérico
//Regressão com o modelo y = a0*log(x) + a1*x + a2*cos(x)
-->g = [log(x); x; cos(x)]; 
-->ag = mmqgenerico(g,y)
ag =
- 340.95302 
1.3640459 
8.0750257 
-->yg = ag'*g;
-->plot(x,yg,'c')
Scilab 5.5.1 Console
MMQ genérico
//Regressão com o modelo y = a0*log(x) + a1*x + a2*cos(x)
-->g = [log(x); x; cos(x)]; 
-->ag = mmqgenerico(g,y)
ag =
- 340.95302 
1.3640459 
8.0750257 
-->yg = ag'*g;
-->plot(x,yg,'c')
Scilab 5.5.1 Console
MMQ genérico
Não esquecer 
de quebrar a 
a linha!
Não esquecer 
de transpor o 
vetor ag!
MMQ genérico
Interpolação de Lagrange
Interpolação de Lagrange
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
P=0;
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
P=0;
for i=1:n
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
P=0;
for i=1:n
L=1;
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
P=0;
for i=1:n
L=1;
for j=1:n
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
P=0;
for i=1:n
L=1;
for j=1:n
if (i~=j) then
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
P=0;
for i=1:n
L=1;
for j=1:n
if (i~=j) then
L = L*(p-x(j))/(x(i)-x(j));
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
P=0;
for i=1:n
L=1;
for j=1:n
if (i~=j) then
L = L*(p-x(j))/(x(i)-x(j));
end
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
P=0;
for i=1:n
L=1;
for j=1:n
if (i~=j) then
L = L*(p-x(j))/(x(i)-x(j));
end
end
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
P=0;
for i=1:n
L=1;
for j=1:n
if (i~=j) then
L = L*(p-x(j))/(x(i)-x(j));
end
end
P = P + L*y(i);
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
P=0;
for i=1:n
L=1;
for j=1:n
if (i~=j) then
L = L*(p-x(j))/(x(i)-x(j));
end
end
P = P + L*y(i);
end
Scinotes
Interpolação de Lagrange
function P = lagrange(x, y, p)
n = length(x);
P=0;
for i=1:n
L=1;
for j=1:n
if (i~=j) then
L = L*(p-x(j))/(x(i)-x(j));
end
end
P = P + L*y(i);
end
endfunction
Scinotes
Interpolação de Lagrange
Scilab 5.5.1 Console
Interpolação de Lagrange
//Aproximação da populaçãoem 1945
Scilab 5.5.1 Console
Interpolação de Lagrange
//Aproximação da população em 1945
-->lagrange(x,y,1945)
Scilab 5.5.1 Console
Interpolação de Lagrange
//Aproximação da população em 1945
-->lagrange(x,y,1945)
ans =
45.52132 
Scilab 5.5.1 Console
Interpolação de Lagrange
//Aproximação da população em 1945
-->lagrange(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Lagrange
Scilab 5.5.1 Console
Interpolação de Lagrange
//Aproximação da população em 1945
-->lagrange(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Lagrange
-->t=x(1):0.25:x(11);
Scilab 5.5.1 Console
Interpolação de Lagrange
//Aproximação da população em 1945
-->lagrange(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Lagrange
-->t=x(1):0.25:x(11);
-->for i=1:length(t)
Scilab 5.5.1 Console
Interpolação de Lagrange
//Aproximação da população em 1945
-->lagrange(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Lagrange
-->t=x(1):0.25:x(11);
-->for i=1:length(t)
-->y_lagrange(i) = lagrange(x,y,t(i));
Scilab 5.5.1 Console
Interpolação de Lagrange
//Aproximação da população em 1945
-->lagrange(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Lagrange
-->t=x(1):0.25:x(11);
-->for i=1:length(t)
-->y_lagrange(i) = lagrange(x,y,t(i));
-->end
Scilab 5.5.1 Console
Interpolação de Lagrange
//Aproximação da população em 1945
-->lagrange(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Lagrange
-->t=x(1):0.25:x(11);
-->for i=1:length(t)
-->y_lagrange(i) = lagrange(x,y,t(i));
-->end
-->plot(t,y_lagrange,'--b') //Traço azul
Scilab 5.5.1 Console
Interpolação de Lagrange
Interpolação de Newton
Interpolação de Newton
Scinotes
Interpolação de Newton
function Tab = diffdiv(x,y)
Scinotes
Interpolação de Newton
function Tab = diffdiv(x,y)
n = length(x);
Scinotes
Interpolação de Newton
function Tab = diffdiv(x,y)
n = length(x);
Tab(:,1)=y;
Scinotes
Interpolação de Newton
function Tab = diffdiv(x,y)
n = length(x);
Tab(:,1)=y;
for i=1:n-1
Scinotes
Interpolação de Newton
function Tab = diffdiv(x,y)
n = length(x);
Tab(:,1)=y;
for i=1:n-1
for j=1:n-i
Scinotes
Interpolação de Newton
function Tab = diffdiv(x,y)
n = length(x);
Tab(:,1)=y;
for i=1:n-1
for j=1:n-i
Tab(j,i+1) = (Tab(j+1,i)-Tab(j,i))/(x(j+i)-x(j));
Scinotes
Interpolação de Newton
function Tab = diffdiv(x,y)
n = length(x);
Tab(:,1)=y;
for i=1:n-1
for j=1:n-i
Tab(j,i+1) = (Tab(j+1,i)-Tab(j,i))/(x(j+i)-x(j));
end
Scinotes
Interpolação de Newton
function Tab = diffdiv(x,y)
n = length(x);
Tab(:,1)=y;
for i=1:n-1
for j=1:n-i
Tab(j,i+1) = (Tab(j+1,i)-Tab(j,i))/(x(j+i)-x(j));
end
end
Scinotes
Interpolação de Newton
function Tab = diffdiv(x,y)
n = length(x);
Tab(:,1)=y;
for i=1:n-1
for j=1:n-i
Tab(j,i+1) = (Tab(j+1,i)-Tab(j,i))/(x(j+i)-x(j));
end
end
endfunction
Scinotes
Interpolação de Newton
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
n = length(x);
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
n = length(x);
Tabdiffdiv = diffdiv(x,y);
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
n = length(x);
Tabdiffdiv = diffdiv(x,y);
P = y(1);
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
n = length(x);
Tabdiffdiv = diffdiv(x,y);
P = y(1);
for i=2:n
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
n = length(x);
Tabdiffdiv = diffdiv(x,y);
P = y(1);
for i=2:n
M=1
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
n = length(x);
Tabdiffdiv = diffdiv(x,y);
P = y(1);
for i=2:n
M=1
for j=1:i-1
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
n = length(x);
Tabdiffdiv = diffdiv(x,y);
P = y(1);
for i=2:n
M=1
for j=1:i-1
M = M*(p-x(j))
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
n = length(x);
Tabdiffdiv = diffdiv(x,y);
P = y(1);
for i=2:n
M=1
for j=1:i-1
M = M*(p-x(j))
end
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
n = length(x);
Tabdiffdiv = diffdiv(x,y);
P = y(1);
for i=2:n
M=1
for j=1:i-1
M = M*(p-x(j))
end
P = P + M*Tabdiffdiv(1,i);
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
n = length(x);
Tabdiffdiv = diffdiv(x,y);
P = y(1);
for i=2:n
M=1
for j=1:i-1
M = M*(p-x(j))
end
P = P + M*Tabdiffdiv(1,i);
end
Scinotes
Interpolação de Newton
function P = newton(x,y,p)
n = length(x);
Tabdiffdiv = diffdiv(x,y);
P = y(1);
for i=2:n
M=1
for j=1:i-1
M = M*(p-x(j))
end
P = P + M*Tabdiffdiv(1,i);
end
endfunction
Scinotes
Interpolação de Newton
Scilab 5.5.1 Console
Interpolação de Newton
//Aproximação da população em 1945
Scilab 5.5.1 Console
Interpolação de Newton
//Aproximação da população em 1945
-->newton(x,y,1945)
Scilab 5.5.1 Console
Interpolação de Newton
//Aproximação da população em 1945
-->newton(x,y,1945)
ans =
45.52132 
Scilab 5.5.1 Console
Interpolação de Newton
//Aproximação da população em 1945
-->newton(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Newton
Scilab 5.5.1 Console
Interpolação de Newton
//Aproximação da população em 1945
-->newton(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Newton
-->t=x(1):0.25:x(11);
Scilab 5.5.1 Console
Interpolação de Newton
//Aproximação da população em 1945
-->newton(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Newton
-->t=x(1):0.25:x(11);
-->for i=1:length(t)
Scilab 5.5.1 Console
Interpolação de Newton
//Aproximação da população em 1945
-->newton(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Newton
-->t=x(1):0.25:x(11);
-->for i=1:length(t)
-->y_newton(i) = newton(x,y,t(i));
Scilab 5.5.1 Console
Interpolação de Newton
//Aproximação da população em 1945
-->newton(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Newton
-->t=x(1):0.25:x(11);
-->for i=1:length(t)
-->y_newton(i) = newton(x,y,t(i));
-->end
Scilab 5.5.1 Console
Interpolação de Newton
//Aproximação da população em 1945
-->newton(x,y,1945)
ans =
45.52132 
//Plot da interpolação de Newton
-->t=x(1):0.25:x(11);
-->for i=1:length(t)
-->y_newton(i) = newton(x,y,t(i));
-->end
-->plot(t,y_newton,'-.r') //Traço e ponto vermelho
Scilab 5.5.1 Console
Interpolação de Newton
Tópicos especiais
Para saber como os gráficos foram gerados
//Ajuste de curvas e interpolação
//Prof. Filipe Taveiros
//Definição do conjunto de dados
x=[1872 1890 1900 1920 1940 1950 1960 1970 1980 1991 1996];
y=[9.9 14.3 17.4 30.6 41.2 51.9 70.2 93.1 119 146.2 157.1];
//Plot do gráfico discreto
plot(x,y,'.r') //Opção pontos discretos vermelhos
ylabel("População")
xlabel("Ano")
//Regressão Linear
sx=sum(x); sy = sum(y); sxq = sum(x.^2); sxy = sum(x.*y);
a = (n*sxy - sx*sy)/(n*sxq-sx*sx);
b = (sy - sx*a)/n;
y1 = a*x + b;
plot(x,y1)
//Regressão polinomial de grau 2
a2 = Lpolinomial(x,y,2)
y2 = a2(3)*x.^2 + a2(2)*x + a2(1);
plot( x, y2, 'm' ) //Cor magenta
//Regressão polinomial de grau 3
a3 = Lpolinomial(x,y,3)
y3 = a3(4)*x.^3 + a3(3)*x.^2 + a3(2)*x + a3(1);
plot(x,y3,'k')
//Regressão polinomial de grau 4
a4 = Lpolinomial(x,y,4)
y4 =a4(5)*x.^4 + a4(4)*x.^3 + a4(3)*x.^2 + a4(2)*x + a4(1);
plot( x, y4, 'r' ) //Cor vermelho
//Plot da interpolação de Lagrange
t=x(1):0.25:x(11);
for i=1:length(t)
y_lagrange(i) = lagrange(x,y,t(i));
end
plot(t,y_lagrange,'--b')
//Plot da interpolação de Newton
t=x(1):0.25:x(11);
for i=1:length(t)
y_newton(i) = newton(x,y,t(i));
end
plot(t,y_newton,'-.r')
legend(["Dados originais"; sprintf("Regressão linear r = 
%.4f",correl(y,y1)); sprintf("Regressão polinomial de grau 2 
r = %.4f",correl(y,y2));sprintf("Regressão polinomial de grau 
3 r = %.4f",correl(y,y3));sprintf("Regressão polinomial de 
grau 4 r = %.4f",correl(y,y4)); "Interpolação de Lagrange";
"Interpolação de Newton"],2)
Scinotes

Continue navegando