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

load('linTrainTables.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 "Time" variable and corresponding degradation feature measurements in the "Condition" variable.
%
% Create a linear degradation model, specifying the life time variable units.

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

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

fit(mdl,linTrainTables,"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('linTestData.mat','linTestData1')

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

threshold = 60;

%
% Assume that you measure the component condition indicator after 48 hours. Predict the remaining useful life of the component at this time using the trained linear degradation model. The RUL is the forecasted time at which the degradation feature will pass the specified threshold.

estRUL = predictRUL(mdl,linTestData1(48,:),threshold)

% The estimated RUL is around 114 hours, which indicates a total predicted life span of 162 hours.
% Copyright 2017-2020 The MathWorks, Inc.