clear all
close all

load carsmall
x1 = Weight;
x2 = Acceleration;
x = [%ones(length(x1),1) 
    x1 x2];
y = MPG;
X = [ones(length(x1),1) x1 x2];
x1(isnan(y),:) = [];
x2(isnan(y),:) = [];
x(isnan(y),:) = [];
X(isnan(y),:) = [];
y(isnan(y)) = [];

% load accidents
% x = hwydata(:,14); %Population of states
% y = hwydata(:,4); %Accidents per state
% %format long
% %b1 = x\y
% X = [ones(length(x),1) x];

b = X\y

b = inv(X'*X)*X'*y

b_ = fitlm(x,y)

b__ = regress(y,X)

yCalc1 = X*b;

scatter3(x1,x2,y)
hold on
plot3(x1,x2,yCalc1,'.')

% scatter(x,y)
% hold on
% plot(x,yCalc1)
% xlabel('Population of state')
% ylabel('Fatal traffic accidents per state')
% title('Linear Regression Relation Between Accidents & Population')
% grid on


% A = magic(3);
% B = [15; 15; 15];
% x = A\B
% A
% A*x
% rank(A)
% A = magic(4);
% b = [34; 34; 34; 34];
% x = A\b
% rank(A)
% A
% A*x