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

2 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 14 Sep 2019
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.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!