How to decrease index of an vector on every time step?

This seems like a fairly straight forward thing to do, however it has been giving me all sorts of trouble.
I am working on a geophysical model, and part of that involves a rainfall rate, and once surface temperatures gets to 100C, subsequent rainfall will accumulate as a reservoir. However, a fraction of this will still evaporate each timestep.
So as you can see below, during times of rainfall, the surface can reach 100C. I to plot another variable however, to show the water that has accumulated in on the surface.
The maths is a little more complex, but lets say 1% is evaporated each timestep, I simply want the reservoir to decrease by 1% each step.
I think the main issue I am having is that the water can only begin to accumulate on the surface when it has reached 100C, continues to accumulate until the rain stops and as soon as the rain stops the accummulated water value will begin to decrease. But the way I have it coded, as soon as the rain stops, the temperature increases and the water in the reservoir isn't considered.
What I am looking for, would be a plot similar in shape to the 'Total Rain' plot, but that begins to decrease once T rises, not remains constant until it rains again.
Here is the code I have:
for n = timesteps
if T(n,1)>373
T(n+1,1) = equation1; % surface temp
else
T(n+1,1) = equation2; % surface temp = 373K
rainTotalAccum = cumsum(rain);
if rainTotalAccum(n)>0
T(n+1,1) = equation2; % To try and stop T increasing once rain stops if there is surface water present
rTotalAccum(n) = rTotalAccum(n) - rTotalAccum(n)*0.01; % 1% of rain evaporated each timestep
end
end
end
What I need is a way to decrease the value of rainTotalAccum each timestep, keeping the temperature at 373K, but everything I have tried so far has failed. I'd really appreciate any advice on this.

 采纳的回答

I don't have all your code so I can't be sure this is the problem but:
rTotalAccum(n) = rTotalAccum(n) - rTotalAccum(n)*0.01;
Shouldn't this be
rTotalAccum(n+1) = rTotalAccum(n) - rTotalAccum(n)*0.01;
Also you are running
T(n+1,1) = equation2;
twice and I don't think that is intentional.

3 个评论

That is correct, that at least fixes the decreasing reservoir part. As for the temperature part, the issue is the surface temp is dependant on rainfall, once it rains, there is a huge evaporative flux which cools the surface to impossible values, so any temperature below 373, is actually set equal to 373 (which is what equation2 does).
So once the rain stops, the huge evaporative flux in equation1 also stops and temperature begins to rise again. So I am trying to 'artificially' keep T=373 while there is still water on the surface, which is why I used the equation2 insdie the second if statement. However this doesn't work, it seems to never go above 373 once it gets to this line.
How about a format like this?
time = linspace(0,25,25*100+1); % arbitrarily chosen time vector
rain = somerandomfunction(time); % vector of rainfall at time
rainTotalAccum = nan(size(time)); %initial
rainTotalAccum(1) = 0; % initial accumulation of rain
T = nan(size(time));
T(1) = 210; % initial temperature
for n = 1:numel(time)-1
if rain(n) > 0
rainTotalAccum(n+1) = rainTotalAccum(n) + rain(n);
else
rainTotalAccum(n+1) = rainTotalAccum(n) - rainTotalAccum(n)*0.01;
end
if rainTotalAccum(n) > 0
T(n+1,1) = equation2;
else
T(n+1,1) = equation1;
end
end
The reason I chose format like this is because whether you should choose equation 2 or 1 should matter on accumulated rain, not on temperature.
Also, cumsum function adds up all of the rain that fell (even the evaporated ones), so I don't think that represents the physical phenomenon well.
Aye that's got it working just fine, lots of holes in it, but that is a physics problem not a programming one, in terms of MATLAB, problem is fixed. Many thanks for that!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by