Can anyone help me?

3 次查看(过去 30 天)
Sein Lim
Sein Lim 2022-1-15
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?

回答(2 个)

Simon Chan
Simon Chan 2022-1-15
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 个评论
Simon Chan
Simon Chan 2022-1-15
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 2022-1-15
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.

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by