Symbolic Variable output from a function input of real number
Show older comments
Hello, I made a function that outputs two variables ([Q1, D1]=tub1(ama), where ama is a real number) but it's giving me errors, after the declaration of constant values I implemented a while loop that uses the secant method, I stablished the initial condition for the while loop one line before (fQ1=1). I think that the problem here is that when it's applying all the equations the initial state which was a double now it's symbolic, I tried using double() and subs() at the end of each iteration to change the class of "fQ1" in order to be able to do the logical operation at the while loop condition but had no luck. The code needs to return the diameter (D1) and flow (Q1) (in a symbolic class) of a fluid circulating through a pipe given the relative pressure height at the inlet (ama) and the height difference (difz), the pipe discharges the fluid jet at the atmosphere (relative pressure=0). Here's the code that I made:
function [Q1,D1]=tub1(ama)
long=1550; %lenght of pipe
difz=200; %height difference
xi=0.001*10^-3; %rugosity of the pippe
g=9.81; %gravity acceleration
visc=1.01*10^-6; %kinematic viscosity
syms D1 V1
q1=1;
q2=10;
fQ1=1;
while abs(fQ1)>1*10^-6
for ii=1:2
if ii==1
Q1=q1;
else
Q1=q2;
end
%Re1=4*Q1/(pi*visc*D1*3600);
V1=4*Q1/(pi*D1^2*3600); %calculation of the speed of the fluid, dividing by 3600 to have the flow in m^3/h
Re1=V1*D1/visc; %Reynolds number
f1=1.325/(log(xi/3.71*D1+5.74/Re1^0.9))^2; %Jain equation for the Darcy friction factor
ahc1=f1*long(1)/D1*V1^2/(2*g); %total frictional losses
fQ1=ama-difz-ahc1; %continuity equation equal to zero
if ii==1
fq1=fQ1;
else
fq2=fQ1;
end
end
q3=q2-fq2*(q2-q1)/(fq2-fq1);
q1=q2;
q2=q3;
D1=4*Q1/(pi*V1*3600);
end
This is the error that I get:
Conversion to logical from sym is not possible.
Error in tub1 (line 11)
while abs(fQ1)>1*10^-6
Any help or advise will be gratly appreciated. Thank you
7 Comments
Torsten
on 13 Mar 2022
Remove the line
syms D1 V1
and tell us what you try to achieve in the while-loop.
Enric Gironès
on 13 Mar 2022
The usual method is to write two equations
f1(D1,V1) = 0
f2(D1,V1) = 0
and to use "fsolve" to solve for D1 and V1.
And I recommend not to use symbolic calculations for such time-consuming operations.
Enric Gironès
on 14 Mar 2022
Torsten
on 14 Mar 2022
If you write down the two equations, I'll show you.
I can't decipher them from your code.
Enric Gironès
on 14 Mar 2022
Answers (0)
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!