Can someone rectify this code?

1 次查看(过去 30 天)
Rajeev
Rajeev 2025-2-23
评论: Sam Chak 2025-2-27
function F = myfun(Q)
ks = 0.015/12; % ks of pipe in ft
D = [8/12 6/12 8/12 8/12 6/12 6/12 6/12 8/12 6/12 6/12]; % Diameter of pipes in ft (10 values)
L = [1000 2000 2000 1000 1000 1000 2000 1000 2500 2500]; % Lengths of pipes in ft (10 values)
nu = 1.06*(10^-5); % kinematic viscosity in ft^2/s
Re(:) = 4*(abs(Q(:)))./(pi.*nu.*D(:)); % Reynolds number from assumed flow
f(:)= 0.25./ (log10((ks./(3.7*D(:)))+(5.74./(Re(:).^ 0.9)))).^2; % Swamee-Jain friction factor equation
K(:) = (8 .*f(:) .*L(:))./(pi.*pi.*32.2*D(:).^5);
hp = (42.5-10.4*(Q(10)^2)+2.8*abs(Q(10)))*(Q(10)/abs(Q(10))); % Pump head equation
% System of equations
F(1) = -2.2 + Q(1) - Q(2) - Q(6) + Q(9);
F(2) = Q(2) + Q(3) - 1.7;
F(3) = Q(4) - Q(3) + Q(5) + Q(10) - 1.9;
F(4) = Q(6) - Q(4) + Q(7);
F(5) = - Q(9) - Q(7) - Q(10) + Q(8);
F(6) = K(2) * Q(2) * abs(Q(2)) - K(3) * Q(3) * abs(Q(3)) - K(6) * Q(6) * abs(Q(6)) - K(4) * Q(4) * abs(Q(4));
F(7) = hp + K(8) * Q(8) * abs(Q(8)) + K(9) * Q(9) * abs(Q(9)) - K(1) * Q(1) * abs(Q(1)) + 30;
F(8) = hp - K(8) * Q(8) * abs(Q(8)) - K(10) * Q(10) * abs(Q(10)) + K(5) * Q(5) * abs(Q(5)) - 15;
F(9) = K(6) * Q(6) * abs(Q(6)) - K(7) * Q(7) * abs(Q(7)) + K(9) * Q(9) * abs(Q(9));
F(10) = K(4) * Q(4) * abs(Q(4)) + K(7) * Q(7) * abs(Q(7)) - K(10) * Q(10) * abs(Q(10));
end
Call
clc
clear all
Q0=[1 1 1 1 1 1 1 1 1]; out=[(1:1:10) ' fsolve(@myfun, Q0) '];
Output is displayed alongside:
function F = myfun(Q)
Error: Function definitions are not supported in this context. Functions can only be created as local or nested functions in code files.

回答(4 个)

Image Analyst
Image Analyst 2025-2-23
When you do this:
out=[(1:1:10) ' fsolve(@myfun, Q0) ']
it's trying to take a vector [1,2,3,4,5,6,7,8,9,10] and append a string ' fsolve(@myfun, Q0) '. It's a string because it's inside single quotes. You can't append a string to a double.
Also, I suspect in myfun.m that there is some lines of code before the "function F = myfun(Q)" line of code.
  1 个评论
Walter Roberson
Walter Roberson 2025-2-23
out=[(1:1:10) ' fsolve(@myfun, Q0) ']
out =
'□□□□□□ fsolve(@myfun, Q0) '
That "works". It converts the 1:10 to unicode positions char(1) through char(10), and the appends the fsolve string. char(10) happens to be newline, so the output display is split across two lines.

请先登录,再进行评论。


VBBV
VBBV 2025-2-23
编辑:VBBV 2025-2-23
@Rajeev Check the size of vectors used for conditions inside fsolve i.e. Q0 and (1:1:10)
function F = myfun(Q)
ks = 0.015/12; % ks of pipe in ft
D = [8/12 6/12 8/12 8/12 6/12 6/12 6/12 8/12 6/12 6/12]; % Diameter of pipes in ft (10 values)
L = [1000 2000 2000 1000 1000 1000 2000 1000 2500 2500]; % Lengths of pipes in ft (10 values)
nu = 1.06*(10^-5); % kinematic viscosity in ft^2/s
Re(:) = 4*(abs(Q(:)))./(pi.*nu.*D(:)); % Reynolds number from assumed flow
f(:)= 0.25./ (log10((ks./(3.7*D(:)))+(5.74./(Re(:).^ 0.9)))).^2; % Swamee-Jain friction factor equation
K(:) = (8 .*f(:) .*L(:))./(pi.*pi.*32.2*D(:).^5);
hp = (42.5-10.4*(Q(10)^2)+2.8*abs(Q(10)))*(Q(10)/abs(Q(10))); % Pump head equation
% System of equations
F(1) = -2.2 + Q(1) - Q(2) - Q(6) + Q(9);
F(2) = Q(2) + Q(3) - 1.7;
F(3) = Q(4) - Q(3) + Q(5) + Q(10) - 1.9;
F(4) = Q(6) - Q(4) + Q(7);
F(5) = - Q(9) - Q(7) - Q(10) + Q(8);
F(6) = K(2) * Q(2) * abs(Q(2)) - K(3) * Q(3) * abs(Q(3)) - K(6) * Q(6) * abs(Q(6)) - K(4) * Q(4) * abs(Q(4));
F(7) = hp + K(8) * Q(8) * abs(Q(8)) + K(9) * Q(9) * abs(Q(9)) - K(1) * Q(1) * abs(Q(1)) + 30;
F(8) = hp - K(8) * Q(8) * abs(Q(8)) - K(10) * Q(10) * abs(Q(10)) + K(5) * Q(5) * abs(Q(5)) - 15;
F(9) = K(6) * Q(6) * abs(Q(6)) - K(7) * Q(7) * abs(Q(7)) + K(9) * Q(9) * abs(Q(9));
F(10) = K(4) * Q(4) * abs(Q(4)) + K(7) * Q(7) * abs(Q(7)) - K(10) * Q(10) * abs(Q(10));
end
clc
clear all
Q0=[1 1 1 1 1 1 1 1 1 1] % check the size of this vector
Q0 = 1×10
1 1 1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
out=[(1:1:10) fsolve(@myfun, Q0)]
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
out = 1×20
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 4.0869 0.8801 0.8199 1.4732 0.5571 0.8769 0.5963 1.1560 -0.1299 0.6896
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 个评论
VBBV
VBBV 2025-2-23

You are probably running a different version of Matlab as torsten mentioned where the function definitions are not supported before the call to function. You can instead put the entire function code myfun after fsolve call. Note that number of equations inside the myfun and number of conditions to passed to fsolve are not same as mentioned in my previous comment.

请先登录,再进行评论。


Torsten
Torsten 2025-2-23
编辑:Torsten 2025-2-23
My guess is that you work with an older MATLAB version where script and function parts cannot be mixed. Thus save
function F = myfun(Q)
ks = 0.015/12; % ks of pipe in ft
D = [8/12 6/12 8/12 8/12 6/12 6/12 6/12 8/12 6/12 6/12]; % Diameter of pipes in ft (10 values)
L = [1000 2000 2000 1000 1000 1000 2000 1000 2500 2500]; % Lengths of pipes in ft (10 values)
nu = 1.06*(10^-5); % kinematic viscosity in ft^2/s
Re(:) = 4*(abs(Q(:)))./(pi.*nu.*D(:)); % Reynolds number from assumed flow
f(:)= 0.25./ (log10((ks./(3.7*D(:)))+(5.74./(Re(:).^ 0.9)))).^2; % Swamee-Jain friction factor equation
K(:) = (8 .*f(:) .*L(:))./(pi.*pi.*32.2*D(:).^5);
hp = (42.5-10.4*(Q(10)^2)+2.8*abs(Q(10)))*(Q(10)/abs(Q(10))); % Pump head equation
% System of equations
F(1) = -2.2 + Q(1) - Q(2) - Q(6) + Q(9);
F(2) = Q(2) + Q(3) - 1.7;
F(3) = Q(4) - Q(3) + Q(5) + Q(10) - 1.9;
F(4) = Q(6) - Q(4) + Q(7);
F(5) = - Q(9) - Q(7) - Q(10) + Q(8);
F(6) = K(2) * Q(2) * abs(Q(2)) - K(3) * Q(3) * abs(Q(3)) - K(6) * Q(6) * abs(Q(6)) - K(4) * Q(4) * abs(Q(4));
F(7) = hp + K(8) * Q(8) * abs(Q(8)) + K(9) * Q(9) * abs(Q(9)) - K(1) * Q(1) * abs(Q(1)) + 30;
F(8) = hp - K(8) * Q(8) * abs(Q(8)) - K(10) * Q(10) * abs(Q(10)) + K(5) * Q(5) * abs(Q(5)) - 15;
F(9) = K(6) * Q(6) * abs(Q(6)) - K(7) * Q(7) * abs(Q(7)) + K(9) * Q(9) * abs(Q(9));
F(10) = K(4) * Q(4) * abs(Q(4)) + K(7) * Q(7) * abs(Q(7)) - K(10) * Q(10) * abs(Q(10));
end
as "myfun.m" and save
clc
clear all
Q0=[1 1 1 1 1 1 1 1 1]; out=[(1:1:10) ' fsolve(@myfun, Q0) '];
as "script.m" in your working directory.
And - as @VBBV noticed - change the size of Q0 to a 10-element vector instead of a 9-element vector.
Then open "script.m" in the MATLAB editor and click on the green RUN button.
  1 个评论
Walter Roberson
Walter Roberson 2025-2-23
In particular R2015b was the first version that permitted mixing script first and then function.
Quite recently, MATLAB also permits code to be mixed as function first and then script

请先登录,再进行评论。


Alex Sha
Alex Sha 2025-2-27
Not sure if I got it right,is the result looks like below?
q1: 4.08694965230257
q2: 0.880101638404846
q3: 0.81989836159516
q4: 1.4732445577558
q5: 0.557067224938124
q6: 0.876903042701873
q7: 0.596341515053935
q8: 1.15598312275933
q9: -0.129944971195848
q10: 0.689586578901236
  1 个评论
Sam Chak
Sam Chak 2025-2-27
Okay, @Alex Sha, let me check.
Q = [4.08694965230257
0.880101638404846
0.81989836159516
1.4732445577558
0.557067224938124
0.876903042701873
0.596341515053935
1.15598312275933
-0.129944971195848
0.689586578901236];
F = myfun(Q)'
F = 10×1
1.0e-12 * 0.0026 0.0060 0 0.0080 0.0071 0.0018 -0.1563 0.0036 -0.0214 0.0142
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function F = myfun(Q)
ks = 0.015/12; % ks of pipe in ft
D = [8/12 6/12 8/12 8/12 6/12 6/12 6/12 8/12 6/12 6/12]; % Diameter of pipes in ft (10 values)
L = [1000 2000 2000 1000 1000 1000 2000 1000 2500 2500]; % Lengths of pipes in ft (10 values)
nu = 1.06*(10^-5); % kinematic viscosity in ft^2/s
Re(:) = 4*(abs(Q(:)))./(pi.*nu.*D(:)); % Reynolds number from assumed flow
f(:) = 0.25./ (log10((ks./(3.7*D(:)))+(5.74./(Re(:).^ 0.9)))).^2; % Swamee-Jain friction factor equation
K(:) = (8 .*f(:) .*L(:))./(pi.*pi.*32.2*D(:).^5);
hp = (42.5-10.4*(Q(10)^2)+2.8*abs(Q(10)))*(Q(10)/abs(Q(10))); % Pump head equation
% System of equations
F(1) = -2.2 + Q(1) - Q(2) - Q(6) + Q(9);
F(2) = Q(2) + Q(3) - 1.7;
F(3) = Q(4) - Q(3) + Q(5) + Q(10) - 1.9;
F(4) = Q(6) - Q(4) + Q(7);
F(5) = - Q(9) - Q(7) - Q(10) + Q(8);
F(6) = K(2) * Q(2) * abs(Q(2)) - K(3) * Q(3) * abs(Q(3)) - K(6) * Q(6) * abs(Q(6)) - K(4) * Q(4) * abs(Q(4));
F(7) = hp + K(8) * Q(8) * abs(Q(8)) + K(9) * Q(9) * abs(Q(9)) - K(1) * Q(1) * abs(Q(1)) + 30;
F(8) = hp - K(8) * Q(8) * abs(Q(8)) - K(10) * Q(10) * abs(Q(10)) + K(5) * Q(5) * abs(Q(5)) - 15;
F(9) = K(6) * Q(6) * abs(Q(6)) - K(7) * Q(7) * abs(Q(7)) + K(9) * Q(9) * abs(Q(9));
F(10) = K(4) * Q(4) * abs(Q(4)) + K(7) * Q(7) * abs(Q(7)) - K(10) * Q(10) * abs(Q(10));
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulation and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by