MATLAB Help: MATLAB time step function

AI Thread Summary
A user new to MATLAB seeks assistance with implementing a time step function for calculating retained rainwater depth and runoff based on rainfall and evapotranspiration data. The function involves conditional equations that determine the state of retained water (St) and runoff (Rt) based on whether the maximum retention capacity (Smax) is exceeded. The user expresses confusion about the syntax, particularly regarding the use of commas in the equations, which are clarified as indicating conditional statements. Suggestions include breaking down the equations into simpler parts and posting the attempted MATLAB code for better guidance. A sample MATLAB code snippet is provided to illustrate how to loop through the data and perform the calculations, emphasizing the need for clarity on constants and variable data inputs. The discussion highlights the importance of incremental calculations and debugging in MATLAB programming.
TheodoreCT
Messages
2
Reaction score
0
Hi guys I'm new to MATLAB but now i need to use it for my study. I've tried to learn by myself to run it but unfortunately it failed, so I need some guidance on how to run following time step function in MATLAB

St = St-1 + Pt - ETt, St-1 + Pt - ETt ≤ Smax
= Smax, St-1 + Pt - ETt > Smax

then

Rt = 0, St-1 + Pt - ETt ≤ Smax
= Pt - (Smax - St-1) - ETt, St-1 + Pt - ETt > Smax

Pt = rainfall depth
ETt = evapotranspiration rate
S = Retained rainwater depth in substrate
Smax = maximum retention capacity
R = runoff
it is a hourly time step function, hourly rainfall data for a year will be inserted to the function
i would be really appreciated if anyone could help me.
 
Physics news on Phys.org
I have a hard time understanding what your equations are trying to do,( although I think I can guess). Do commas separate equations? If so, what does "=Smax," at the beginning of line 2 mean? It may help if you add some comments and break up the lines into simpler parts. You will want to do that in the MATLAB code anyway. When code doesn't work, try to do calculations in baby steps. You can combine them later into more complicated equations.
[Edit] Oh, I see now that the comma means 'if' Maybe you should post the MATLAB code that you tried to run.
 
FactChecker said:
I have a hard time understanding what your equations are trying to do,( although I think I can guess). Do commas separate equations? If so, what does "=Smax," at the beginning of line 2 mean? It may help if you add some comments and break up the lines into simpler parts. You will want to do that in the MATLAB code anyway. When code doesn't work, try to do calculations in baby steps. You can combine them later into more complicated equations.
[Edit] Oh, I see now that the comma means 'if' Maybe you should post the MATLAB code that you tried to run.

thank you for your kind response, maybe you can refer to the pic below & the function actually looks like this
Function.jpg

i don't know how to explain the commas (sorry I'm not good in explaining things) , but its similar to simple function like this
Rt = (R1, R2, R3...) where R1, R2, R3 is the Rt calculated at every hour.
In this case, Smax = 16mm, Rt and St equations are different before and after it reach Smax
 
Can you show what you tried to do for this so far? At the very least you should give more info about what is constant and what isn't, so that we can better help. Also show some sample data that will be fed in.

It should be rather straightforward to loop through your data and calculate these values once the problem is properly stated.
 
Ok. Here is something to get you started. I am at home and can't test this, so you will probably have to work on it to get it to run correctly. Also, you may have to change the indices of P and ET to get everything lined up correctly.

Code:
% find out how much data there is
[num_Pts, temp] = size(P);

% set initial values for t=1;
S(1) = 0;  % initial amount in the substrate
P(1) = 0;
ET(1) = 0;

% loop through the data and do the calculations
for t=(2:num_Pts)
   % calculate total  for time t as though there is no limit
   before_limiting = S(t-1) + P(t) - ET(t);

   % if the limit is not exceeded, it is all retained in S and there is no runoff R
   if before_limiting <= Smax
      S(t) = before_limiting;
      R(t) = 0;
   else
   % if the limit is exceeded, the max is retained in S and the rest is runoff
      S(t) = Smax;
      R(t) = before_limiting - Smax;
   end
end
 

Similar threads

Replies
5
Views
1K
Replies
6
Views
6K
Replies
11
Views
3K
Replies
9
Views
187K
Replies
2
Views
4K
Replies
7
Views
2K
Back
Top