MATLAB: How to use previous points calculated from a function.

loopMATLAB

Hello I have the following function
d=abs(B32 + A32*[X;Y]);sqrt(A32(1,1)^2+A32(1,2)^2);
the answer (d) changes based on the row matlab is on.
I have no problem calculating (d) based on the row.
What I want to do is use an equation R = d2*(t2-t1)/(d1-d2)
What I want to do is d2 to equal the current (d) that is being calculated.
I want d1 to equal the previous answer for (d).
I want the first d1 that is used to = 0 then the next d1 to equal (d) and so forth.
Is this possible.
Any help is greatly appreciated.

Best Answer

% Plotting the TestPoints on the graph
for i = 1:size(TestPoints,1)
X=TestPoints(i,2);
Y=TestPoints(i,3);
Delta1=log(P1)-0.5*mu1*inv(Sw)*mu1'+mu1*inv(Sw)*[X;Y];
Delta2=log(P2)-0.5*mu2*inv(Sw)*mu2'+mu2*inv(Sw)*[X;Y];
Delta3=log(P3)-0.5*mu3*inv(Sw)*mu3'+mu3*inv(Sw)*[X;Y];
pause(1)
clc
% The first initial d1 = 0 because there is no previous distance. It's the
% first one. The next d1 will have the previous value of d2.
try
d1;
catch
d1=0;
end
% The first initial t1 = 0 because there is no previous time. It's the
% first one. The next t1 will have the previous value of t2.
try
t1;
catch
t1=0;
end
%
d2=abs(B32 + A32*[X;Y]);sqrt(A32(1,1)^2+A32(1,2)^2); % Distance to the LDA Boundary that separates aged and replace
t2=TestPoints(i,1); %time in seconds for the particular datapoint
%
%Calculating the Remaining Useful Life
RUL = d2*(t2-t1)/(d1-d2); % Units are in seconds
RUL_days=RUL/86400; % Units are in days
d1=d2;
t1=t2;
pause(1)
end