# How to prevent decrement (melting) past 0 until growth increases again?

1 ビュー (過去 30 日間)
Siân Chilcott 2020 年 8 月 7 日
コメント済み: Siân Chilcott 2020 年 8 月 12 日
I am creating a snow and ice height time series. Once the snow (hsnow) has completely melted, the energy is used to melt ice (hice). I may be being stupid but I can't seem to find a way to prevent the snow height falling below 0 (as the energy that causes it to decrease past 0 would be used to decrease an ice layer).
Basically once the snow height falls below 0 I want to enforce it at 0 or prevent further decrement past 0, until the snowfall increases again.
hsnow(day+1) = hsnow(day) + snowfall(day) + dh_snow;
h_ice(day+1) = hice(day) + growth(day) + dh_ice;
(dh_snow and dh_ice are the amount of ice and snow melted per day based on the energy available.)
Thanks in advance for any help!

サインインしてコメントする。

### 採用された回答

Image Analyst 2020 年 8 月 7 日
Another way, inside the loop, is to use the max() function:
hsnow(day+1) = max([0, hsnow(day) + snowfall(day) + dh_snow]);
h_ice(day+1) = max([0, hice(day) + growth(day) + dh_ice]);
If either one would be negative, like -5, then it will clip to 0 because the max of -5 and 0 is 0.

#### 10 件のコメント

Siân Chilcott 2020 年 8 月 11 日
I want to fit the polynomial to T_s. When I run it for 1 year it looks like the figure below (i've enforced it at 0 for melt reasons). I receive the error I explained earlier when I try and run it for more than one year. I would just like to know why it produces no real roots on day 615.
.
Image Analyst 2020 年 8 月 11 日
For me it crashes at day 241. Try running the attached script.
Now let's say that I took the values of a, b, c, d, and e when it crashes and extend the days to 500. It would look like the plot in the lower left:
You can see that the 4th order equation has a min of 2.41 which means that it never crosses the x axis and has no real roots. So your code will just have to take that into account.
Siân Chilcott 2020 年 8 月 12 日
Ah that makes sense - thanks so much for your help!

サインインしてコメントする。

### その他の回答 (1 件)

Sudheer Bhimireddy 2020 年 8 月 7 日
As soon as you estimated hsnow, force all/any negative values to 0
hsnow(hsnow<0)=0;

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by