% Laboratory in Oceanography - Data and Methods % SMAST, UMass Dartmouth % % Code for generating plot of example of system of equations - this case % stage vs. flow rating curve, with observations at different times. % % Written by Miles A. Sundermeyer, 1/27/09 % set the variables time = [0 14 28 42 56 70]; stage = [0.6120 0.6470 0.5800 0.6290 0.6880 0.5830]; flow = [0.330 0.395 0.241 0.338 0.531 0.279]; % create a figure window, figure number 1 figure(1) clf plot(stage,flow,'b*') xlabel('stage(m)') ylabel('flow (m^3s)') title('Stage vs. Discharge Rating Curve') % fit line to data % create X matrix for linear fit X = [stage' ones(size(time'))]; B = X\flow'; % left divide y = Xb by X to solve for B=[m b]' % (need to transpose stage to column vector first) % now B contains both the slope, m, and intercept, b stagefit = [min(stage) max(stage)]; hold on plot(stagefit,[stagefit' ones(size(stagefit'))]*B,'r-'); % print the figure to a postscript file print -deps2c stage_example_1 % replace ‘-deps2c’ with ‘-djpeg’ for jpeg file