Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Help with vectorization, instead of loops

1 view (last 30 days)
kyrresc
kyrresc on 31 Mar 2016
Closed: Sabin on 19 Dec 2022
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)

Answers (0)

This question is closed.

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!