Effacer les filtres
Effacer les filtres

how to solve this error: FSOLVE requires all values returned by functions to be of data type double?

4 vues (au cours des 30 derniers jours)
function E = valvecoeff(x)
% cv_249 = x(1);
% cv_251 = x(2);
m_w1 = 3.5;
m_w2 = 1.5;
T_water_out_DIC1 = 80+273;
T_water_out_AC = 90+273;
kPa2Pa = 10^3;
%PD across circuit 2
PD_pipe2 = 0.96; % In kPa from toolbox online - assuming 3 inch dia, 20 ft length, flow rate for 5 kg/sec
PD_DIC1 = (160*(m_w2^2) + (2.3*10^3)*m_w2 - 10^4)/kPa2Pa; % PD across DIC1 in kPa
P_water_ent =400; % input('Pressure at which the water enters into the circuit in kPa');
P_water_ent_valve = P_water_ent - PD_DIC1 - PD_pipe2; % [kPa]
% T_water_out_DIC1 = % input('temp. at which water leaves DIC1 in K')
rho_w =refpropm('D','T',T_water_out_DIC1,'P',P_water_ent_valve,'water'); % density of entering water(kg/m^3)
Q_w2 = m_w2/rho_w; % vol. water flow across valve B249(m3/sec)
Q_w2_min = 60*Q_w2; % in m3/min % sec to min
Q_w2_gpm = 264.172*Q_w2_min; % (gpm)
% PD across circuit 1
PD_pipe1 = 0.96; % [kPa]
PD_oc = 5; % have an equation in terms of flow rate once you get the spec sheet from FSE
m_tot = m_w1 + m_w2; % water flow rate through AC (kg/sec)
PD_IC1= (590*(m_w1^2) + 0.42*m_w1+ 0.56)/kPa2Pa;
PD_IC2 =(590*(m_w1^2) + 0.4*m_w1+ 0.51)/kPa2Pa;
PD_AC= (1400*(m_tot^2) + 0.37* m_tot + 0.44)/kPa2Pa;
P_water_ent_251 = P_water_ent - PD_pipe1 - PD_oc - PD_IC1 - PD_IC2 - PD_AC; % [kPa]
rho_w1 =refpropm('D','T',T_water_out_AC,'P',P_water_ent_251,'water'); % % density of entering water(kg/m^3)
Q_w1 = m_tot/rho_w1;% vol. water flow across valve B251(m3/sec)
Q_w1_min = 60*Q_w1; % in m3/min % sec to min
Q_w1_gpm = 264.172*Q_w1_min; % (gpm)
PD_B249_psi = ((Q_w2_gpm/x(1))^2)*(10^2); % Q is in gpm, and Press drop is in psi
PD_B251_psi = ((Q_w1_gpm/x(2))^2)*(10^2); % Q is in m3/hr, and Press drop is in psi
PD_B249 = 6.89476*PD_B249_psi; %psi to kPa
PD_B251 = 6.89476*PD_B251_psi; %psi to kPa
PD_1 = PD_pipe1 +PD_oc +PD_IC1 +PD_IC2 + PD_AC + PD_B251;
PD_2 = PD_pipe2 + PD_DIC1 + PD_B249+ PD_AC+ PD_B251;
E = PD_1 - PD_2 == 0;
end
I am calling the above function using the snippet below but I get the error that: FSOLVE requires all values returned by functions to be of data type double.
fun = @valvecoeff;
x0 = [40,70];
d = fsolve(fun,x0)
  1 commentaire
Walter Roberson
Walter Roberson le 4 Oct 2017
The above appears to be fairly inefficient. All of your calculations up to and including Q_w1_gpm are working with constant values, building up values independent of the input x. For efficiency you should calculate all of those values once outside of the function, and pass the values you need in to the function, using

Connectez-vous pour commenter.

Réponses (1)

John D'Errico
John D'Errico le 4 Oct 2017
Modifié(e) : John D'Errico le 4 Oct 2017
Virtually impossible to read. Next time, learn to format your code to be readable.
Based on the line at the end...
E = PD_1 - PD_2 == 0;
Your equations appear to be in symbolic form. Or, that is a logical result. In either case, fsolve will fail. Fsolve is a NUMERICAL solver.
  3 commentaires
Walter Roberson
Walter Roberson le 4 Oct 2017
You appear to be working with all numeric values until you get to the final line
E = PD_1 - PD_2 == 0;
The "==" operator does not return a numeric data type: it returns a datatype named "logical" whose potential values as "false" or "true" . For computation purposes, false is 0 and true is 1, but fsolve tests the output data type using isnumeric() and logical values are not numeric.
It is most likely that the line should be changed to
E = PD_1 - PD_2;
John D'Errico
John D'Errico le 4 Oct 2017
Modifié(e) : John D'Errico le 4 Oct 2017
Elaborate on what? I can't even read your code.
https://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup#answer_18099
It looks like you have inputs there, apparently commented out. But hard to know without editing your post. Worse, you have a function called refpropm that I have no idea what it does or where it comes from. So I could never test your code anyway.
The line
E = PD_1 - PD_2 == 0;
is NOT how you return results for fsolve. Read the examples for fsolve.
If PD_1 and PD_2 are symbolic, then fsolve is the wrong tool to be using here in the first place.
If PD_1 and PD_2 are scalars, then you are trying to use fsolve to solve 1 equation in 2 unknowns. This will fail too.
If PD1 and PD2 are vectors of length 2, then fsolve will fail, because the line
E = PD_1 - PD_2 == 0;
is a logical test for equality. It will always generate either 0 or 1. Again, this is not how fsolve is used. READ THE HELP!

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by