Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this is being coded in matlab, having trouble with finding the error equations 2

ID: 3805012 • Letter: T

Question

this is being coded in matlab, having trouble with finding the error equations

2. Curve-Fitting Program Recently, acoustic Doppler current profilers (ADCP) allow for the river flow velocity to be measured instantaneously along a cross-section of a river. In this assignment, we will be using 3 data for a side-looking ADCP that is permanently mounted at a river gauging station. Thus, we are able to generate a large dataset of discharge versus gauge height. The data are reported in cubic feet per second (cfs) for discharge and in feet (ft) for gauge height, and the gauge height in the dataset represents the height above the zero-flow height; hence, h0 in Equation (1) is zero.

First, we must obtain the stage-discharge data and store them on our hard drive for analysis. Download the function file gaugedata.m from the course website. At the Matlab prompt, run the commands [Q, h, a2, b2] = gaugedata; data = [h Q]; save my_data.dat -ascii data a2 b2 The first line generates a random set of data for you to analyze. The second line, packs these data in a single variable so that the first column contains stage in ft and the second column contains discharge in cfs. The third line saves this data in a file called my_data.dat. The fourth and fifth lines display the values stored in the fit parameters a2 and b2. Write down these parameter values for comparison later to your curve fit. Write a Matlab program to do the following tasks. For the curve fitting, you should write your own function file. You may base this file on Figure 14.15 in the Chapra (2011) textbook.

• Load in the data and plot the data as points ’.’ (blue dot) in figure(1).

• Fit a power law of the form 2 2 b Q ah = (2) and report the fitted parameter values a2 and b2. Compare these values to the base values output in the commands above. Overlay the data in figure(1) with the fitted curve.

• Create a scatter plot in a second figure window that presents the actual error Q Qˆ n = - and the squared error ( ) 2 2 Q Qˆ n = - for each measurement. On the x-axis, plot the measured gauge height h, and on the y-axis plot the errors. Use the subplot(2,1,1) command to plot the n errors and subplot(2,1,2) command to plot the 2 n errors. Experiment with linear, log, and loglog plots to get the best presentation of the data. Remember to label your axes including units of the data.

• Calculate the mean and standard deviation of the errors Q Qˆ n = - and squared errors ( ) 2 2 Q Qˆ n = - . Report these calculated values with their appropriate units. • Remove the data points from the dataset that correspond to the top 3 errors Q Qˆ n = - . You should write the code in your program to do this automatically. Repeat the curve fit 4 for this new data set, output the new fit coefficients, and compute the mean and standard deviation of the n and 2 n errors. How has the fit improved or gotten worse in comparison to the base answer recorded above?

• Plot the revised curve fit in figure(1) with the other data and curves. Make the revised curve thicker by setting LineWidth equal to 2.5 in plot so the revised curve stands out. Also plot an ’x’ (red cross) on top of the data points that were removed. Add a legend to the plot and be sure to label your axes. Comment on any improvement you observe in the fit and explain how the fit has changed. You may use the built-in Matlab functions min, max, mean, and std, but do not use any other Matlab functions that replace significant parts of the programming work, such as find, polyfit, or nlinfit.

here is the guage data function:

function [Q, h, a2, b2] = gaugedata
%
% GAGEDATA Generate synthetic stage-discharge measurements
%
% This function uses random number generators to generate synthetic
% measurements of discharge Q in cfs versus gauge height (called river
% stage) h in ft. The gauge height is normalized so that zero flow rate is
% obtained at zero gauge height.
%
% Example
% [Q, h, a2, b2] = gaugedata;
%
% Kuang-An Chang
% Modified from Prof. Socolofsky's lecture
% CVEN 302
% Spring 2014

% The base stage-discharge relationship is obtained from the Brazos River
% at State Highway 21 near Bryan, and it approximated by Q = 190 x ^(1.64).
% Enter the base parameters of the fit
a0 = 1.64;
a1 = 190;
  
% Generate a random set of parameters
a0 = a0 + 2*sqrt(3)*rand(1) * 0.6;
a1 = a1 + randn(1) * 85;
  
% Generate the synthetic data
hmax = 45;
hmin = 0.5;
nh = 5;
dh = (hmax - hmin) / (nh - 1);
xp = 0.5;
for i = 1:nh
xp = [xp hmin+dh*i/5:dh*i/20:dh*i];
end
xp = xp' + 0.05*randn(length(xp),1).*xp';
yp = (a1 + randn(length(xp),1)*a1*0.03) .* xp .^ ...
(a0 + randn(length(xp),1)*a0*0.03);
h = xp;
Q = yp;

% Get some data at high flow rate
n_errors = 3;
he = linspace(40,60,n_errors);
he = he' + 0.05*randn(length(he),1).*he';
Qe = (a1 + randn(length(he),1)*a1*0.03) .* he .^ ...
(a0 + randn(length(he),1)*a0*0.03);

% Randomly insert the high flow-rate data and add a systematic error to it.
for i = 1:n_errors
j = floor(rand*length(h));
Q(j) = Qe(i) - 0.3*2*sqrt(3)*rand(1).*Qe(i);
h(j) = he(i);
end

% Remove any negative values
h(h<0) = -h(h<0);
Q(Q<0) = -Q(Q<0);
a2 = a1;
b2 = a0;

Explanation / Answer

1. Stage-Discharge Relationship
The U.S. Geological Survey (USGS) reports daily streamflow measurements across the United
States using a network of river flow gauges. Each gauge consists of a stilling well in which the
river flow depth is measured. The depth measurement is converted to river flow using a
carefully calibrated stage-discharge relationship. Measurements are made on an hourly basis,
and the daily mean flow is reported in a National database of stream flow data available at
http://waterdata.usgs.gov/nwis
A detailed summary of the gauge development process is also available online at
http://ga.water.usgs.gov/edu/measureflow.html
The development of a reliable stage-discharge relationship is a difficult and time-consuming
task. The flow rate is measured by measuring the velocity at several lateral locations and depths
along a river cross section near the gauge. Together with the gauge height at the time of
measurement, this represents one point on the stage-discharge relationship. Many points must be
surveyed at different water levels spanning the range from low to high flow in order to produce a
reliable stage-discharge relationship.
Depending on the river cross-section, stage-discharge curves can have several fitted equations
with multiple parameters, but the general building block is a power-law fit of the form
Q = a2 (h h0 )b (1)
where h is the gauge height and Q is the flow rate; h0 is the gauge height when there is no flow
and a2 and b2 are fitting parameters.

Discharge versus stage for the Brazos River at SH 21 near Bryan, Texas.
Because the river cross-section changes over time, new stage-discharge relationships must be
established on a periodic basis. Figure 1 shows a plot of daily flow versus river stage for the
Brazos River measured at SH 21 near Bryan, Texas. Several streaks are evident in the figure;
these are each for a different stage-discharge relationship. The scatter in the plot is a result of
averaging: the data presented here are for the average water depth and average flow rate over one
day. The stage-discharge relationship is used by the USGS on an hourly basis to record the flow
and develop this reported average.
2. Curve-Fitting Program
Recently, acoustic Doppler current profilers (ADCP) allow for the river flow velocity to be
measured instantaneously along a cross-section of a river. In this assignment, we will be using
3
data for a side-looking ADCP that is permanently mounted at a river gauging station. Thus, we
are able to generate a large dataset of discharge versus gauge height. The data are reported in
cubic feet per second (cfs) for discharge and in feet (ft) for gauge height, and the gauge height in
the dataset represents the height above the zero-flow height; hence, h0 in Equation (1) is zero.
First, we must obtain the stage-discharge data and store them on our hard drive for analysis.
Download the function file gaugedata.m from the course website. At the Matlab prompt, run
the commands
[Q, h, a2, b2] = gaugedata;
data = [h Q];
save my_data.dat -ascii data
a2
b2
The first line generates a random set of data for you to analyze. The second line, packs these
data in a single variable so that the first column contains stage in ft and the second column
contains discharge in cfs. The third line saves this data in a file called my_data.dat. The
fourth and fifth lines display the values stored in the fit parameters a2 and b2. Write down these
parameter values for comparison later to your curve fit.
Write a Matlab program to do the following tasks. For the curve fitting, you should write your
own function file. You may base this file on Figure 14.15 in the Chapra (2011) textbook.
• Load in the data and plot the data as points ’.’ (blue dot) in figure(1).
• Fit a power law of the form
2
2
Q = a hb (2)
and report the fitted parameter values a2 and b2. Compare these values to the base values
output in the commands above. Overlay the data in figure(1) with the fitted curve.
• Create a scatter plot in a second figure window that presents the actual error = QQˆ
and the squared error ( )2
2 = QQˆ for each measurement. On the x-axis, plot the
measured gauge height h, and on the y-axis plot the errors. Use the subplot(2,1,1)
command to plot the errors and subplot(2,1,2) command to plot the 2 errors.
Experiment with linear, log, and loglog plots to get the best presentation of the data.
Remember to label your axes including units of the data.
• Calculate the mean and standard deviation of the errors = QQˆ and squared errors
( )2
2 = QQˆ . Report these calculated values with their appropriate units.
• Remove the data points from the dataset that correspond to the top 5 errors = QQˆ .
You should write the code in your program to do this automatically. Repeat the curve fit
4
for this new data set, output the new fit coefficients, and compute the mean and standard
deviation of the and 2 errors. How has the fit improved or gotten worse in
comparison to the base answer recorded above?
• Plot the revised curve fit in figure(1) with the other data and curves. Make the
revised curve thicker by setting LineWidth equal to 2.5 in plot so the revised curve
stands out. Also plot an ’o’ (red circle) on top of the data points that were removed.
Add a legend to the plot and be sure to label your axes. Comment on any improvement
you observe in the fit and explain how the fit has changed.
You may use the built-in Matlab functions min, max, mean, and std, but do not use any other
Matlab functions that replace significant parts of the programming work, such as find,
polyfit, or nlinfit.
3. Memorandum
In the body of your memorandum, include each of the plots generated by your code and report all
of the values computed for the fit parameters and for the error statistics. Also include a general
discussion of the two curve fits and answers to the questions posed in the programming
assignment. Which fit matches the data better? Why?
Include a listing of all of your program codes in the appendix, using the appropriate text to
introduce each component of the program.