Symbolic Variable output from a function input of real number

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

Remove the line
syms D1 V1
and tell us what you try to achieve in the while-loop.
I want to achieve (if it's possible) to get Q1 and D1 depending on the input of the function ("ama"), I use the while loop to iterate and find a solution, because the fQ1 equation is transcendental (line 23, using the secant method). I don't know if there's an other specific function to get the desired result or another method.
Thank you.
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.
Thank you for your response, I don't know how I can arrange the two equations and then using fsolve, I keep having the error "undefined function or variable 'D1'".
If you write down the two equations, I'll show you.
I can't decipher them from your code.
This is the equation which the variables are V1, D1 and "ama":
ama-200-1.325/(log(0.001*10^-3/3.71*D1+5.74/(V1*D1/1.01*10^-6)^0.9))^2*1000/D1*V1^2/(2*9.81)=0;
And what is the aim ? Given ama, express D1 in terms of V1 or vice versa ?
To determine both D1 and V1, you need a second equation.

Sign in to comment.

Answers (0)

Categories

Products

Release

R2018b

Asked:

on 13 Mar 2022

Edited:

on 14 Mar 2022

Community Treasure Hunt

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

Start Hunting!