MATLAB Answers

0

Calculate slope at specific time over many days using polyfit and for loop

Asked by Benjamin Downing on 14 Sep 2019
Latest activity Commented on by Star Strider
on 14 Sep 2019
I have a data set that show daily fluctuation in groundwater. I need to find the slope of the hydrograph every day between the hours of 4 and 12. I have tested this loop with other operators and they are working on the correct intervals. the hours 12 to 4 correspond to the first 17 rows of every 96 rows.
How do I correctly use polyfit to calculate slope during the assigned intervals?
The error I am recieving is as follows:
Unable to perform assignment because the indices on the left side are not compatible with the size of
the right side.
Error in ETg_toy (line 105)
tR(h)=polyfit(subset_day,subset_depth,1)
Below is the code I am trying to use:
load('GroundwaterDataA');
A = GroundwaterDataA;
depth=flip(A(:,2))
day=flip(A(:,1))
for h=1:200 %day day 200 = day 199
start2=(h*96)-15;
endp2=h*96;
subset_depth=depth(start2:endp2)
subset_day=day(start2:endp2)
tR(h)=polyfit(subset_day,subset_depth,1)
pause
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Star Strider
on 14 Sep 2019
 Accepted Answer

You appear to be calculating them correctly, just not storing them correctly. For a linear fit, polyfit will produce a (1x2) vector of the coefficients.
Try this:
tR(h,:)=polyfit(subset_day,subset_depth,1)
Note the added dimension.

  2 Comments

That solved the issue. Thank you for your help!

Sign in to comment.