Info

此问题已关闭。 请重新打开它进行编辑或回答。

Help with vectorization, instead of loops

1 次查看(过去 30 天)
kyrresc
kyrresc 2016-3-31
关闭: Sabin 2022-12-19
I've just recently started reading up on vectorization, and I wondered if anyone could give me some advice on how I could vectorize this code: NB: Instead of the outer while-loop I could've used a for loop from 1:dt:1000, which is why I want to vectorize it. Thanks in advance!!
while t < 1000
Z = 0.5;
Kz = (rho*kt*A*pin)/(400*Mh*Gm*fh^2*H*Kh^2);
fz = Z + log(1-Z) + Kz;
dfz = 1 - (1/(1-Z));
while abs(fz/dfz) >= 10^-4
fz = Z + log(1-Z) + Kz;
dfz = 1 - (1/(1-Z));
Z = Z - fz/dfz;
end %while
%
dhdt = @(H) (- (100*Mh)/M *((2*H^2*fh^2*Gm*Z^2)/pin + (kts*As*rho*(H-Hv))/(100*Mh)));
F1 = dt*dhdt(H);
F2 = dt*dhdt(H+1/2*F1);
F3 = dt*dhdt(H+1/2*F2);
F4 = dt*dhdt(H+F3);
X(i) = H;
tvec(i) = t;
zvec(i) = Z;
H = H + 1/6*(F1+2*F2+2*F3+F4);
t = t + dt;
i = i + 1;
end %while
plot(tvec,X);
figure
plot(tvec,zvec)

回答(0 个)

此问题已关闭。

标签

尚未输入任何标签。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by