% Nome: João Vitor Tacchi Martins Lanna
% Matricula: 20.2.8088
% Ajuste de Curvas pelo Metodo dos Minimos Quadrados - Regressao Linear Simples

clear; clc; close all;
disp('Ajuste de Curva por Minimos Quadrados');

% 1) Dados iniciais
xtab = [0.3 2.7 4.5 5.9 7.8];
ytab = [1.8 1.9 3.1 3.9 3.3];

% 2) Numero de pontos e de parametros
n = length(xtab);
p = 2;

% 3) Somatorios
Sx = sum(xtab);
Sy = sum(ytab);
Sxy = sum(xtab .* ytab);
Sx2 = sum(xtab .^ 2);
Sy2 = sum(ytab .^ 2);

% 4) Coeficientes
b1 = (Sx*Sy - n*Sxy) / (Sx^2 - n*Sx2)
b0 = (Sy - b1*Sx) / n

% 5) Desvio residual
u = b0 + b1 * xtab;
D = sum((ytab - u) .^ 2);

% 6) Indicadores
r2 = 1 - D / (Sy2 - (1/n)*Sy^2)
sigma2 = D / (n - p)

% 7) Modelo
ufun = @(x) b0 + b1 * x;

% Função
fprintf("u(x) = %.4f + %.4f * x\n", b0, b1)

% 8) Grafico
xgrid = min(xtab)-0.5 : 0.1 : max(xtab)+0.5;
plot(xgrid, ufun(xgrid), "-", xtab, ytab, "go");
title("Regressão Linear por por Minimos Quadrados");
legend("Reta ajustada", "Dados Experimentais", "Location", "northwest")
grid on;

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: