Can anyone help me?
조회 수: 1 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (2개)
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
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
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
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Financial Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!