Can someone rectify this code?
조회 수: 5 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (4개)
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
2025년 2월 23일
out=[(1:1:10) ' 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
2025년 2월 23일
편집: VBBV
2025년 2월 23일
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
out=[(1:1:10) fsolve(@myfun, Q0)]
댓글 수: 1
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
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
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
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
2025년 2월 27일
Q = [4.08694965230257
0.880101638404846
0.81989836159516
1.4732445577558
0.557067224938124
0.876903042701873
0.596341515053935
1.15598312275933
-0.129944971195848
0.689586578901236];
F = myfun(Q)'
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 Center 및 File Exchange에서 Simulation and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!