%% Predict RUL Using Exponential Degradation Model
% Load training data.

load('expTrainTables.mat')

% The training data is a cell array of tables. Each table is a degradation feature profile for a component. Each profile consists of life time measurements in the "Hours" variable and corresponding degradation feature measurements in the "Condition" variable.
%
% Create an exponential degradation model, specifying the life time variable units.

mdl = exponentialDegradationModel('LifeTimeUnit',"hours");

%
% Train the degradation model using the training data. Specify the names of the life time and data variables.

fit(mdl,expTrainTables,"Time","Condition")

%
% Load testing data, which is a run-to-failure degradation profile for a test component. The test data is a table with the same life time and data variables as the training data.

load('expTestData.mat')

%
% Based on knowledge of the degradation feature limits, define a threshold condition indicator value that indicates the end-of-life of a component.

threshold = 500;

%
% Assume that you measure the component condition indicator every hour for 150 hours. Update the trained degradation model with each measurement. Then, predict the remaining useful life of the component at 150 hours. The RUL is the forecasted time at which the degradation feature will pass the specified threshold.

for t = 1:150 
 update(mdl,expTestData(t,:)) 
end 
estRUL = predictRUL(mdl,threshold) 

% The estimated RUL is around 137 hours, which indicates a total predicted life span of 287 hours.
%
% Copyright 2018 The MathWorks, Inc.