Info

This question is closed. Reopen it to edit or answer.

Nested if conditions to compute flow

1 view (last 30 days)
Saleem Sarwar
Saleem Sarwar on 2 Oct 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
My dataset includes time series data of rainfall and CN for one year. I am trying to determine the flow for two conditions i.e
if Rainfall > 0.2 then
Runoff = 1000/CN-10
otherwise
Runoff = (Rainfall - 0.2 * (1000/CN -10)^2)/Rainfall + 0.8 *(1000/CN -10).
I have tried following script but it did not work.
idx = Rainfall > 0.2;
Runoff(idx) = 1000/CN - 10;
Runoff(~idx) = (Rainfall - 0.2 * (1000/CN -10)^2)/Rainfall + 0.8 *(1000/CN -10);
Can any body suggest me script?
Regards, Saleem
  1 Comment
per isakson
per isakson on 2 Oct 2015
Edited: per isakson on 2 Oct 2015
"but it did not work" &nbsp What happened? Error or what? &nbsp Rainfall and Runoff how are they defined?

Answers (2)

Eng. Fredius Magige
Eng. Fredius Magige on 2 Oct 2015
Edited: Walter Roberson on 2 Oct 2015
Try this one:
dataset=[]; % copy you three columns of time, rainfall and CN into [], preferable from excel sheet
% note arrangement of you data very important 1st time, 2nd rainfall and 3rd CN
[datarow datacol]=size(dataset)
hold on
for n=1:datarow
Rainfall=data(n,2)
if (Rainfall > 0.2)
Runoff = 1000/(dataset(n,3)-10)
else
Runoff = (Rainfall - 0.2 * (1000/(dataset(n,3)-10))^2)/Rainfall + 0.8 *(1000/(dataset(n,3)-10)).
end
hold off
  2 Comments
Walter Roberson
Walter Roberson on 2 Oct 2015
You would need to assign to Runoff(n) not just to Runoff
Eng. Fredius Magige
Eng. Fredius Magige on 2 Oct 2015
% The previous just after for, the data is edited be dataset. otherwise be specific, inclusive CN %
[datarow datacol]=size(dataset) hold on for n=1:datarow Rainfall=dataset(n,2) if (Rainfall > 0.2) Runoff = 1000/(dataset(n,3)-10) else Runoff = (Rainfall - 0.2 * (1000/(dataset(n,3)-10))^2)/Rainfall + 0.8 *(1000/(dataset(n,3)-10)). end hold off

Thorsten
Thorsten on 2 Oct 2015
Edited: Thorsten on 2 Oct 2015
Does this work for you?
idx = Rainfall > 0.2;
Runoff(idx) = 1000/CN(idx) - 10;
Runoff(~idx) = (Rainfall(~idx) - 0.2 * (1000./CN(~idx)-10).^2)./Rainfall(~idx) + 0.8 *(1000./CN(~idx) -10);
If not, please report exactly what went wrong.
  3 Comments
Saleem Sarwar
Saleem Sarwar on 2 Oct 2015
No, this will not work as I have to compute excess rainfall fulfilling these two conditions 1. if Rainfall - 0.2*S < 0, then excess rainfall will be 0 otherwise excess rainfall = (Rainfall -0.2*s)^2/ Rainfall + 0.8*S) where rainfall and S are daily time series data of years.
Thorsten
Thorsten on 2 Oct 2015
What about this? And do your really mean -0.2*s instead of -0.2*S in the "otherwise" condition?
idx = Rainfall - 0.2*S < 0;
ExcessRainfall(idx) = 0;
ExcessRainfall(~idx) = (Rainfall(~idx) - 0.2*s(~idx)).^2./Rainfall(~idx) + 0.8*S(~idx);

This question is closed.

Community Treasure Hunt

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

Start Hunting!