% RegressMultEx2.m % % Calculations for multiple regression example. % Multiple regression fit of tensile strength of steel versus composition, % C % and Cr %. % Create the matrix of x values (and 1's). X = [ 1, 0.08, 14.5; 1, 0.12, 16.0; 1, 0.12, 18.0; 1, 0.20, 23.0]; % Create y pts. y = [415, 450, 450, 515]'; % Print values. data = [X,y] % Now compute the multiple regression solution. Xplus = pinv(X) % Find the hyperplane fit. ba_soln = Xplus * y % Print out the pts predicted by the fit. fit = [X,X * ba_soln] hold off % Make 3D plot. miny = 400; maxy = 525; plot3(data(:,2)',data(:,3)',data(:,4)','+k') xlabel('x1'), ylabel('x2'), zlabel('y') axis([0, 0.25, 0, 25, miny, maxy]) grid on hold on for index = 1 : length(y) plot3([data(index,2);data(index,2)], ... [data(index,3);data(index,3)], ... [miny,data(index,4)],'-g') end plot3(fit(:,2)',fit(:,3)',fit(:,4)','or') hold off