Can anyone help me?

2 views (last 30 days)
Sein Lim
Sein Lim on 15 Jan 2022
Answered: Image Analyst on 15 Jan 2022
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?

Answers (2)

Simon Chan
Simon Chan on 15 Jan 2022
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 Comments
Simon Chan
Simon Chan on 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 on 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.

Sign in to comment.


Image Analyst
Image Analyst on 15 Jan 2022
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!