Nested Loops Not Changing with Time
2 次查看(过去 30 天)
显示 更早的评论
I am working on a problem that simulates flow break up. My code is supposed to show how the fluid breaks up over time. I have run into the issue that the flow doesn't change with time. I was wondering if a new set of eyes could help me figure out why this is happening.
for n=1:10000000
for j=2:N-2
un(j)=uo(j)+dt*S(j)...
+3*dt*mu/(rho*favg(j))*(f(j+1)-f(j))/dN*(uo(j+1)-uo(j-1))/(2*dN)...
+3*dt*mu/rho*(uo(j+1)-2*uo(j)+uo(j-1))/dN^2 ...
-dt*uo(j)*(uo(j+1)-uo(j-1))/(2*dN);
un(1)=-un(2); %symmetric BC
un(N-1)=-un(N-2); %symmetric BC
uo(j)=un(j);
fn(j)=f(j)-dt*f(j)*((uo(j)-uo(j-1))/dN)-dt*uo(j)*((f(j+1)-f(j-1))/(2*dN));
hn(j)=(fn(j)^0.5);
f(j)=fn(j);
h(j)=hn(j);
end
hmin=min(h);
if hmin<bkthd
break
end
Time=n*dt;
figure(1)
plot(h)
hold on
plot(-h)
title(['Time=', num2str(Time), ''])
end
Thanks in advance!
0 个评论
回答(1 个)
the cyclist
2019-11-19
Nothing in your inner for loop depends on n. Things depend on N. So it looks like that inner loop just does the same calculation over and over again.
3 个评论
the cyclist
2019-11-20
Your code is too complex for me to just look at it and figure out what you intend, or what might be going wrong. If you want to upload your full code, such that we could actually run it, that would help.
I can tell you some things I would do myself, to try to debug.
Separate the different segments visually, and write comments for each section (or possibly each line) so that it is very clear what it is supposed to be doing, e.g. ...
% Describe the H calculation
H(n,j)=0.25*((f(n,j).*(2-fzz(n,j))+(fz(n,j)).^2)./(f(n,j)+0.25*(fz(n,j)).^2).^(3/2));
H(n,1)=0.25*((f(n,1).*(2-fzz(n,1))+(fz(n,1)).^2)./(f(n,1)+0.25*(fz(n,1)).^2).^(3/2));
H(n,N+1)=0.25*((f(n,N+1).*(2-fzz(n,N+1))+(fz(n,N+1)).^2)./(f(n,N+1)+0.25*(fz(n,N+1)).^2).^(3/2));
% Describe the S calculation
S(n,j)=-2*sigma/rho*(H(n,j+1)-H(n,j))/dN;
S(n,1)=-2*sigma/rho*(H(n,2)-H(n,1))/dN;
% Describe the h calculation
h(n,j)=(f(n,j).^0.5);
Perhaps make more descriptive variable names. (Although maybe these are obvious to you.)
Pull lines like these:
uo(n,1)=un(n+1,1);
uo(n,N+1)=un(n+1,N+1);
outside of the loop over j, since they have no dependence on j. (I wonder if this is part of the problem.)
Use the debugger to stop the code at the very first line of the for loop, to see if what you get what you expect, rather than waiting until the entire loop is done to check the final results.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!