creating a loop and functions
이전 댓글 표시
Hi,
I'm trying to perform a simple model but can't seem to create a loop. The issue is that I use known values (ho and hl) to solve the equation.
Then I need to use a data set of (ho) consisting of a column of 298 into the equation while the (hl) is from the previous iteration.
here is my code, I'm using a (for loop), but the problem is that it produces the same value of the intitial (hl) for all 298 outputs. Any ideas what is wrong?
Al=36000;
Qr=31.5;
Ai=3.14*(150/2)^1/2;
Time=table2array(wl(:,1));
wl_sea=table2array(wl(:,3));
wl_lagoon=table2array(wl(:,4));
ho=20.62949/100;
deltat=0;
hl=23.908655/100;
kf=0.3;
% the first step is to use the first known lagoon water level to calculate
% the next level using the sea water level as input
hl0=hl+(deltat*(Ai/Al))*((2*9.81)^0.5/kf)*((ho-hl)/(abs(ho-hl)^0.5)+(deltat*(Qr/Al)))
ho=wl_sea;
for i=1:1:298
hi = hl0;
h(i)=hi;
hi = hi+(deltat*(Ai/Al))*((2*9.81)^0.5/kf)*((ho-hi)/(abs(ho-hi)^0.5)+(deltat*(Qr/Al)))
end
댓글 수: 6
Star Strider
2019년 10월 10일
The ‘hl0’ variable is a scalar that never changes (created outside the loop). In each iteration of the loop, you overwrite ‘hi’ with that same value, then store it as ‘h’. So nothing ever changes.
You need to solve that, since I have no idea what you want to do.
Tarek Zaqout
2019년 10월 10일
Star Strider
2019년 10월 10일
Another problem that I just now noticed is that ‘deltat’ is 0, so that makes the entire rest of the expression (to the right of it) equal to 0. If you make ‘deltat’ something other than 0, ‘h’ changes.
Tarek Zaqout
2019년 10월 10일
Tarek Zaqout
2019년 10월 10일
Peter Perkins
2019년 10월 15일
Loop issues aside, you definitely can make this (and the other two lines like it)
Time = table2array(wl(:,1))
simpler:
Time = wl.Time % or whatever the var name is
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!