The one I want to see could not appear.
function [u, Re] = Q2i(Q)
d = 5
u =Q*0.000001/(pi*(d*0.001)^2/4)
end
This my function script.
clear all;clc;close
P = [0.62 0.94 1.13 1.72 2.34 2.7 2.98 4.05 4.54 5.32 5.98 6.12 6.73 7.78 8.95 9.91 10.25 11.3 12.44]
Q = [3.87 4.49 5.26 5.43 6.02 6.44 6.87 7.55 7.73 8.42 8.75 10.32 10.79 11.32 14.13 15.66 16.06 17.98 19.11]
d = 5
density = 789
viscosity = 0.0012318
u = Q2i(Q)
Re = (density * u * (d*0.001)) / viscosity
a = num2str(Re)
for k = length(Q)
if 2300 > a
fl = 8./a
else
ft = 0.0396 * a.^-0.25
end
end
This is my working script.
The thing is that 'fl' did not show in the working space. May I know what mistakes I have made?

Réponses (2)

Simon Chan
Simon Chan le 15 Jan 2022

1 vote

Varaible a becomes a character array after you use function num2str, so it is comparing a 'character' instead of a number.
On the other hand, index k is not used inside the for loop.
Actually in your case, for loop is not requried and just use the following:
fl = Re(2300>Re);
ft = 0.0396 * Re(Re>=2300).^-0.25;

8 commentaires

John D'Errico
John D'Errico le 15 Jan 2022
+1
Sein Lim
Sein Lim le 15 Jan 2022
May I know why you use that code?
Simon Chan
Simon Chan le 15 Jan 2022
Sorry for typo error. Variable fl should be calculated as follows:
fl = 8./Re(2300>Re)
The comparisons (2300>Re) and (Re>=2300) calculates the logical indexes which satisy their condition and hence for-loop is not required.
Torsten
Torsten le 15 Jan 2022
Modifié(e) : Torsten le 15 Jan 2022
function main
P = [0.62 0.94 1.13 1.72 2.34 2.7 2.98 4.05 4.54 5.32 5.98 6.12 6.73 7.78 8.95 9.91 10.25 11.3 12.44];
Q = [3.87 4.49 5.26 5.43 6.02 6.44 6.87 7.55 7.73 8.42 8.75 10.32 10.79 11.32 14.13 15.66 16.06 17.98 19.11];
d = 5;
density = 789;
viscosity = 0.0012318;
u = Q2i(Q,d)
Re = (density * u * (d*0.001)) / viscosity
f = zeros(size(Q));
for i = 1:numel(Re)
if 2300 > Re(i)
f(i) = 8/Re(i)
else
f(i) = 0.0396 * Re(i)^-0.25
end
end
end
function u = Q2i(Q,d)
u = Q*0.000001/(pi*(d*0.001)^2/4)
end
Sein Lim
Sein Lim le 15 Jan 2022
Thank you everyone.
Torsten
Torsten le 15 Jan 2022
The comparisons (2300>Re) and (Re>=2300) calculates the logical indexes which satisy their condition and hence for-loop is not required.
I doubt that the OP really wanted to cut the correspondence to the Re array by splitting the flow resistance parameter in an array for laminar regime (fl) and an array for turbulent regime (ft). Maybe there is an indexing solution also for the case where the results (dependent on the flow regime) are written in one array f which in length corresponds to Re, but - to be honest - I still find it more transparent with an if statement within a for-loop. I'm aware that convinced MATLABers will disagree.
Simon Chan
Simon Chan le 15 Jan 2022
Thanks for your comment.
The following code uses NaN as the result when the data is not valid to the specific group.
idx.fl = 2300>Re; % Index for laminar regime (fl)
fl = NaN(1,length(Q)); % Initialize fl
ft = NaN(1,length(Q)); % Initialize ft
fl(idx.fl) = 8./Re(idx.fl); % fl stores its valid data (belongs to idx.fl)
ft(~idx.fl) = 0.0396 *Re(~idx.fl).^-0.25; % ft stores its valid data (belongs to ~idx.fl)
Torsten
Torsten le 15 Jan 2022
And for one array only:
idx.fl = 2300>Re; % Index for laminar regime (fl)
f = zeros(1,length(Q)); % Initialize f
f(idx.fl) = 8./Re(idx.fl); % f stores laminar flow resistance parameters (belongs to idx.fl)
f(~idx.fl) = 0.0396 *Re(~idx.fl).^-0.25; % f stores turbulent flow resistance parameters (belongs to ~idx.fl)
Yes, it's nice.

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 15 Jan 2022

0 votes

Because you have this:
if 2300 > a
fl = 8./a
else
ft = 0.0396 * a.^-0.25
end
and because you never initialized fl before the loop, and because fl never showed up, that means you never entered the top part of your if block, meaning that "a" was never less than 2300.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by