clear;
clc;
close all;
fprintf ('Ajuste de Curva por Minimos Quadrados \n\n');
% Dados Iniciais
x_tab = [0.3 2.7 4.5 5.9 7.8];
y_tab = [1.8 1.9 3.1 3.9 3.3];
n = length (x_tab);
p = 2;
% Somatório da variável x
Sx = sum(x_tab);
% Somatório da variável y
Sy = sum(y_tab);
% Somatório do produto dos pontos (x · y)
Sxy = sum (x_tab .* y_tab );
% Somatório dos quadrados de x
Sx2 = sum(x_tab .^2);
% Somatório dos quadrados de y
Sy2 = sum(y_tab .^2);
% Calcule os coeficientes da reta ajustada (b1 e b0):
b1 = (Sx*Sy - n*Sxy) / (Sx^2 - n*Sx2)
b0 = (Sy - b1 * Sx) / n
U_tab = b0 + b1 * x_tab;
D = sum((y_tab - U_tab).^2);
r2 = 1 - (D / (Sy2 - (1/n) * Sy^2))
sigma2 = D / (n-p)
U = @(x) b0 + b1 * x;
x_grid = min(x_tab) - 0.5 : 0.1 : max (x_tab) + 0.5;
figure (1)
plot(x_grid, U(x_grid));
hold on;
plot(x_tab, y_tab, 'o');
grid on;
title ('Ajuste de Curvas');
xlabel ('x');
ylabel ('y');
legend ('Modelo', 'Tabela');
To embed this project on your website, copy the following code and paste it into your website's HTML: