Can anyone help me?

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 日

1 投票

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 件のコメント

John D'Errico
John D'Errico 2022 年 1 月 15 日
+1
Sein Lim
Sein Lim 2022 年 1 月 15 日
May I know why you use that code?
Simon Chan
Simon Chan 2022 年 1 月 15 日
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 2022 年 1 月 15 日
編集済み: Torsten 2022 年 1 月 15 日
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 2022 年 1 月 15 日
Thank you everyone.
Torsten
Torsten 2022 年 1 月 15 日
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 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 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2022 年 1 月 15 日

回答済み:

2022 年 1 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by