This example shows how to forecast the tidal depth at Ockway Bay. Predicting tidal depths is vital because your boat can get easily stuck in the mud in a shallow bay if you are not aware of the water depth.
Note: System Identification Toolbox software is required to run this example. |
ThingSpeak™ channel 50289 contains data about tidal depth at Ockway Bay. The data is collected once every 5 minutes. Field 1 of the channel contains tidal depth data.
Read the data using the thingSpeakRead
function
from channel 50289 on a particular day, for example, July 01, 2016.
Note: This example is designed to illustrate how an AR model may be used to approximately predict a sinusoidal function such as tide level, and is not designed as an alternative to sophisticated tidal forecasting techniques. MATLAB^{®} functions that forecast tide levels using measured tidal data, such as UTide functions are available on MATLAB Central. |
startDate = datetime(2016,07,01,0,0,0); endDate = datetime(2016,07,02,0,0,0); dateRange = startDate:endDate; data = thingSpeakRead(50289,'DateRange',dateRange,'Fields',1);
Use the iddata
function
to create an iddata
object of the tidal depth data.
As the tidal data is not zero mean, detrend the data to make the data
zero mean. Since the tidal depth varies with time, use the ar
function
to fit a discrete-time autoregressive model to the data.
sampleTime = 5; IDdata = iddata(data,[],sampleTime,'OutputName',{'Tidal Depth'},'TimeUnit','minutes')
IDdata = Time domain data set with 288 samples. Sample time: 5 minutes Outputs Unit (if specified) Tidal Depth
% Capture the offest before detrending trend = getTrend(IDdata,0); % Detrend the data to zero mean IDdata = detrend(IDdata,0); modelOrder = 8; % Fit an AR model to represent the system sys = ar(IDdata,modelOrder);
Use the forecast
function
to forecast the tidal depth for the next day. Set the number of samples
of the forecast data to be 288 as the measured tidal depth data is
updated once every 5 minutes.
numSamples = 288; % yf is the forecasted model response, and yf_sd is the standard deviation of the output. % x0 is the estimated value for initial states, and sysf is the forecasting state-space model. % Also returned are the state trajectory, x, and standard deviation of the trajectory, x_sd [yf,x0,sysf,yf_sd,x,x_sd] = forecast(sys,IDdata,numSamples); % Retrend data before plotting IDdata = retrend(IDdata,trend); yf = retrend(yf,trend);
Plot the measured data along with the forecasted tidal data.
figure; UpperBound = iddata(yf.OutputData+1*yf_sd,[],yf.Ts,'Tstart',yf.Tstart,'TimeUnit','minutes'); LowerBound = iddata(yf.OutputData-1*yf_sd,[],yf.Ts,'Tstart',yf.Tstart,'TimeUnit','minutes'); plot(IDdata(:,:,[]),'r',yf(:,:,[]),'b'); hold on % Plot also the 1-std deviation of uncertainties of the forecasted model plot(UpperBound,'k--',LowerBound,'k--'); legend({'measured','forecasted','1 sd uncertainty'},'Location','best'); xlabel('Time'); ylabel('Tidal depth (inches)'); title('Measured and Forecasted Tidal Wave Depths');
This plot shows the measured and forecasted system response along with 1 standard deviation uncertainty bounds.