There is a function that solve a system of equations with various restrictions?

1 view (last 30 days)
clear; clc
v=1.20e-6;
e=4.6e-5;
g=9.80665;
D=[0.3636 0.3636 0.4096 0.289 0.3636 0.3636 0.289 0.317];
L=[25 20 12 20 20 12 25 20];
Q=ones(1,8);
Re=ones(1,8);
f=ones(1,8);
hL=ones(1,8);
p=ones(1,8);
The metod to solve this particular problem needs the asumption of three values for Q and with those calculate the rest of the Qs.
But those Qs needs to satisfy three conditions; all of DQs must be less than 1e-4. (And all of the variables in the code needs to be positive real values).
Q1c=0.3970;
Q4c=0.0934;
Q8c=0.4340;
DQ1=0;
DQ2=0;
DQ3=0;
while abs(Q(1)/Q1c-1)>1e-3
Q(1)=Q1c-DQ1;
Q(4)=Q4c-DQ2;
Q(8)=Q8c-DQ3;
Q(2)=0.6-Q(1);
Q(3)=1+Q(4)-Q(1);
Q(5)=Q(2)+0.6-Q(3);
Q(6)=Q(5)+Q(8);
Q(7)=0.8-Q(8);
for i=1:8
Re(i)=4*Q(i)/(pi*D(i)*v);
end
for i=1:8
f(i)=0.25*(log10(e/(3.7*D(i))+5.74/(Re(i)^(0.9))))^(-2);
end
for i=1:8
hL(i)=8*f(i)*L(i)/(g*pi^(2)*D(i)^(5))*Q(i)^(2);
end
sum1=hL(3)+hL(2)-hL(1);
sum2=hL(5)+hL(6)-hL(3)-hL(4);
sum3=hL(7)-hL(6)-hL(8);
for i=1:8
p(i)=16*f(i)*L(i)/(g*pi^(2)*D(i)^(5))*Q(i);
end
sumk1=sum(p(1:3));
sumk2=sum(p(3:6));
sumk3=sum(p(6:8));
DQ1=sum1/sumk1;
DQ2=sum2/sumk2;
DQ3=sum3/sumk3;
end
Q(1)=Q1c;
Q(4)=Q4c;
Q(8)=Q8c;

Answers (1)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH on 5 Feb 2020
debes convertir tu código a función, donde la entrada sean las 3 Q que asumes y la salida el resto de Qs, y luego de eso utilizar el sistema de restricciones de Matlab https://la.mathworks.com/help/optim/ug/nonlinear-systems-with-constraints.html
además de esto tienes for que no son necesarios :
you can convert your code to function, where the input is the 3 Q that you assume and the output the rest of Qs, and after that use the Matlab restriction system https://la.mathworks.com/help/optim/ug /nonlinear-systems-with-constraints.html
furthermore you have unnecessary for loops:
v=1.20e-6;
e=4.6e-5;
g=9.80665;
D=[0.3636 0.3636 0.4096 0.289 0.3636 0.3636 0.289 0.317];
L=[25 20 12 20 20 12 25 20];
Q=ones(1,8);
Re=ones(1,8);
f=ones(1,8);
hL=ones(1,8);
p=ones(1,8);
Q1c=0.3970;
Q4c=0.0934;
Q8c=0.4340;
DQ1=0;
DQ2=0;
DQ3=0;
while abs(Q(1)/Q1c-1)>1e-3
Q(1)=Q1c-DQ1;
Q(4)=Q4c-DQ2;
Q(8)=Q8c-DQ3;
Q(2)=0.6-Q(1);
Q(3)=1+Q(4)-Q(1);
Q(5)=Q(2)+0.6-Q(3);
Q(6)=Q(5)+Q(8);
Q(7)=0.8-Q(8);
Re=4*Q./(pi.*D.*v);
f=0.25*(log10(e./(3.7*D)+5.74./(Re.^(0.9)))).^(-2);
hL=8*f.*L./(g*pi.^(2).*D.^(5)).*Q.^2;
sum1=hL(3)+hL(2)-hL(1);
sum2=hL(5)+hL(6)-hL(3)-hL(4);
sum3=hL(7)-hL(6)-hL(8);
p=16.*f.*L./(g*pi.^2.*D.^5).*Q;
sumk1=sum(p(1:3));
sumk2=sum(p(3:6));
sumk3=sum(p(6:8));
DQ1=sum1/sumk1;
DQ2=sum2/sumk2;
DQ3=sum3/sumk3;
end
Q(1)=Q1c;
Q(4)=Q4c;
Q(8)=Q8c;

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!