clc
clear all
close all

x=[-40.0;-15.0;10.0;38.0;67.0]
y=[-24.0;-12.0;-12.0;15.0;30.0]

W_l=eye(5)

% as eqs. de condicao, que relacionam as observacoes (coordenadas) e os
% parametros sao LINEARES

% y1+v1=m*x1+b
% y2+v2=m*x2+b
% y3+v3=m*x3+b
% y4+v4=m*x4+b
% y5+v5=m*x5+b

% v1-m*x1=-y1
% v2-m*x2=-y2
% v3-m*x3=-y3
% v4-m*x4=-y4
% v5-m*x5=-y5

B=[-x(1) -1;-x(2) -1;-x(3) -1;-x(4) -1;-x(5) -1]
f=[-y(1);-y(2);-y(3);-y(4);-y(5)]

N=B'*W_l*B
t=B'*W_l*f
delta=inv(N)*t
v=f-B*delta

hold on
plot(x(1), y(1), 'r.', 'LineWidth', 2, 'MarkerSize', 15);
plot(x(2), y(2), 'r.', 'LineWidth', 2, 'MarkerSize', 15);
plot(x(3), y(3), 'r.', 'LineWidth', 2, 'MarkerSize', 15);
plot(x(4), y(4), 'r.', 'LineWidth', 2, 'MarkerSize', 15);
plot(x(5), y(5), 'r.', 'LineWidth', 2, 'MarkerSize', 15);

m = delta(1); % Slope
b = delta(2) % Y-intercept
% Generate a range of x-values
x = linspace(x(1), x(5), 100);
% Calculate the corresponding y-values
y = m*x + b;
% Plot the line
plot(x, y);
hold off