while loop in a for loop
Mostra commenti meno recenti
hello. i am trying to plot a graph of ut (terminal velocity) against D (diameter) and my code is as below. i can plot the data but somehow the graph is not what i expected it to be. can someone help me? thank you in advance!
% m-file to plot ut vs D
D=0:1e-3:5e-3;
ut=miniclasstest1920_i(D);
plot(D,ut);
[D(1) ut(1)];
[D(end) ut(end)];
ut1=miniclasstest1920_i(1e-5);
ut2=miniclasstest1920_i(5e-3);
[ut1 ut2]
% function miniclasstest1920_i
function ut=f(D)
rho_s=2350;
rho=1100;mu=2e-3;
g=9.81;
LD=length(D);
ut=zeros(LD,1);
for n=1:LD
u=zeros(10,1);
Re=zeros(10,1);
u(1)=1e-5;
Re(1)=(rho*u(1).*D(n))/mu;
err(1)=abs(Re(1)-0);
i=1;
while err(i)>1e-6
if Re(i)<0.2
u(i+1)=((g*D(n).^2)*(rho_s-rho))/(18*mu);
Re(i+1)=(rho*u(i+1)*D(n))/mu;
elseif 500<Re(i)<2e5
u(i+1)=sqrt((3.*D(n)*g*(rho_s-rho))/rho);
Re(i+1)=(rho*u(i+1)*D(n))/mu;
else
disp('odd')
end
i=i+1;
err(i)=abs(Re(i)-Re(i-1));
end
end
u=u(1:i-1);
ut(n)=u(end);
end
6 Commenti
Jan
il 15 Apr 2021
How can we help you based on the information "somehow the graph is not what i expected it to be"? We do not know, what you expect.
Mathieu NOE
il 15 Apr 2021
hello
code fixed !! the output of the function was incorrect (wrong index and not at the right position in the loop structure)
% m-file to plot ut vs D
D=0:1e-3:5e-3;
ut=f(D);
plot(D,ut);
function ut=f(D)
rho_s=2350;
rho=1100;
mu=2e-3;
g=9.81;
LD=length(D);
ut=zeros(LD,1);
for n=1:LD
u=zeros(10,1);
Re=zeros(10,1);
u(1)=1e-5;
Re(1)=(rho*u(1).*D(n))/mu;
err(1)=abs(Re(1)-0);
ci=1;
while err(ci)>1e-6
if Re(ci)<0.2
u(ci+1)=((g*D(n).^2)*(rho_s-rho))/(18*mu);
Re(ci+1)=(rho*u(ci+1)*D(n))/mu;
elseif 500<Re(ci)<2e5
u(ci+1)=sqrt((3.*D(n)*g*(rho_s-rho))/rho);
Re(ci+1)=(rho*u(ci+1)*D(n))/mu;
else
disp('odd')
ci
end
ci=ci+1;
err(ci)=abs(Re(ci)-Re(ci-1));
end
ut(n)=u(ci) ; % and not u(end)
end
end
Siti Jamilah Binti Mohd Yatim
il 15 Apr 2021
Mathieu NOE
il 16 Apr 2021
Glad I could help
have a good day
Jan
il 16 Apr 2021
@Mathieu NOE: If you post this as an answer, it can be accepted.
Mathieu NOE
il 19 Apr 2021
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!