% ***** LEAST-SQUARES REGRESSION ***** % % m: Number of data samples % n: Number of factors or dependent variables % p: Index value of a single sample within the set of m samples % q: Index value of an individual feature within the % ith sample's feature (or variable) vector % x: Feature vector with n+1 features, i.e. x=[x0 x1 x2 x3] & x0=1 % theta: Parameter vector with at least n+1 parameters, % i.e. theta=[theta0 theta1 theta2 theta3] % h: "Hypothesis" or prediction model, i.e. h=theta*transpose(x) % J: Cost function dependent on the parameter values in vector theta clear all close all X=[]; Y=[]; H=[]; THETA1=[]; THETA2=[]; % Read in data points S = readtable("simulatedDegradationData.csv", "Delimiter", ",", "ReadVariableNames", true); for n = 1:7 count = 2; x = table2array(S((((n-1)*20+1):(n*20)), 3))./max(table2array(S((((n-1)*20+1):(n*20)), 3))); y = table2array(S((((n-1)*20+1):(n*20)), 4))./max(table2array(S((((n-1)*20+1):(n*20)), 4))); % Initialize parameters theta(1) = 1.1; theta(2) = 1.1; % Initialize the "momentum" alpha alpha = [0.1 1.1]; m = length(y); h = theta(1)+theta(2).*x; J(1) = (1/m).*sum(h-log(y)).^2; e=1; while (e > 0.00001) theta(1) = theta(1)-alpha(1).*(2/m).*sum((h-log(y))); theta(2) = theta(2)-alpha(2).*(2/m).*sum((h-log(y)).*x); h = theta(1)+theta(2).*x; J(count) = (1/m).*sum((h-log(y)).^2); e = abs(J(count)-J(count-1)); count = count+1; set(gcf,'Color','white') subplot(2,1,1) plot(x, log(y), '.','LineWidth',2) hold on if (n > 1) plot(X, Y, '+','LineWidth',2) plot(X, H, '-','LineWidth',2) end title('Exponential Curve Fit via Linear Least Squares','fontsize',11,'fontweight','b') xlabel('Normalized Segment Number (Qty 20)','fontsize',10) ylabel('Normalized Dependent Variable','fontsize',10) set(gca,'XColor',[0.35,0.35,0.35]) set(gca,'YColor',[0.35,0.35,0.35]) grid on grid minor set(gca,'Color',[0.65,0.65,0.65]) axis([-0.1 1.1 -2.1 1.1]) hold on plot(x, h, 'o','LineWidth',1) pause(0.1) hold off subplot(2,1,2) plot(2:(count-1),J(2:(count-1)),'g','LineWidth',2); title('Cost Function','fontsize',11,'fontweight','b') xlabel('Epoch Number','fontsize',10) ylabel('Cost Value','fontsize',10) grid on grid minor set(gca,'Color',[0.65,0.65,0.65]) set(gca,'XColor',[0.35,0.35,0.35]) set(gca,'YColor',[0.35,0.35,0.35]) axis([0 100 0 0.1]) hold off end X = [X x]; Y = [Y log(y)]; H = [H h]; THETA1 = [THETA1 theta(1)] THETA2 = [THETA2 theta(2)] end